How can I get a list of messages from my POP3 server?

John Zukowski

After getting a POP3 provider for JavaMail (quite possibly from Sun at http://java.sun.com/products/javamail/pop3.html, getting a list of messages involves the following steps:

  1. Creating a session
  2. Getting a store for the POP3 provider
  3. Getting a folder to read messages from (even if the provider doesn't use folders)
  4. Then finally getting a directory of messages

The following program demonstrates this:

import javax.mail.*;
import javax.mail.internet.*;

public class GetListExample {
  public static void main (String args[]) 
      throws Exception {
    String host = args[0];
    String username = args[1];
    String password = args[2];

    // Get session
    Session session = Session.getInstance(
      System.getProperties(), null);

    // Get the store
    Store store = session.getStore("pop3");
    store.connect(host, username, password);

    // Get folder
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_ONLY);

    // Get directory
    Message message[] = folder.getMessages();
    for (int i=0, n=message.length; i<n; i++) {
       System.out.println(i + ": " 
         + message[i].getFrom()[0] 
         + "	" + message[i].getSubject());
    }

    // Close connection 
    folder.close(false);
    store.close();
  }
}
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


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

 

 

About | Sitemap | Contact