How to add a MimeMultipart to another one?

末鹿安然 提交于 2019-12-24 22:13:35

问题


This is possibly a very stupid question, but I'm trying to compose an Email message like suggested here

  • multipart/mixed
    • multipart/alternative
      • text/html
      • text/plain
    • attachment 1
    • attachment 2

So I'm having

MimeMultipart altPart = new MimeMultipart("alternative");

BodyPart textPart = new MimeBodyPart();
textPart.setContent("someText", "text/plain");
altPart.addBodyPart(textPart);

BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent("someHtml", "text/html");
altPart.addBodyPart(htmlPart);

MimeMultipart mixedPart = new MimeMultipart("multipart/mixed");

and need to add altPart to mixedPart, but I can't as the only adding method accepts BodyPart only. WTF?

Note that unlike here, I'm not mixing up packages.


回答1:


You need to wrap your MimeMultipart in another MimeBodyPart, using the MimeBodyPart.setContent(Multipart mp) method. Then you can add the MimeBodyPart to the mixedPart Object:

MimeMultipart alternativeMultipart = new MimeMultipart("alternative");

BodyPart textPart = new MimeBodyPart();
textPart.setContent("someText", "text/plain");
alternativeMultipart.addBodyPart(textPart);

BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent("someHtml", "text/html");
alternativeMultipart.addBodyPart(htmlPart);

MimeBodyPart alternativeBodyPart = new MimeBodyPart();
alternativeBodyPart.setContent(alternativeMultipart);

MimeMultipart mixedMultipart = new MimeMultipart("mixed");
mixedMultipart.addBodyPart(alternativeBodyPart);

MimeBodyPart textPart1 = new MimeBodyPart();
textPart1.setContent("someOtherText", "text/plain");
mixedMultipart.addBodyPart(textPart1);


来源:https://stackoverflow.com/questions/29549863/how-to-add-a-mimemultipart-to-another-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!