Javamail 性能

Javamail performance(Javamail 性能)

本文介绍了Javamail 性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been using javamail to retrieve mails from IMAP server (currently GMail). Javamail retrieves list of messages (only ids) in a particular folder from server very fast, but when I actually fetch message (only envelop not even contents) it takes around 1 to 2 seconds for each message. What are the techniques should be used for fast retrieval?

这是我的代码:

    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 连接来加快速度.

I believe that Gmail throttles the IMAP message reads to one every second or so. You might be able to speed it up with multiple IMAP connections.

这篇关于Javamail 性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Javamail 性能

基础教程推荐