views:

136

answers:

2

How does one parse the mail files generated on a Linux system using Java? I mean, I want to be able to extract the From, To, Timestamp and Subject of the various emails inside the file. Any suggestions?

+1  A: 

Those files belong to the Mail Transfer Agent and maybe the user's mail client. Other programs should tread very softly or better yet keep out altogether. Or is your program a mail client?

The "clean" way to do this would be to open up an SMTP or IMAP connection to the mail server / MTA and ask it for pieces of mail on behalf of your user, using his credentials that he gives you.

There's a Java mail API for this that knows how to do this well: http://java.sun.com/products/javamail/ .

Carl Smotricz
Oh I am not writing a mail client. It is just to analyze some statistics about a survey.
Legend
+2  A: 

javax.mail.internet.MimeMessage.parse(InputStream)

it's protected but you can subclass it to use the method. However, the file format is quite simple, if you just want some headers, why not parse it on your own?

irreputable
I'll evaluate that. Thanks. I wrote a parsing script in Python but I was facing some problems with some content.
Legend