views:

154

answers:

1

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


}
+1  A: 

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.

Ryan Elkins