How can I find out how many messages are waiting to be read on my IMAP/POP server?

John Zukowski

The getMessageCount() method of Folder reports this information. The following program demonstrates.

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

public class GetCountExample {
  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");

    // Get Count
    int count = folder.getMessageCount();
    System.out.println("Messages waiting: " 
      + count);
    count = folder.getUnreadMessageCount();
    System.out.println("Unread messages waiting: " 
      + count);

    // Close connection