Javamail性能

我一直在使用javamail从IMAP服务器(目前是GMail)检索邮件。 Javamail非常快速地从服务器检索特定文件夹中的消息列表(仅ID),但是当我实际获取消息(仅包含信息甚至不包含内容)时,每条消息大约需要1到2秒。 应该使用哪些技术进行快速检索?

这是我的代码:

try { IMAPStore store = null; if(store!=null&&store.isConnected())return; Properties props = System.getProperties(); Session sessionIMAP = Session.getInstance(props, null); try { store = (IMAPStore) sessionIMAP.getStore("imaps"); store.connect("imap.gmail.com",993,"username@gmail.com","password"); } catch (Exception e) { e.printStackTrace(); } IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX"); folder.open(Folder.READ_ONLY); System.out.println("start"); Message[] msgs = folder.getMessages(1,10); long ftime = System.currentTimeMillis(); FetchProfile fp=new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); folder.fetch(msgs, fp); long time = System.currentTimeMillis(); System.out.println("fetch: "+(time-ftime)); for (Message message : msgs) { System.out.println(message.getSubject()); Address[] from = message.getFrom(); for (Address address : from) { System.out.println(address); } Address[] recipients = message.getAllRecipients(); for (Address address : recipients) { System.out.println(address); } } long newTime = System.currentTimeMillis(); System.out.println("convert: "+(newTime-time)); }catch (Exception e) { e.printStackTrace(); } } 

我相信Gmail会每隔一秒左右限制IMAP邮件的读取速度。 您可以通过多个IMAP连接加快速度。

我不确定这是否是Javamail问题,因为它可能是Gmail问题。 我有一个应用程序可以检索来自多个来源的邮件,包括Gmail,而Gmail绝对是最慢的。 Javamail api非常简单,但如果不了解您当前正在做什么就很难提出建议。

我遇到了同样的事情。 在分析之后,我注意到每次我尝试做一个message.getFrom()时都会调用getBody,即使我只是访问应该被Envelope标志覆盖的字段。 请参阅https://java.net/projects/javamail/forums/forum/topics/107956-gimap-efficiency-when-only-reading-headers