繁体   English   中英

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

[英]javamail-1.4.5 error at parsing gmail message received via imap

我正在使用javamail-1.4.5从gmail(imap)获取消息。 如果Content-Disposition具有未引用的参数,则方法getDisposition失败。

消息部分:

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

例外:

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:这是我的代码的一部分。 我在方法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);
    }
}

消息格式错误。 哪个程序创建了消息? 请将此错误报告给该程序的所有者。

您可以通过将系统属性“ mail.mime.parameters.strict”设置为“ false”来解决此错误。 有关javax.mail.internet包ParameterList类的信息,请参见javadocs

另外,您可能需要升级到JavaMail的当前1.5.2版本

它失败,因为存在语法错误。 缺乏报价是非法的。 对于异常,您无能为力,只需提交补丁,而围绕内容处置和内容类型错误进行补丁就永无休止。 以我的经验,Content-Disposition所获得的不仅仅是错误。 我至少写了十二种解决方法(不适用于javamail),每种方法都带有单元测试。 那是艰苦的工作,可能不值得。

由于必须为未指定的CD提供适当的备用,因此您也可以利用该备用来进行错误和荒谬的处理:

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

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

暂无
暂无

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

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