问题描述
我一直试图序列化一个MimeMessage实例,但是当我在网上阅读时,它是不可能的。我想通过序列化MimeMessage实例来实现的是我想对该实例进行散列并将其沿邮件发送。我到目前为止编码是这样的:
I have been trying to serialize a MimeMessage instance, but as I read on web it is not possible. What I want to achieve with serializing a MimeMessage instance is that I want to hash that instance and send it along mail itself. What I coded so far is this:
MimeMessage message = new MimeMessage(session);
//...setting up content of MimeMessage
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("object.ser")));
oos.writeObject(message);
oos.close();
它在GlassFish服务器上编译,但在尝试使用服务时遇到运行时错误。它说:
It compiles on GlassFish server, but I get a runtime error when I try to use service. It says:
exception
java.io.NotSerializableException: javax.mail.internet.MimeMessage
我试过这样做;但它不起作用:
I tried it to do in this way; yet it didn't work, either:
Object obj = new Object();
obj = (Object)message;
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("object.ser")));
oos.writeObject(obj);
oos.close();
是否有任何方法可以实现序列化MimeMessage实例或绕过并以其他方式破解它?
Is there any way to achieve serializing a MimeMessage instance or to go around and hack it in some other way?
推荐答案
实际上, MimeMessage
,以便将内容保存为n RFC- 822 mime message。
Actually, MimeMessage
does not implement Serializable
by design, you can extend MimeMessage
to do so but you do not need to as MimeMessage
has facilities using writeTo(OutputStream) to allow you to save the content as n RFC-822 mime message.
try (OutputStream str = Files.newOutputStream(Paths.get("message.eml"))) {
msg.writeTo(str);
}
然后,您可以阅读此消息以供日后处理使用,那么你也可以通过配置的会话构建新的MIME消息,使用,它使用已配置的会话。
If you happen to be using spring's JavaMailSender then you can also construct new mime messages through the configured session by using createMimeMessage(InputStream) which uses the configured session.
这篇关于如何序列化Mimemessage实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!