Adding attachment to existing MimeMessage

穿精又带淫゛_ 提交于 2019-12-11 07:43:18

问题


I am reading a Mime message like this:

             InputStream is = new FileInputStream("c:\\Temp\\test.eml");
             MimeMessage message = new MimeMessage(session,is);

Now i need to simply add an attachment to the existing MimeMessage without changing anything else.

How can i do this?

I tried:

        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);

But it seems to change the original message.


回答1:


So, the answer in this case will be to create a new Message with the content of the old message like this: Multipart multipart = (Multipart)message.getContent();

And then add the attachment to the new message.



来源:https://stackoverflow.com/questions/20264964/adding-attachment-to-existing-mimemessage

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