我正在尝试使用 JavaMail 1.4.5 从 IMAP 帐户获取消息,但在BODYSTRUCTURE.parseParameters方法中出现空指针异常。
查看 parseParameters 代码,我找到了这一行
list.set(null, "DONE"); // XXX - hack
问题是 set 方法试图将.toLowerCase()调用为空值!!!
它试图解析的响应是这样的:
* 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
感谢任何可以帮助我的人!