SSL上的JavaMail IMAP非常慢 - 批量获取多条消息

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

我知道因为这是 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 允许我们批量获取多条消息的 header 信息或标志,则无法直接获取多条消息的内容。

幸运的是,我们可以编写自己的 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。

希望这会有所帮助!

 原文地址:https://www.coder.work/article/73556

关于SSL上的JavaMail IMAP非常慢 - 批量获取多条消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8322836/

 
posted @ 2025-05-27 08:49  译林  阅读(62)  评论(0)    收藏  举报