logo       

Re: Best Java library for parsing email messages?: msg#00506

lang.scala

Subject: Re: Best Java library for parsing email messages?

Well, one solution that basically works is to use the Java Mail API
along with a library called mstor. You can google for both of those to
find out about them, how to install them; it's just a matter of
throwing the necessary jars in your classpath.

Here is some sample code that reads a message from the SpamAssassin
corpus, a ham message, and prints out the from and subject fields
without me having to write my own parser.

import java.util.Properties
import javax.mail._
import javax.mail.internet._
import net.fortuna.mstor._
import net.fortuna.mstor.data._
import net.fortuna.mstor.data.xml._
import net.fortuna.mstor.search._
import net.fortuna.mstor.tag._
import net.fortuna.mstor.util._

object msgexample extends Application {
val session = Session.getDefaultInstance(new Properties)
val store = session.getStore(new
URLName("mstor:g:/dl/20021010_easy_ham/easy_ham"))
store.connect
// idea: a single file storing one mail message is a valid mbox file
// akward but it works
val folder = store.getFolder("0001.ea7e79d3153e7469e7a9c3e0af6a357e")
folder.open(Folder.READ_ONLY)
val msg = folder.getMessages()(0)

Console.println("From: " + msg.getFrom()(0))
Console.println("Subject: " + msg.getSubject()(0))
// could also do msg.getContent analogously if we wanted, etc.
// java mail api docs cover the fields available
}

Hooray for not reinventing the wheel. Hooray for code that works
albeit slightly akwardly. Yay!

Warren



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise