Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Faster reading of inbox in Java

I'd like to get a list of everyone who's ever been included on any message in my inbox. Right now I can use the javax mail API to connect via IMAP and download the messages:

Folder folder = imapSslStore.getFolder("[Gmail]/All Mail");
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();

for(int i = 0; i < messages.length; i++) {
  // This causes the message to be lazily loaded and is slow
  String[] from = messages[i].getFrom();
}

The line messages[i].getFrom() is slower than I'd like because is causes the message to be lazily loaded. Is there anything I can do to speed this up? E.g. is there some kind of bulk loading I can do instead of loading the messages one-by-one? Does this load the whole message and is there something I can do to only load the to/from/cc fields or headers instead? Would POP be any faster than IMAP?

like image 893
Ben McCann Avatar asked Apr 24 '12 04:04

Ben McCann


1 Answers

You want to add the following before the for loop

FetchProfile fetchProfile = new FetchProfile();
fetchProfile.add(FetchProfile.Item.ENVELOPE);
folder.fetch(messages, fetchProfile);

This will prefetch the "envelope" for all the messages, which includes the from/to/subject/cc fields.

like image 114
RecursivelyIronic Avatar answered Oct 04 '22 01:10

RecursivelyIronic