开发者

Javamail performance

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 e开发者_C百科nvelop not even contents) it takes around 1 to 2 seconds for each message. What are the techniques should be used for fast retrieval?

here is my code:

    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();
    }


}


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.


Please set the Property mail.imap.fetchsize with the required size. the default is 16k. In case you increase the size of this property, retrieve speed will go up.

props.put("mail.imap.fetchsize", "3000000");

Note that if you're using the "imaps" protocol to access IMAP over SSL, all the properties would be named "mail.imaps.*".

Good Luck.

Yaniv


I'm not sure if this is a Javamail issue as much as it may be a Gmail issue. I have an application that retrieves mail from a number of sources, including Gmail, and Gmail is definitely the slowest. The Javamail api is pretty straightforward, but it would be hard to make suggestions without seeing what you are currently doing.


I'm running into the same thing. After profiling, I noticed that getBody was being called every time I tried to do a message.getFrom() like you are, even though I was only accessing fields that should be covered by the Envelope flag. See https://java.net/projects/javamail/forums/forum/topics/107956-gimap-efficiency-when-only-reading-headers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜