Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Create Intelligent E-mail Filters with JavaMail and Classifier4j : Page 2

Tired of the limitations and annoying false positives with commercial spam filters? Classifier4J is an open source Java library that will let you build custom applications that read e-mails and other types of text documents, separating the wheat from the chaff exactly the way you intend.


advertisement
Building Your First E-mail Client
This application will be a very simple console application; you can expand it later into something more complex. One cool idea is to build it into a servlet-based application that gives you a hosted e-mail client, such as Hotmail or Yahoo!, but with a built-in spam filter and classifier.

Here is the application:

package com.devx.jmail; import javax.mail.*; import javax.mail.internet.*; import java.util.*; import java.io.*; import net.sf.classifier4J.*; public class MailReader { public static void main(String[] args) { try { String popServer="yourpopserveraddress"; String popUser="yourpopusername"; String popPassword="yourpoppassword"; GetMail(popServer, popUser, popPassword); } catch (Exception e) { e.printStackTrace(); } System.exit(0); }

The values for popServer, popUser, and popPassword should be the correct values for your POP3 account or the application won't work. As you can see this is a very simple console application that doesn't do much (yet!), and the GetMail function is the workhorse.

Getting Mail
The JavaMail API is huge, and has way too much depth to go into detail here, so for the purposes of this example you'll be doing the simplest thing possible: logging in, scanning the inbox for contents, and downloading a copy of those contents. You can view the full function GetMail in the download, but the snippets that handle the heavy lifting are shown here:

store.connect(popServer, popUser, popPassword); folder = store.getDefaultFolder(); if (folder == null) throw new Exception("No default folder"); folder = folder.getFolder("INBOX"); if (folder == null) throw new Exception("No POP3 INBOX"); folder.open(Folder.READ_ONLY); Message[] msgs = folder.getMessages(); for (int nMsg = 0; nMsg < msgs.length; nMsg++) { strEmail = buildMessage(msgs[nMsg]); }

After setting up a JavaMail store object, you connect to it using the ServerName, UserName, and Password parameters. Should this work and not throw an exception (the code in Listing 2 should be writing a try..catch clause), you will be able to get the default folder associated with the store. If there is no default folder, then an exception will be thrown.

Every POP3 account has an 'INBOX' folder containing incoming mail. If that folder exists, it is opened, and an array of Message objects is read from it. This is the list of all mail that is currently in your inbox. Don't worry, reading the mail won't delete it from your inbox as the folder is opened as 'READ_ONLY.'

You then loop through all of these Message objects and build a string out of the Message using the 'buildMessage' function. This function is available in its entirety in the download, but the key parts of it are shown here:


InputStream is = messagePart.getInputStream(); BufferedReader reader=new BufferedReader(new InputStreamReader(is)); String thisLine=reader.readLine(); while (thisLine!=null) { strReturn +=thisLine; thisLine=reader.readLine(); }

A POP3 e-mail message is made up of a number of entities, including the sender name, sender address, subject, and body. The body can be made up of a number of parts and may include attachments. The buildMessage function gets all these entities and simply appends them all to a string that it returns to the caller.

The important part of the message for our example is the e-mail body. This can be numerous lines of text, so the messagePart object (which is built from the e-mail body, see the full function) exposes an InputStream that you can use to read it line by line. This is used to create a BufferedReader, which then reads in the e-mail body.

You now have a simple e-mail client that logs in to your POP3 box, gets the mail from your inbox, downloads them one by one, and converts them into a string that can be used for classification and summarization with Classifier4J.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date