简体   繁体   中英

javamail-1.4.5 error at parsing gmail message received via imap

I'm using javamail-1.4.5 for getting messages from gmail (imap). If Content-Disposition has an unquoted parameters, method getDisposition fails.

message part:

Content-Transfer-Encoding: 7bit
Content-Type: message/rfc822
Content-Disposition: attachment;
    creation-date=Wed, 11 Feb 2015 10:23:48 GMT;
    modification-date=Wed, 11 Feb 2015 10:23:48 GMT

exception:

javax.mail.internet.ParseException: Expected ';', got ","
        at javax.mail.internet.ParameterList.<init>(ParameterList.java:289)
        at javax.mail.internet.ContentDisposition.<init>(ContentDisposition.java:100)
        at javax.mail.internet.MimeBodyPart.getDisposition(MimeBodyPart.java:1076)

UPD1: this is a part of my code. I'm getting error in method handlePart, line 1

private void handleMessage(Message message) {
    Object content = message.getContent();
    if(content instanceof Multipart) {
        handleMultipart((Multipart) content);
    }
    else {
        handlePart(message);
    }
}

private void handleMultipart(Multipart mp) {
    for(int i = 0; i < mp.getCount(); i++) {
        Part part = mp.getBodyPart(i);
        Object content = part.getContent();
        if(content instanceof Multipart) {
            handleMultipart((Multipart) content);
        }
        else {
            handlePart(part);
        }
    }
}

private void handlePart(Part part) {
    String disposition = part.getDisposition(); //GETTING ERROR
    String contentType = part.getContentType();
    if(disposition == null) {
        if(contentType.toLowerCase().startsWith("text/html")) {
            html = (String) part.getContent();
        }
        else if(contentType.toLowerCase().startsWith("text/plain")) {
            text = (String) part.getContent();
        }
        else {
            handleAttachment(part);
        }
    }
    else if(disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
        handleAttachment(part);
    }
    else if(disposition.equalsIgnoreCase(Part.INLINE)) {
        handleAttachment(part);
    }
}

The message is incorrectly formatted. What program created the message? Please report this bug to the owner of that program.

You can work around this bug by setting the System property "mail.mime.parameters.strict" to "false"; see the javadocs for the javax.mail.internet package and the ParameterList class.

Also, you might want to upgrade to the current 1.5.2 version of JavaMail .

It fails because there's a syntax error. The lack of quoting is illegal. There's not much you can do about the exception, short of submitting a patch, and patching around content-disposition and content-type errors is neverending work. In my experience, Content-Disposition gets more than its fair share of errors. I've written at least a dozen workarounds (not for javamail), each with unit tests. That's hard work and may not be worth it.

Since you have to have a decent fallback for unspecified CD, you can leverage that fallback for errant and nonsensical dispositions too:

String disposition = null;
try {
    disposition = part.getDisposition();
} catch(ParseException x) {
    // treat Content-Disposition as unspecified if it cannot be parsed
    disposition = null;
}

BTW: Send yourself a message with "Content-type: text/plain; utf8", and check that you handle that parse exception too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM