Search code examples
javaemailjakarta-mailimap

Reading mail form IMAP server using javax.mail


I'm trying to read messages from IMAP folders using javax.mail, but after fetchig few messages Im getting following exception:

* 20 FETCH ()
DEBUG IMAPS: ignoring bad response, THROW: 
com.sun.mail.iap.ParsingException: error in FETCH parsing, unrecognized item at index 12, starts with ")
    at com.sun.mail.imap.protocol.FetchResponse.parse(FetchResponse.java:219)
    at com.sun.mail.imap.protocol.FetchResponse.<init>(FetchResponse.java:96)
    at com.sun.mail.imap.protocol.IMAPProtocol.readResponse(IMAPProtocol.java:392)
    at com.sun.mail.iap.Protocol.command(Protocol.java:354)
    at com.sun.mail.imap.protocol.IMAPProtocol.fetch(IMAPProtocol.java:2113)
    at com.sun.mail.imap.protocol.IMAPProtocol.fetch(IMAPProtocol.java:2094)
    at com.sun.mail.imap.IMAPFolder.fetch(IMAPFolder.java:1253)
    at app.mail.imap.ImapClient.synchronizeLocalData(ImapClient.java:563)
    at app.mail.imap.ImapClient.lambda$6(ImapClient.java:351)
    at java.lang.Thread.run(Thread.java:745)
* BYE Internal error occurred. Refer to server log for more information. [2016-11-03 21:20:44]

This is the code Im using to read messages (session, store and folder are already opened):

final Message[] messages = imapFolder.getMessagesByUID(1, IMAPFolder.LASTUID);
final FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.FLAGS);
imapFolder.fetch(messages, fp);

for (final Message msg : messages)
{
    final long uid = imapFolder.getUID(msg);
    final String subject = MimeUtility.decodeText(msg.getSubject());
    final Date date = msg.getReceivedDate();
    info("MESSAGE: {} -> {} on {}", uid, subject, date);
}

It seems it's happening only on very specific messages and only when fetching more informations than message UID (if Im reading only uid then code works).

As far as I know many IMAP servers implementations lack many features from IMAP protocol. Bearing that in mind, what is safest and most reliable method for fetching and reading messages from IMAP folders?

javax.mail version: 1.5.3 (provided by wildfly app server 10.0.1.Final).


Solution

  • Thanks for your help. It seems it is indeed IMAP server error. When I'm opening message in web client i get:

    Connection error (#022).
    

    Also KMail do not download messages for that folder (I bet other clients too).