Recently I wrote a java daemon process that monitors a POP3 email box for messages, automatically downloads the messages, and then processes them. Basically my daemon process looks for returned and bounced emails in a POP3 mailbox and then updates certain flags in our ERP system so we don’t send to that email address again. In some cases people reply to the messages our ERP system sends out, and in that case my daemon process forwards those messages to an appropriate user for manual review and handling.
I’m not going to go into the details of how this daemon process work since it goes far beyond the scope of this post (and it is a huge piece of software in itself). However, I am going to share the basic java class that I use to check the POP3 mail account, download messages, and then loop through them for processing.
For this example to work, you will need to download the JavaBeans Activation Framework, and the Javamail libraries. Unzip the downloaded archives and put the appropriate jar file into your classpath. This example was tested with JavaMail 1.3.1 (mail-1.3.1.jar) and the Javabeans Activation Framework 1.0.2 (activation-1.0.2.jar).
My example also uses Log4j to log its output. If you don’t want to configure log4j, then replace all of my log.info and log.debug calls with a call to System.out.println.
Once you get your classpath setup, take the class I present below and compile it. The only thing you’ll have to modify to run the example is a few lines in the main() method. Once you get that running, you should be able to take the code, pick it apart, and make it do what you need for your own applications.