JavaMail: MimeMessage customer headers

A

Abraham Khalil

How to add custom headers to a MimeMessage object?
The problem I'am having is reading from POP3 account and getting the
MimeMessage object. Before I want to save the message content to disk,
I want to add a few headers.

The error I'am getting is
POP3 messages are read-only

I've open the inbox folder as READ_WRITE but still no luck.
Anyone know how to add customer headers to the MimeMessage object when
reading from a POP3 account?

Thanks
 
G

GaryM

(e-mail address removed) (Abraham Khalil) wrote in
I've open the inbox folder as READ_WRITE but still no luck.
Anyone know how to add customer headers to the MimeMessage object
when reading from a POP3 account?

You're working with the reference to the original Message. There are
2 ways I can think of to do it.

1. Dupe the message

FileOutputStream outstream = new FileOutputStream(filename);
MimeMessage myNewMessage = new MimeMessage(theOneIWantToChange);
myNewMessage.setHeader("My-Header","My Head Value");
myNewMessage.write(outstream);
outstream.close();


2. Insert header into output stream

FileOutputStream outstream = new FileOutputStream(filename);
PrintWriter out = new PrintWriter(outstream);
out.println("X-My-Header: My Header Value");
out.flush();
message.writeTo(outstream); // the mimemessage
outstream.flush();
out.close();
outstream.close();


HTH,

Gary
 
A

Abraham Khalil

Thanks in a million. It was a reference problem.
Was doing the first one, cloning the message object but giving two
great answers is a bonus. Number two is definately a brillant solution
as its
more efficient than cloning the whole object. Once again, thank you
for
your contribution.
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top