Extending MimeMessage

J

Jim G

I thought it would be useful to create a class that holds a javamail
Message even after the mail store is closed. To do this I decided to
extend MimeMessage. However MimeMessage has no default constructor so
I got in a mess selecting which constructor to execute. MimeMessage
gas really easy ways to create itself, but not when you extend it due
to the constraints of how and when super can be called.

I wanted this class to construct the Message as part of it
constructor, but the closest I could get was the following code.

This creates an replica in an overall sense, but the headers fields
are in a different order and sometimes it drops an irrelevant blank
line in an attachment, probably due to reencoding.

It's a theoretical question, but can anyone see a better way of doing
this using a constructor. BTW, I did create a true replica in the end
using an instance method.


public class PersistentMimeMessage extends MimeMessage {

static Properties props = new Properties();
static Session session = Session.getDefaultInstance(props);

public PersistentMimeMessage(Message m) {

super(session); // Create an empty, orphan MimeMessage
try {
Enumeration enum = m.getAllHeaders();
while(enum.hasMoreElements()) {
Header h = (Header) enum.nextElement();
super.setHeader(h.getName(), h.getValue());
}
Object content = m.getContent();
super.setContent(content, m.getContentType());
super.saveChanges();

} catch (MessagingException e1) {
} catch (IOException e) {
e.printStackTrace();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
DewittMill
Top