Javamail performance
Asked Answered
M

4

4

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?

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,"[email protected]","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();
    }


}
Marna answered 29/3, 2010 at 14:10 Comment(0)
C
5

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.

Cementum answered 29/3, 2010 at 14:10 Comment(0)
M
3

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

Margarettemargarida answered 29/3, 2010 at 14:10 Comment(0)
L
2

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.

Laughlin answered 29/3, 2010 at 14:14 Comment(0)
S
1

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

Skullcap answered 29/3, 2010 at 14:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.