Using JavaMail with multiple search terms (Yahoo mailbox POP3 searching)

I normally don't share source code that doesn't work, but I just want to make a note here for myself on how to use JavaMail to search for multiple search terms. While the code doesn't currently work, I believe it does show the correct syntax for searching for multiple search terms, and there's just something about the overall JavaMail API (or Yahoo Mail) that I don't understand at this moment.

package javamailtests;

import java.io.InputStream;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.search.*;

public class JavaMailMultipleSearchTerms {

  public static void main(String args[]) throws Exception {

    // (1) mail server info
    String host = "pop.mail.yahoo.com";
    String user = "USERNAME";
    String password = "PASSWORD";

    // (2) connect to my pop3 inbox in read-only mode
    Properties properties = System.getProperties();
    Session session = Session.getDefaultInstance(properties);
    Store store = session.getStore("pop3");
    store.connect(host, user, password);
    Folder inbox = store.getFolder("inbox");
    inbox.open(Folder.READ_ONLY);

    // (3) create a search term for all "unseen" messages
    Flags seen = new Flags(Flags.Flag.SEEN);
    FlagTerm unseenFlagTerm = new FlagTerm(seen, false);

    // (4) create a search term for all recent messages
    Flags recent = new Flags(Flags.Flag.RECENT);
    FlagTerm recentFlagTerm = new FlagTerm(recent, true);

    // (5) combine the search terms with a JavaMail AndTerm:
    //     http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#JavaMailFetching
    SearchTerm searchTerm = new AndTerm(unseenFlagTerm, recentFlagTerm);
    Message[] messages = inbox.search(searchTerm);
    
    if (messages.length == 0) System.out.println("No messages found.");

    for (int i = 0; i < messages.length; i++) {
      System.out.println("Message " + (i + 1));
      System.out.println("From : " + messages[i].getFrom()[0]);
      System.out.println("Subject : " + messages[i].getSubject());
      System.out.println("Sent Date : " + messages[i].getSentDate());
      System.out.println();
    }

    inbox.close(true);
    store.close();
  }
}

As you can see from the code, I try to create these two JavaMail API search terms (FlagTerm instances):

// (3) create a search term for all "unseen" messages
Flags seen = new Flags(Flags.Flag.SEEN);
FlagTerm unseenFlagTerm = new FlagTerm(seen, false);

// (4) create a search term for all recent messages
Flags recent = new Flags(Flags.Flag.RECENT);
FlagTerm recentFlagTerm = new FlagTerm(recent, true);

I then create a JavaMail SearchTerm in the following code, by combining my two FlagTerm instances with a JavaMail AndTerm:

SearchTerm searchTerm = new AndTerm(unseenFlagTerm, recentFlagTerm);

Once that SearchTerm is created, I try to search for the list of mailbox messages which matches my search criteria here:

Message[] messages = inbox.search(searchTerm);

As mentioned, this code isn't currently working -- it doesn't return any messages from my Yahoo Mail account, even though I know I have unread, recent messages.

I need to work on some other things for a few days, so I'm just putting this code out here in hopes it will help someone else attempting to use the JavaMail API to use multiple search criteria for searching your own mailboxes. (And if someone else happens to know what's wrong with my approach, I'm open to suggestions also.)

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.