将MimeMessage附加到另一个MimeMessage

时间:2018-11-06 16:58:35

标签: java mime mime-message

我有一个MimeMessage A,并且想添加另一个MimeMessage B作为附件。

我(不起作用)的尝试:

MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(base64mimeMessage, "message/rfc822");
mimeBodyPart.setHeader("Content-Transfer-Encoding", "base64");
multipart.addBodyPart(mimeBodyPart);

稍后尝试对结果签名或加密的原因是哪个java.io.IOException: unsupported object at com.sun.mail.handlers.message_rfc822.writeTo

2 个答案:

答案 0 :(得分:0)

也许对其他人有帮助,所以这是一个可行的解决方案

MimeBodyPart mimeBodyPart = new MimeBodyPart();
DataSource dataSource = new ByteArrayDataSource(Base64.decode(itemAttachment.getMimeContent(), Base64.DEFAULT), "message/rfc822");
mimeBodyPart.setDataHandler(new DataHandler(dataSource));
multipart.addBodyPart(mimeBodyPart);

答案 1 :(得分:0)

Session session = Session.getInstance(new Properties());
MimeMessage mimeMessageToAttach = new MimeMessage(session);

MimeBodyPart attachment = new MimeBodyPart();
String contentType = "message/rfc822; name=mymail.eml";
attachment.setContent(mimeMessageToAttach, contentType);

MimeMultipart multipart = new MimeMultipart();
multipart.addBodyPart(mimeBodyPart);