我正在阅读这样的 Mime 消息:
InputStream is = new FileInputStream("c:\\Temp\\test.eml");
MimeMessage message = new MimeMessage(session,is);
现在我只需要在现有的 MimeMessage 中添加一个附件,而无需更改任何其他内容。
我该怎么做?
我试过:
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource("C:\\attachment.pdf");
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("encrypted_body.pdf");
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
message.setContent(multipart);
但它似乎改变了原来的消息。
最佳答案
因此,在这种情况下,答案将是创建一个包含旧消息内容的新消息,如下所示:Multipart multipart = (Multipart)message.getContent();
然后将附件添加到新邮件中。
关于java - 向现有 MimeMessage 添加附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20264964/