venerdì 22 gennaio 2010

Retrieving Mail with Java


Software Designers and Programmers don't make a great use of incoming mail automation.
It is a pity, because handling incoming mail can offer important application advantages.
We try to explain why with the help of 2 small scenarios:

Replies to Invitations/Surveys Processing


Let’s imagine that you send mail to a group of people in order to invite them to an event, make a survey, or some other scope.
After the sending, you have to process replies, by means of:

  • a Form in an Internet/Application (costly and awkward for the user),
  • a Secretary that receives and registers replies from people involved,
  • a simple Reply to the mail

In this last case you don’t need to have an internet application (hosted by some provider) or dedicated human resources.

Document Management


If you deal with a Document Management system you know well that the activity most annoying for users is to upload files into the System.
You can easily make the System do it by itself just sending a mail to the proper MailBox.

How to write a class for incoming mail handling


Now, we are going to explain how manage mail retrieval with Java.

Additional Library


The first, trivial but important issue is that you need to download to an additional (javax.mail) library, not generally included into J2EE Application Servers.

We need to use the following classes:

  • javax.mail.Session: represents a mail session
  • javax.mail.Store: an abstract class that models a message store and its access protocol, for storing and retrieving messages
  • javax.mail.Folder: An abstract class that represents a folder for mail messages
  • javax.mail.Message: Represents an email message

Coding Step by Step


First of all get a Session object:
Properties props = new Properties(); 
Session session = Session.getDefaultInstance(props, null); 

and then make a connection to the Store with the getStore() method::

Store store = session.getStore(“pop3”);

From the Store object we can connect to server with three parameters: host, username and password:

String host = "mypop.it";
String username = "myusername";
String password = "mypassword";
store.connect(host, username, password);

Once we are connected, we need to access the appropriate Folder before mail retrieval.

If POP mail is used, only one folder is available, INBOX.
In case of IMAP Server type, the Folder can be INBOX or any other folder name that was set up. Assuming you want to read mail from your INBOX, simply connect to the folder with the getFolder() method of Session and then open the Folder with the open() method:

Folder folder = store.getFolder("INBOX");     
folder.open(Folder.READ_WRITE); 

You can use the getMessage() method to get an individual message, or as in the following example, use the getMessages() method to get all the messages::

Message[] messages = folder.getMessages(); 

Now, for example, we can save all messages in a directory:

for (int i = 0; i < messages.length; i++){
File mymail = new File(“C:/dir_email/nome_email.eml”);
OutputStream out=new FileOutputStream(mymail);              
mess[i].writeTo(out); 
mess[i].setFlag(Flags.Flag.DELETED,true); /*set flag=true to delete file */
}
Finally, close folder and connection:
folder.close(true); 
store.close(); 



Complete example

A complete example can be retrieved here: ServiceMail.java.

Nessun commento:

Posta un commento