向现有的MimeMessage添加附件 [英] Adding attachment to existing MimeMessage

查看:1720
本文介绍了向现有的MimeMessage添加附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读这样的Mime消息:

I am reading a Mime message like this:

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

现在我只需要简单地将附件添加到现有的MimeMessage中即可.

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

我该怎么做?

我尝试过:

        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.

推荐答案

因此,在这种情况下,答案将是创建一条具有以下旧消息内容的新消息:Multipart multipart = (Multipart)message.getContent();

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.

这篇关于向现有的MimeMessage添加附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆