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: 
Actually, `MimeMessage` [does not implement `Serializable` by design](http://www.oracle.com/technetwork/java/javamail/faq-135477.html#serialize), you can extend `MimeMessage` to do so but you do not need to as `MimeMessage` has facilities using [writeTo(OutputStream)](https://javamail.java.net/nonav/docs/api/javax/mail/internet/MimeMessage.html#writeTo(java.io.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);
}

```

You can then read this message in for later processing using the [MimeMessage(Session,InputStream)](https://javamail.java.net/nonav/docs/api/javax/mail/internet/MimeMessage.html#MimeMessage(javax.mail.Session,%20java.io.InputStream)) constructor with the session object.

```
Session session = Session.getInstance(props);
try (InputStream str = Files.newInputStream(Paths.get("message.eml"))) {
    MimeMessage msg = new MimeMessage(session, str);
    // Do something with the message, maybe send it.
    Transport.send(msg);
}

```

If you happen to be using spring's [JavaMailSender](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/mail/javamail/JavaMailSender.html) then you can also construct new mime messages through the configured session by using [createMimeMessage(InputStream)](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/mail/javamail/JavaMailSender.html#createMimeMessage-java.io.InputStream-) which uses the configured session.
This has worked for me

***Serialisation***

```cs
            var mailMessage = new MimeMessage();
            mailMessage.From.Add(new MailboxAddress("from", "email@example.com"));
            mailMessage.To.Add(new MailboxAddress("to", "email2@example.com"));
            mailMessage.ReplyTo.Add(new MailboxAddress("reply", "email3@example.com"));
            mailMessage.Subject = "Test subject";
            var bodyBuilder = new BodyBuilder();
            bodyBuilder.TextBody = "GenericEmail";
            bodyBuilder.HtmlBody = JsonConvert.SerializeObject(new Settings() { Exchange = "x" });
            mailMessage.Body = bodyBuilder.ToMessageBody();
            using var memoryStream = new MemoryStream();
            mailMessage.WriteTo(memoryStream);
            return memoryStream.ToArray();

```

***Deserialisation***

```cs
            using var stream = new MemoryStream(data);
            return MimeMessage.Load(stream);

```

Note that my use case is to send the email to AMQP queue, so that the email sender sends it when available or ready.. My AMQP receiver processes the email template so I send the template in textbody and variables in htmlbody