问题
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
- multipart/alternative
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