Faster Mailbox Reading in Java

I would like to get a list of everyone who has ever been included in any message in my inbox. Right now I can use the javax mail API to connect via IMAP and download 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(); } 

Linear messages [i] .getFrom () are slower than I would like, because this leads to lazy loading of the message. Can I do something to speed it up? For instance. Is there some kind of bulk upload that I can do instead of downloading messages one by one? Does this load the entire message, and is there something I can do to only load fields / headers / cc / cc or headers? Will POP be faster than IMAP?

+6
source share
2 answers

You can use the fetch method in the folder. According to Javadocs:

Customers use this method to indicate that these items are enthusiastic in a given message range. The implementation expects these items to be displayed for a given range of messages in an efficient manner. Please note that this method is only a hint for pre-fetching the desired items.

For FROM fetching, the corresponding FetchProfile is ENVELOPE. Of course, it still depends on the implementation, and the mail server really helps.

+5
source

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 be a prefetch of the "envelope" for all messages, which includes the from / to / subject / cc fields.

+6
source

Source: https://habr.com/ru/post/914001/


All Articles