Javamail get all emails form a specific sender

imapjakarta-mailjavapop3

In java I need to get all emails from a specific sender. There are couple of ways to do that as shown here: http://www.codejava.net/java-ee/javamail/using-javamail-for-searching-e-mail-messages

But I need to have the process to be done on gmail side. It is not fine for me to read all emails and then decide which one I should proceed.

Also in http://alvinalexander.com/java/javamail-multiple-search-terms-pop3-mailbox-yahoo you can search message body which it is great, but what about the sender (FROM part)? How can I filter it?

Also reading all emails each time is not good for our bandwidth (Of course we can mark the last read email and read just all new emails somehow like using UIDFolder)

Thanks

Best Answer

I found it myself :)

Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);

SearchTerm sender = new FromTerm(new InternetAddress("from@example.com"));
Message[] messages = inbox.search(sender);

for (int i = 1; i < messages.length; i++) {
    read_message(messages[i]);
}

All I needed was to add FromTerm!

Related Topic