为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

小唯快跑啊 2019-11-05 11:24:48
当创建的电子邮件,你都应该设置内容类型来multipart/alternative发送HTML和文本时,或multipart/mixed发送文本和附件时。如果要发送HTML,文本和附件,该怎么办?同时使用?
查看完整描述

3 回答

?
蝴蝶刀刀

TA贡献1801条经验 获得超8个赞

使用multipart/mixed与第一部分为multipart/alternative及以后的零件的附件。反过来,在零件内使用text/plaintext/html零件multipart/alternative

有能力的电子邮件客户端应然后识别该multipart/alternative部分,并在必要时显示文本部分或html部分。它还应将所有随后的零件显示为附件。

这里要注意的重要一点是,在多部分MIME消息中,部分内包含部分是完全有效的。从理论上讲,嵌套可以扩展到任何深度。然后,任何具有合理能力的电子邮件客户端都应该能够递归处理所有消息部分。


查看完整回答
反对 回复 2019-11-05
?
慕容3067478

TA贡献1773条经验 获得超3个赞

消息有内容。内容可以是文本,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);


查看完整回答
反对 回复 2019-11-05
  • 3 回答
  • 0 关注
  • 361 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信