Reading from javamail takes a long time

后端 未结 4 1970
孤城傲影
孤城傲影 2021-02-15 15:57

I use javamail to read mails from an exchage account using IMAP protocol. Those mails are in plain format and its contents are XMLs.

Almost all those mails have short

4条回答
  •  春和景丽
    2021-02-15 16:41

    I finally solved this issue and wanted to share.

    The solution, at least the one that worked to me, was found in this site: http://www.oracle.com/technetwork/java/faq-135477.html#imapserverbug

    So, my original code typed in my first post becomes to this:

    Session sesion = Session.getInstance(System.getProperties());
    Store store = sesion.getStore("imap");
    store.connect(host, user, passwd);
    Folder inbox = store.getFolder("INBOX");
    inbox.open(Folder.READ_WRITE);
    
    // Convert to MimeMessage after search 
    MimeMessage[] messages = (MimeMessage[]) carpetaInbox.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
    for (int i = 0 ; i< messages.length ; i++){
        // Create a new message using MimeMessage copy constructor
        MimeMessage cmsg = new MimeMessage(messages[i]);
        // Use this message to read its contents 
        Object obj = cmsg.getContent(); 
    // ....
    }
    

    The trick is, using MimeMessage() copy constructor, create a new MimeMessage and read its contents instead of original message. You should note that such object is not really connected to server, so any changes you make on it, like setting flags, won't take effect. Any change on message, have to be done on original message.

    To sum up: This solution works reading large Plain Text mails (up to 15Mb) connecting to an Exchange Server using IMAP protocol. The times lowered from 51-55min to read a 13Mb mail, to 9seconds to read same mail. Unbelievable.

    Hope this helps someone and sorry for English mistakes ;)

提交回复
热议问题