albert green
albert green

Reputation: 429

Reading emails with groovy (Java Mail)

I am using groovy in order to access gmail and read the Inbox. It is regular JavaMail and will not describe it here.

So for simplicity, after I connect to the store, I use this:

folder.open(Folder.READ_ONLY)

 folder.messages.each { msg ->
    ...
    doSomething with msg
...
}

this is working fine.

However I have a performance issue. Sometimes messages[] could be big. Some folders contain more than 1000 messages, and checking them all takes time.

I am looking for a quicker way to get only those emails that are the most recent (for example messages from the last 5 days or something like that)

of course I have the date information in each msg and I could do my comparison, but this is not efficient since it will loop through the entire collection.

Is there a better way to get those messages?

Upvotes: 0

Views: 2481

Answers (1)

dkarp
dkarp

Reputation: 14763

If you have JavaMail issue a SEARCH command with the criterion SINCE 04-JAN-2011, you'll get back the set messages in the currently-selected folder delivered since January 4th. (SENTSINCE 04-JAN-2011 will do the same thing, only based on the "Date" message header.)

Something along the lines of this:

folder.search(new ReceivedDateTerm(ComparisonTerm.GE, sinceDate));

Upvotes: 1

Related Questions