Faster reading of inbox in Java
Asked Answered
F

2

6

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?

Foxhound answered 24/4, 2012 at 4:21 Comment(0)
K
5

You can use fetch-method in Folder. According Javadocs:

Clients use this method to indicate that the specified items are needed en-masse for the given message range. Implementations are expected to retrieve these items for the given message range in a efficient manner. Note that this method is just a hint to the implementation to prefetch the desired items.

For fetching FROM appropriate FetchProfile is ENVELOPE. Of course it is still up to implementation and mail server does that really help.

Koziarz answered 24/4, 2012 at 4:38 Comment(0)
R
6

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.

Realty answered 17/10, 2012 at 0:25 Comment(0)
K
5

You can use fetch-method in Folder. According Javadocs:

Clients use this method to indicate that the specified items are needed en-masse for the given message range. Implementations are expected to retrieve these items for the given message range in a efficient manner. Note that this method is just a hint to the implementation to prefetch the desired items.

For fetching FROM appropriate FetchProfile is ENVELOPE. Of course it is still up to implementation and mail server does that really help.

Koziarz answered 24/4, 2012 at 4:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.