JavaMail:BODY STRUCTURE.parse参数中的空指针异常。这是一个错误吗?

时间:2012-11-06 12:52:11

标签: nullpointerexception javamail

我正在尝试从IMAP帐户使用JavaMail 1.4.5获取消息,并在 BODYSTRUCTURE.parseParameters 方法中获得空指针异常。

查看parseParameters代码,我找到了这一行

list.set(null, "DONE"); // XXX - hack

问题是set方法试图将 .toLowerCase()调用为null值!!!

它试图解析的响应是:

* 1 FETCH (BODYSTRUCTURE (("TEXT" "PLAIN" ("CHARSET" "us-ascii") NIL NIL "7BIT" 55 4 NIL NIL NIL NIL)(("TEXT" "HTML" ("CHARSET" "us-ascii") NIL NIL "7BIT" 410 10 NIL NIL NIL NIL)("IMAGE" "JPEG" ("NAME" "image.jpg") "<53498286-6B3E-4AC8-8CA0-481152C80968@xxxx.it>" NIL "BASE64" 536628 NIL ("inline" ("FILENAME" "image.jpg")) NIL NIL) "RELATED" ("TYPE" "text/html" "BOUNDARY" "Apple-Mail=_56FA3EC6-FB02-4882-A1C5-487652E3B4E5") NIL NIL NIL) "ALTERNATIVE" ("BOUNDARY" "Apple-Mail=_CB164992-2501-4351-94D1-61CE7C8D90DC") NIL NIL NIL))

并启用调试,我收到了这些消息:

DEBUG IMAP: parsing BODYSTRUCTURE 
DEBUG IMAP: msgno 1 
DEBUG IMAP: parsing multipart 
DEBUG IMAP: parsing BODYSTRUCTURE 
DEBUG IMAP: msgno    1 
DEBUG IMAP: single part 
DEBUG IMAP: type TEXT 
DEBUG IMAP: subtype    PLAIN 
DEBUG IMAP: parameter name CHARSET 
DEBUG IMAP: parameter value    us-ascii

然后是NullPointerException

Exception in thread "main" java.lang.NullPointerException
at javax.mail.internet.ParameterList.set(ParameterList.java:165)
at com.sun.mail.imap.protocol.BODYSTRUCTURE.parseParameters(BODYSTRUCTURE.java:404)
at com.sun.mail.imap.protocol.BODYSTRUCTURE.<init>(BODYSTRUCTURE.java:224)
at com.sun.mail.imap.protocol.BODYSTRUCTURE.<init>(BODYSTRUCTURE.java:109)
at com.sun.mail.imap.protocol.FetchResponse.parse(FetchResponse.java:158)
at com.sun.mail.imap.protocol.FetchResponse.<init>(FetchResponse.java:67)
at com.sun.mail.imap.protocol.IMAPResponse.readResponse(IMAPResponse.java:136)
at com.sun.mail.imap.protocol.IMAPProtocol.readResponse(IMAPProtocol.java:270)
at com.sun.mail.iap.Protocol.command(Protocol.java:313)
at com.sun.mail.imap.protocol.IMAPProtocol.fetch(IMAPProtocol.java:1529)
at com.sun.mail.imap.protocol.IMAPProtocol.fetch(IMAPProtocol.java:1521)
at com.sun.mail.imap.protocol.IMAPProtocol.fetchBodyStructure(IMAPProtocol.java:1221)
at com.sun.mail.imap.IMAPMessage.loadBODYSTRUCTURE(IMAPMessage.java:1307)
at com.sun.mail.imap.IMAPMessage.getDataHandler(IMAPMessage.java:623)
at javax.mail.internet.MimeMessage.getContent(MimeMessage.java:927

感谢任何可以帮助我的人!

6 个答案:

答案 0 :(得分:12)

我终于找到了问题的原因。

我在我的项目中包含了Apache Cxf。

Cxf包含对geronimo-javamail_1.4_spec的引用,它覆盖了一些javamail类。

排除对geronimo的引用,一切正常!

答案 1 :(得分:11)

您可能从两个不同版本的JavaMail中混合使用了JavaMail类。检查你的类路径以获取javax.mail。*类的其他实例,可能是在j2ee.jar或javaee.jar中。

答案 2 :(得分:0)

我有类似(可能相同)的问题。

此问题与此Oracle FAQ here中描述的邮件服务器错误有关。

解决方案是:

  • 修复您的IMAP服务器中的错误
  • 或使用Oracle FAQ中描述的解决方法

    // Get the message object from the folder in the
    // usual way, for example:
    MimeMessage msg = (MimeMessage)folder.getMessage(n);
    
    // Use the MimeMessage copy constructor to make a copy
    // of the entire message, which will fetch the entire
    // message from the server and parse it on the client:
    MimeMessage cmsg = new MimeMessage(msg);
    
    // The cmsg object is disconnected from the server so
    // setFlags will have no effect (for example).  Use
    // the original msg object for such operations.  Use
    // the cmsg object to access the content of the message.
    

// Get the message object from the folder in the
// usual way, for example:
MimeMessage msg = (MimeMessage)folder.getMessage(n);

// Copy the message by writing into an byte array and
// creating a new MimeMessage object based on the contents
// of the byte array:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
msg.writeTo(bos);
bos.close();
SharedByteArrayInputStream bis =
        new SharedByteArrayInputStream(bos.toByteArray());
MimeMessage cmsg = new MimeMessage(session, bis);
bis.close();

// The cmsg object is disconnected from the server so
// setFlags will have no effect (for example).  Use
// the original msg object for such operations.  Use
// the cmsg object to access the content of the message.

我感谢这个Oracle论坛thread

答案 3 :(得分:0)

感谢您给我这个问题的建议

我在项目中添加以下依赖项,一切正常

class UsersController < ApplicationController

    before_action :check_user

    private

    def check_user
      if current_user != @user
        redirect_to root_url, alert: "Sorry, This Profile belongs to someone else !"
      end
    end
end

答案 4 :(得分:0)

我在一台Mac电脑上遇到了同样的问题。我尝试在另一台Mac中进行设置。它像魅力一样运作。 我已经清理了所有的詹金斯,然后在有问题的机器(使用与另一台机器相同的战争)中再次使用war来启动实例。两台机器上具有相同的Java版本(1.8.0_211)。我没有在这里引入任何项目,只是尝试检查轮询邮箱触发器是否与虚拟项目一起正常工作。我犯了同样的错误。我不知道类路径的区别在哪里,因为我没有单独配置任何东西。任何指针在这里都会有所帮助。

答案 5 :(得分:0)

非常感谢 Stefano Bertini 和 Bill Shannon 的回答!

就我而言,它是从 cxf-rt-frontend-jaxws 中排除的:

<!-- CXF -->
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>${cxf.version}</version>
    <exclusions>
        <exclusion>
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-javamail_1.4_spec</artifactId>
        </exclusion>
    </exclusions>
</dependency>