邮件分段/替代vs分段/混合

当创建的电子邮件,你都应该设置内容类型来multipart/alternative发送HTML和文本时,或multipart/mixed发送文本和附件时。


如果要发送HTML,文本和附件,该怎么办?同时使用?


小唯快跑啊
浏览 363回答 3
3回答

蝴蝶刀刀

使用multipart/mixed与第一部分为multipart/alternative及以后的零件的附件。反过来,在零件内使用text/plain和text/html零件multipart/alternative。有能力的电子邮件客户端应然后识别该multipart/alternative部分,并在必要时显示文本部分或html部分。它还应将所有随后的零件显示为附件。这里要注意的重要一点是,在多部分MIME消息中,部分内包含部分是完全有效的。从理论上讲,嵌套可以扩展到任何深度。然后,任何具有合理能力的电子邮件客户端都应该能够递归处理所有消息部分。

慕容3067478

消息有内容。内容可以是文本,html,DataHandler或Multipart,并且只能有一个内容。多个零件只有身体零件,但可以有多个零件。像消息一样,BodyParts可以包含已经描述的内容。带有HTML,文本和附件的消息可以按如下所示分层查看:message  mainMultipart (content for message, subType="mixed")    ->htmlAndTextBodyPart (bodyPart1 for mainMultipart)      ->htmlAndTextMultipart (content for htmlAndTextBodyPart, subType="alternative")        ->textBodyPart (bodyPart2 for the htmlAndTextMultipart)          ->text (content for textBodyPart)        ->htmlBodyPart (bodyPart1 for htmlAndTextMultipart)          ->html (content for htmlBodyPart)    ->fileBodyPart1 (bodyPart2 for the mainMultipart)      ->FileDataHandler (content for fileBodyPart1 )以及构建此类消息的代码:    // the parent or main part if you will    Multipart mainMultipart = new MimeMultipart("mixed");    // this will hold text and html and tells the client there are 2 versions of the message (html and text). presumably text    // being the alternative to html    Multipart htmlAndTextMultipart = new MimeMultipart("alternative");    // set text    MimeBodyPart textBodyPart = new MimeBodyPart();    textBodyPart.setText(text);    htmlAndTextMultipart.addBodyPart(textBodyPart);    // set html (set this last per rfc1341 which states last = best)    MimeBodyPart htmlBodyPart = new MimeBodyPart();    htmlBodyPart.setContent(html, "text/html; charset=utf-8");    htmlAndTextMultipart.addBodyPart(htmlBodyPart);    // stuff the multipart into a bodypart and add the bodyPart to the mainMultipart    MimeBodyPart htmlAndTextBodyPart = new MimeBodyPart();    htmlAndTextBodyPart.setContent(htmlAndTextMultipart);    mainMultipart.addBodyPart(htmlAndTextBodyPart);    // attach file body parts directly to the mainMultipart    MimeBodyPart filePart = new MimeBodyPart();    FileDataSource fds = new FileDataSource("/path/to/some/file.txt");    filePart.setDataHandler(new DataHandler(fds));    filePart.setFileName(fds.getName());    mainMultipart.addBodyPart(filePart);    // set message content    message.setContent(mainMultipart);
打开App,查看更多内容
随时随地看视频慕课网APP