我目前正在尝试使用JavaMail从IMAP服务器(包括Gmail和其他邮件)获取电子邮件。基本上,我的代码是可用的:我确实可以获取邮件的头部、正文内容等等。我的问题是:在没有SSL的IMAP服务器上操作时,处理一条信息基本上需要1-2ms。而当我使用IMAPS服务器(例如Gmail)时,我会达到大约250毫秒/消息。我仅在处理邮件时测量时间(连接、握手等不计入时间)。
我知道,由于这是SSL,数据是加密的。但解密所需的时间应该不会那么重要,是吗?
我尝试设置更高的ServerCacheSize值、更高的连接池大小,但现在已经没有更多的想法了。有人遇到过这个问题吗?或许解决了呢?
我的担忧是,JavaMail API每次从IMAPS服务器获取电子邮件时都会使用一个不同的连接(涉及握手的开销……)。如果是这样,是否有办法覆盖这种行为?
这是我的代码(虽然相当标准),从Main()类中调用:
public static int connectTest(String SSL, String user, String pwd, String host) throws IOException,
ProtocolException,
GeneralSecurityException {
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", SSL);
props.setProperty("mail.imaps.ssl.trust", host);
props.setProperty("mail.imaps.connectionpoolsize", "10");
try {
Session session = Session.getDefaultInstance(props, null);
// session.setDebug(true);
Store store = session.getStore(SSL);
store.connect(host, user, pwd);
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
int numMess = inbox.getMessageCount();
Message[] messages = inbox.getMessages();
for (Message m : messages) {
m.getAllHeaders();
m.getContent();
}
inbox.close(false);
store.close();
return numMess;
} catch (MessagingException e) {
e.printStackTrace();
System.exit(2);
}
return 0;
}
提前致谢。