Home > Open Source, Uncategorized > Checking Email with Java

Checking Email with Java

November 3rd, 2007 deevis

I’ve probably used Java to send email somewhere in the neighborhood of 30 times and have never really had much trouble with it. But how in blazes do I connect to a (Pop3) account and pull down email? Well, after much cursing at the javamail API the solution crystallized to something containing the following:

Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
store = session.getStore("pop3");
store.connect( "mail.isp.com", "myUserName", "myPassword" );
inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Message[] msgs = inbox.getMessages();
// Do something with the messages here
inbox.close(false);
store.close();

It should be noted that the elusive activation.jar and mail.jar from J2EE 1.4 were used to get the above to compile and run. When I tried using the JEE5 libraries, I didn’t have a POP3 implementation and got “JavaMail – NoSuchProviderException: No provider for pop3″ exceptions.

Here’s a code snippet that will show you all of the providers you have available to you:


Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
Provider[] p = session.getProviders();
for (int i=0;i System.out.println(p[i].getProtocol());
}

I get back the results: imap, pop3, smtp.

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Technorati
  • TwitThis
Categories: Open Source, Uncategorized Tags:
Comments are closed.