简体   繁体   English

解析通过imap收到的gmail消息时出现javamail-1.4.5错误

[英]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). 我正在使用javamail-1.4.5从gmail(imap)获取消息。 If Content-Disposition has an unquoted parameters, method getDisposition fails. 如果Content-Disposition具有未引用的参数,则方法getDisposition失败。

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. UPD1:这是我的代码的一部分。 I'm getting error in method handlePart, line 1 我在方法handlePart的第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"; 您可以通过将系统属性“ mail.mime.parameters.strict”设置为“ false”来解决此错误。 see the javadocs for the javax.mail.internet package and the ParameterList class. 有关javax.mail.internet包ParameterList类的信息,请参见javadocs

Also, you might want to upgrade to the current 1.5.2 version of JavaMail . 另外,您可能需要升级到JavaMail的当前1.5.2版本

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. 以我的经验,Content-Disposition所获得的不仅仅是错误。 I've written at least a dozen workarounds (not for javamail), each with unit tests. 我至少写了十二种解决方法(不适用于javamail),每种方法都带有单元测试。 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: 由于必须为未指定的CD提供适当的备用,因此您也可以利用该备用来进行错误和荒谬的处理:

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. 顺便说一句:使用“内容类型:文本/纯文本; utf8”向您发送一条消息,并检查您是否也处理了该解析异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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