Question: 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();

```

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

```

Is there any way to achieve serializing a MimeMessage instance or to go around and hack it in some other way?

Answer: 
`NotSerializableException` is thrown when the Object being serialized does not implement `java.io.Serializable` interface. Since, [javax.mail.internet.MimeMessage](http://docs.oracle.com/javaee/6/api/javax/mail/internet/MimeMessage.html) does not implement this interface it *cannot* be serialized.

```
public class MimeMessage extends Message implements MimePart

```

Consider serializing its content instead; by wrapping it up in a custom domain object (with the message text and recipients) that *implements Serializable*. De-serialize this domain object when required and then go on to construct a new *MimeMessage* from its contents.
We can create MimeMessage and write it to a ByteArrayOutputStream as below:

```
JavaMailSenderImpl sender = new JavaMailSenderImpl();
MimeMessage message = sender.createMimeMessage();
MimeMailMessage mimeMailMessage = new MimeMailMessage(message); 
mimeMailMessage.setTo("To@address.com");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
mimeMailMessage.getMimeMessage().writeTo(bos);
byte[] byteArrayMessage = bos.toByteArray());
bos.close();

```