基于SSL的JavaMail IMAP非常慢 – 批量获取多条消息

我目前正在尝试使用JavaMail从IMAP服务器(Gmail和其他人)获取电子邮件。 基本上,我的代码工作:我确实可以得到标题,正文内容等。 我的问题如下:当处理IMAP服务器(没有SSL)时,处理消息基本上需要1-2ms。 当我使用IMAPS服务器(因此使用SSL,例如Gmail)时,我的消息达到250米左右。 我只测量处理消息的时间(不考虑连接,握手等)。

我知道因为这是SSL,所以数据是加密的。 但是,解密的时间应该不那么重要,是吗?

我已经尝试设置更高的ServerCacheSize值,更高的connectionpoolsize,但我的想法很严重。 谁有人遇到这个问题? 解决了一个人可能希望?

我担心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; } 

提前致谢。

经过大量工作和JavaMail人员的帮助后,这种“缓慢”的来源来自API中的FETCH行为。 实际上,正如pjaol所说,每当我们需要消息的信息(标题或消息内容)时,我们就会返回服务器。

如果FetchProfile允许我们批量获取许多消息的头信息或标志,则无法直接获取多个消息的内容。

幸运的是,我们可以编写自己的IMAP命令来避免这种“限制”(这样做是为了避免内存不足错误:在一个命令中获取内存中的每个邮件都可能非常繁重)。

这是我的代码:

 import com.sun.mail.iap.Argument; import com.sun.mail.iap.ProtocolException; import com.sun.mail.iap.Response; import com.sun.mail.imap.IMAPFolder; import com.sun.mail.imap.protocol.BODY; import com.sun.mail.imap.protocol.FetchResponse; import com.sun.mail.imap.protocol.IMAPProtocol; import com.sun.mail.imap.protocol.UID; public class CustomProtocolCommand implements IMAPFolder.ProtocolCommand { /** Index on server of first mail to fetch **/ int start; /** Index on server of last mail to fetch **/ int end; public CustomProtocolCommand(int start, int end) { this.start = start; this.end = end; } @Override public Object doCommand(IMAPProtocol protocol) throws ProtocolException { Argument args = new Argument(); args.writeString(Integer.toString(start) + ":" + Integer.toString(end)); args.writeString("BODY[]"); Response[] r = protocol.command("FETCH", args); Response response = r[r.length - 1]; if (response.isOK()) { Properties props = new Properties(); props.setProperty("mail.store.protocol", "imap"); props.setProperty("mail.mime.base64.ignoreerrors", "true"); props.setProperty("mail.imap.partialfetch", "false"); props.setProperty("mail.imaps.partialfetch", "false"); Session session = Session.getInstance(props, null); FetchResponse fetch; BODY body; MimeMessage mm; ByteArrayInputStream is = null; // last response is only result summary: not contents for (int i = 0; i < r.length - 1; i++) { if (r[i] instanceof IMAPResponse) { fetch = (FetchResponse) r[i]; body = (BODY) fetch.getItem(0); is = body.getByteArrayInputStream(); try { mm = new MimeMessage(session, is); Contents.getContents(mm, i); } catch (MessagingException e) { e.printStackTrace(); } } } } // dispatch remaining untagged responses protocol.notifyResponseHandlers(r); protocol.handleResult(response); return "" + (r.length - 1); } } 

getContents(MimeMessage mm,int i)函数是一个经典函数,它以递归方式将消息内容打印到文件中(网上提供了许多示例)。

为了避免内存不足错误,我只需设置一个maxDocs和maxSize限制(这是任意完成的,可以改进!)使用如下:

 public int efficientGetContents(IMAPFolder inbox, Message[] messages) throws MessagingException { FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.FLAGS); fp.add(FetchProfile.Item.ENVELOPE); inbox.fetch(messages, fp); int index = 0; int nbMessages = messages.length; final int maxDoc = 5000; final long maxSize = 100000000; // 100Mo // Message numbers limit to fetch int start; int end; while (index < nbMessages) { start = messages[index].getMessageNumber(); int docs = 0; int totalSize = 0; boolean noskip = true; // There are no jumps in the message numbers // list boolean notend = true; // Until we reach one of the limits while (docs < maxDoc && totalSize < maxSize && noskip && notend) { docs++; totalSize += messages[index].getSize(); index++; if (notend = (index < nbMessages)) { noskip = (messages[index - 1].getMessageNumber() + 1 == messages[index] .getMessageNumber()); } } end = messages[index - 1].getMessageNumber(); inbox.doCommand(new CustomProtocolCommand(start, end)); System.out.println("Fetching contents for " + start + ":" + end); System.out.println("Size fetched = " + (totalSize / 1000000) + " Mo"); } return nbMessages; } 

不要在这里我使用的是不稳定的消息号码(如果消息从服务器中删除,则会发生这些更改)。 一个更好的方法是使用UID! 然后,您将命令从FETCH更改为UID FETCH。

希望这会有所帮助!

在迭代消息之前,您需要将FetchProfile添加到收件箱。 消息是一个延迟加载对象,它将为每个消息以及未提供默认配置文件的每个字段返回服务器。 例如

 for (Message message: messages) { message.getSubject(); //-> goes to the imap server to fetch the subject line } 

如果你想显示像收件箱列表,只说发件人,主题,发送,附件等..你会使用如下的东西

  inbox.open(Folder.READ_ONLY); Message[] messages = inbox.getMessages(start + 1, total); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); fp.add(FetchProfileItem.FLAGS); fp.add(FetchProfileItem.CONTENT_INFO); fp.add("X-mailer"); inbox.fetch(messages, fp); // Load the profile of the messages in 1 fetch. for (Message message: messages) { message.getSubject(); //Subject is already local, no additional fetch required } 

希望有所帮助。

总时间包括加密操作所需的时间。 加密操作需要随机播种机。 存在不同的随机种子实现,其提供用于密码术的随机比特。 默认情况下,Java使用/ dev / urandom ,这在java.security中指定,如下所示:

 securerandom.source=file:/dev/urandom 

在Windows上,java使用Microsoft CryptoAPI种子function,通常没有问题。 但是,在unix和linux上,Java默认使用/ dev / random进行随机播种。 并且/ dev / random上的读取操作有时会阻塞并需要很长时间才能完成。 如果您使用的是* nix平台,那么花在此上的时间将计入总时间。

既然,我不知道你正在使用什么平台,我不能肯定地说这可能是你的问题。 但如果你是,那么这可能是你的运营需要很长时间的原因之一。 其中一个解决方案可能是使用/ dev / urandom而不是/ dev / random作为随机播种器,它不会阻塞。 这可以使用系统属性“java.security.egd”指定。 例如,

  -Djava.security.egd=file:/dev/urandom 

指定此系统属性将覆盖java.security文件中的securerandom.source设置。 你可以尝试一下。 希望能帮助到你。