简体   繁体   中英

How to get mails from Gmail server using multithreading?

I have a problem with getting e-mails from Gmail server using javax.mail API. I have to fetch 5 mails at a time by using 5 threads. Please help me.

folder = (IMAPFolder) store.getFolder(m_StrfolderName);
folder.open(Folder.READ_WRITE);
Message []messages = folder.getMessages(); 

Instead of doing this:

Message[] messages = folder.getMessages(); 

You could (in theory) do this:

final int count = folder.getMessageCount();
// in multiple threads
for (int i /* in a subset of [0 .. count - 1] */) {
    Message message = folder.getMessage(i);
    // process it
}

However, I don't think this is going to fetch messages in parallel. The problem is that when getMessage(int) is talking to the IMAP server, it holds a local lock (the cache lock) on the folder. This effectively means that messages will be fetched one at a time.

I guess you could attempt to open multiple IMAP sessions to your mailbox, but I suspect that the remote IMAP server (gmail) won't let you do that.

But here's a question for you. Do actually need to fetch the email messages in parallel, or would processing them in parallel be sufficient? (Where is the bottleneck in your code? Fetching or processing?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM