这可能是一个非常愚蠢的问题,但我正在尝试撰写电子邮件,例如suggested here
所以我有
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");
并且需要将altPart
添加到mixedPart
,但我不能,因为唯一的添加方法只接受BodyPart
。 WTF?
请注意,与here不同,我不会混淆包裹。
答案 0 :(得分:2)
您需要使用MimeMultipart
方法将MimeBodyPart
包裹在另一个MimeBodyPart.setContent(Multipart mp)
中。然后,您可以将MimeBodyPart
添加到mixedPart
对象:
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);