目前手头在做一个采用邮件回复的方式对工作流进行审批的项目。之前一篇博文对思路做了简单描述。项目开发完成在本地进行测试的时候完成没有问题。但在进入待发布环境交测试组测试时,却一直报错。远程调试发现如下代码报空指针异常:

  try{
	        IMAPStore store = (IMAPStore) session.getStore("imap"); // 使用imap会话机制,连接服务器  
	        store.connect(user, password);  
	        IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX"); // 收件箱  error
	        folder.open(Folder.READ_WRITE);

   标记error的行报空指针,打印异常栈:

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)
at a.Email3.main(Email3.java:71)

   反复远程和本地调试,未发现问题。

   网上对相关错误进行搜索,发现有几例相似:

tomcat使用javamail发送邮件失败解决办法

javamail报空指针异常

javaMail在Tomcat下收取邮件时报空指针错误;Jetty和mian运行正常

    针对几种思路,包括缺失jar包和代码不严谨或lib包冲突均作了尝试,未有效解决

    因怀疑lib包冲突可能性更大,在本地main进行测试,正常通过。将tomcat下所有lib全部导入本地test依赖,发现报错,巡查lib包,发现有geronimo-javamail_1.4_spec-1.3.jar包,嫌疑较大。删除该包,代码正常。由此,基本可判定系该包与javamail发生冲突,可能有同名class被先加载。

网上有对该冲突的解决方法的描述:

geronimo-javamail_1.4_spec-1.3.jar与mail.jar 冲突解决办法

   方法简单明了,即删除冲突的jar包。因本项目的管理使用maven,如不将根本问题解决,只删除jar包是徒劳的。所以以下尝试从根本上将依赖冲突解决。

   IDE中pom.xml全文检索该包,未发现有项目的直接依赖。因maven存在传递依赖,所以可判定是由项目的间接依赖导致。通过maven命令查看依赖树,方法如下:

        选中项目,右键->run as ,选择maven build ...,然后在对话框中的Goals项填入" dependency:tree",再在"debug output"打上勾,并将此项启动的名字改为"showDependency-tree",然后点击“Debug"/Run按钮, console里面会很详细地显示每个pom里面配置的依赖它们会带来哪些jar文件。

   因output巨长,下面仅作节选:

[INFO] com.fx.oa:per-evaluation:jar:0.0.1-SNAPSHOT
[INFO] +- com.fx.fxplatform:fxplatform-shared:jar:1.0.0-SNAPSHOT:compile
[INFO] |  +- org.springframework:spring-beans:jar:3.1.2.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-core:jar:3.1.2.RELEASE:compile
[INFO] |  +- org.springframework:spring-web:jar:3.1.2.RELEASE:compile
[INFO] |  |  +- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  |  \- org.springframework:spring-context:jar:3.1.2.RELEASE:compile
[INFO] |  |     \- org.springframework:spring-aop:jar:3.1.2.RELEASE:compile
[INFO] |  +- org.springframework:spring-webmvc:jar:3.1.2.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-asm:jar:3.1.2.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-context-support:jar:3.1.2.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-expression:jar:3.1.2.RELEASE:compile
[INFO] |  +- org.springframework:spring-test:jar:3.1.2.RELEASE:test (scope managed from compile)
[INFO] |  +- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.12:compile
[INFO] |  |  \- org.codehaus.jackson:jackson-core-asl:jar:1.9.12:compile
[INFO] |  +- ognl:ognl:jar:3.0.6:compile
[INFO] |  +- javassist:javassist:jar:3.12.1.GA:compile
[INFO] |  +- log4j:log4j:jar:1.2.16:compile
[INFO] |  +- org.slf4j:slf4j-log4j12:jar:1.6.1:compile
[INFO] |  +- org.slf4j:slf4j-api:jar:1.6.1:compile
[INFO] |  +- org.apache.poi:poi:jar:3.8:compile
[INFO] |  |  \- commons-codec:commons-codec:jar:1.5:compile
[INFO] |  +- org.apache.poi:poi-ooxml:jar:3.8:compile
[INFO] |  |  \- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  +- org.apache.poi:poi-ooxml-schemas:jar:3.8:compile
[INFO] |  |  \- org.apache.xmlbeans:xmlbeans:jar:2.3.0:compile
[INFO] |  |     \- stax:stax-api:jar:1.0.1:compile
[INFO] |  +- commons-beanutils:commons-beanutils:jar:1.8.3:compile
[INFO] |  |  \- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] |  +- com.thoughtworks.xstream:xstream:jar:1.3.1:compile
[INFO] |  |  \- xpp3:xpp3_min:jar:1.1.4c:compile
[INFO] |  +- org.jasig.cas.client:cas-client-core:jar:3.2.1:compile
[INFO] |  +- com.caucho:hessian:jar:4.0.33:compile
[INFO] |  +- net.sourceforge.pinyin4j:pinyin4j:jar:2.5.0:compile
[INFO] |  +- org.freemarker:freemarker:jar:2.3.20:compile
[INFO] |  +- commons-lang:commons-lang:jar:2.5:compile
[INFO] |  +- javax.mail:mail:jar:1.4.1:compile
[INFO] |  \- velocity:velocity:jar:1.5:compile
[INFO] |     +- commons-collections:commons-collections:jar:3.1:compile
[INFO] |     \- oro:oro:jar:2.0.8:compile
[INFO] +- com.fx.fxplatform:fxplatform-server:jar:1.0.0-SNAPSHOT:compile
[INFO] |  +- com.fx.fxplatform:fxplatform-cache:jar:1.0.0-SNAPSHOT:compile
[INFO] |  |  +- com.fx.fxplatform:fxplatform-redis:jar:1.0.0-SNAPSHOT:compile
[INFO] |  |  |  \- com.alibaba:fastjson:jar:1.1.23:compile
[INFO] |  |  +- org.springframework.data:spring-data-mongodb:jar:1.1.0.RELEASE:compile
[INFO] |  |  |  +- org.springframework.data:spring-data-commons-core:jar:1.4.0.RELEASE:compile
[INFO] |  |  |  \- org.mongodb:mongo-java-driver:jar:2.9.1:compile
[INFO] |  |  +- jdom:jdom:jar:1.0:compile
[INFO] |  |  \- cglib:cglib-nodep:jar:2.2:compile
[INFO] |  +- org.mybatis:mybatis:jar:3.0.5:compile
[INFO] |  +- org.mybatis:mybatis-spring:jar:1.0.1:compile
[INFO] |  |  +- org.springframework:spring-tx:jar:3.0.5.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-jdbc:jar:3.1.2.RELEASE:compile (version managed from 3.0.5.RELEASE)
[INFO] |  +- commons-fileupload:commons-fileupload:jar:1.2.1:compile
[INFO] |  +- javax.activation:activation:jar:1.1.1:compile
[INFO] |  +- org.apache.struts:struts2-core:jar:2.3.15.1:compile
[INFO] |  |  +- org.apache.struts.xwork:xwork-core:jar:2.3.15.1:compile
[INFO] |  |  |  +- asm:asm:jar:3.3:compile
[INFO] |  |  |  \- asm:asm-commons:jar:3.3:compile
[INFO] |  |  |     \- asm:asm-tree:jar:3.3:compile
[INFO] |  |  +- commons-io:commons-io:jar:2.0.1:compile
[INFO] |  |  \- com.sun:tools:jar:1.5.0:system
[INFO] |  +- org.apache.struts:struts2-spring-plugin:jar:2.3.15.1:compile
[INFO] |  |  \- org.apache.commons:commons-lang3:jar:3.1:compile
[INFO] |  +- org.apache.struts:struts2-convention-plugin:jar:2.3.15.1:compile
[INFO] |  +- org.apache.struts:struts2-json-plugin:jar:2.3.15.1:compile
[INFO] |  +- redis.clients:jedis:jar:2.1.0:compile
[INFO] |  |  \- commons-pool:commons-pool:jar:1.5.5:compile
[INFO] |  +- org.codehaus.groovy:groovy-all:jar:2.2.1:compile
[INFO] |  +- com.itextpdf:itextpdf:jar:5.4.5:compile
[INFO] |  +- com.itextpdf:itext-asian:jar:5.2.0:compile
[INFO] |  +- net.sf.json-lib:json-lib:jar:2.4:compile
[INFO] |  |  \- net.sf.ezmorph:ezmorph:jar:1.0.6:compile
[INFO] |  +- org.csource:org-csource:jar:1.24:compile
[INFO] |  +- org.quartz-scheduler:quartz:jar:1.8.6:compile
[INFO] |  +- org.springframework.data:spring-data-redis:jar:1.0.2.RELEASE:compile
[INFO] |  |  \- org.slf4j:jcl-over-slf4j:jar:1.6.6:compile
[INFO] |  \- org.apache.ant:ant:jar:1.7.0:compile
[INFO] |     \- org.apache.ant:ant-launcher:jar:1.7.0:compile
[INFO] +- com.fx.oa:sys-settings-api:jar:0.0.1-SNAPSHOT:compile
[INFO] |  \- com.fx.oa:oa-util:jar:0.0.1-SNAPSHOT:compile
[INFO] |     +- org.seleniumhq.selenium:selenium-java:jar:2.39.0:compile
[INFO] |     |  +- org.seleniumhq.selenium:selenium-android-driver:jar:2.39.0:compile
[INFO] |     |  |  \- org.seleniumhq.selenium:selenium-remote-driver:jar:2.39.0:compile
[INFO] |     |  |     +- org.json:json:jar:20080701:compile
[INFO] |     |  |     \- com.google.guava:guava:jar:15.0:compile
[INFO] |     |  +- org.seleniumhq.selenium:selenium-chrome-driver:jar:2.39.0:compile
[INFO] |     |  +- org.seleniumhq.selenium:selenium-htmlunit-driver:jar:2.39.0:compile
[INFO] |     |  |  +- net.sourceforge.htmlunit:htmlunit:jar:2.13:compile
[INFO] |     |  |  |  +- xalan:xalan:jar:2.7.1:compile
[INFO] |     |  |  |  |  \- xalan:serializer:jar:2.7.1:compile
[INFO] |     |  |  |  +- org.apache.httpcomponents:httpmime:jar:4.3.1:compile
[INFO] |     |  |  |  +- net.sourceforge.htmlunit:htmlunit-core-js:jar:2.13:compile
[INFO] |     |  |  |  +- xerces:xercesImpl:jar:2.11.0:compile
[INFO] |     |  |  |  +- net.sourceforge.nekohtml:nekohtml:jar:1.9.19:compile
[INFO] |     |  |  |  +- net.sourceforge.cssparser:cssparser:jar:0.9.11:compile
[INFO] |     |  |  |  |  \- org.w3c.css:sac:jar:1.3:compile
[INFO] |     |  |  |  \- org.eclipse.jetty:jetty-websocket:jar:8.1.12.v20130726:compile
[INFO] |     |  |  |     +- org.eclipse.jetty:jetty-util:jar:8.1.12.v20130726:compile
[INFO] |     |  |  |     +- org.eclipse.jetty:jetty-io:jar:8.1.12.v20130726:compile
[INFO] |     |  |  |     \- org.eclipse.jetty:jetty-http:jar:8.1.12.v20130726:compile
[INFO] |     |  |  \- org.apache.httpcomponents:httpclient:jar:4.3.1:compile
[INFO] |     |  |     \- org.apache.httpcomponents:httpcore:jar:4.3:compile
[INFO] |     |  +- org.seleniumhq.selenium:selenium-firefox-driver:jar:2.39.0:compile
[INFO] |     |  |  \- org.apache.commons:commons-exec:jar:1.1:compile
[INFO] |     |  +- org.seleniumhq.selenium:selenium-ie-driver:jar:2.39.0:compile
[INFO] |     |  |  +- net.java.dev.jna:jna:jar:3.4.0:compile
[INFO] |     |  |  \- net.java.dev.jna:platform:jar:3.4.0:compile
[INFO] |     |  +- org.seleniumhq.selenium:selenium-iphone-driver:jar:2.39.0:compile
[INFO] |     |  +- org.seleniumhq.selenium:selenium-safari-driver:jar:2.39.0:compile
[INFO] |     |  +- org.seleniumhq.selenium:selenium-support:jar:2.39.0:compile
[INFO] |     |  |  \- org.seleniumhq.selenium:selenium-api:jar:2.39.0:compile
[INFO] |     |  \- org.webbitserver:webbit:jar:0.4.14:compile
[INFO] |     |     \- io.netty:netty:jar:3.5.2.Final:compile
[INFO] |     \- xml-apis:xml-apis:jar:1.4.01:compile
[INFO] +- javax.servlet:servlet-api:jar:2.5:provided
[INFO] +- javax.servlet:jsp-api:jar:2.0:provided
[INFO] +- com.fx.oa:per-evaluation-api:jar:0.0.1-SNAPSHOT:compile
[INFO] |  \- com.fx.oa:com-dependence-api:jar:0.0.1-SNAPSHOT:compile
[INFO] |     +- com.fx.oa:oa-ldap:jar:0.0.1-SNAPSHOT:compile
[INFO] |     |  +- org.springframework:spring-aspects:jar:3.1.2.RELEASE:compile
[INFO] |     |  +- org.springframework:spring-instrument:jar:3.1.2.RELEASE:compile
[INFO] |     |  +- org.springframework.ldap:spring-ldap-core:jar:1.3.1.RELEASE:compile
[INFO] |     |  +- org.springframework.ldap:spring-ldap-core-tiger:jar:1.3.1.RELEASE:compile
[INFO] |     |  +- org.springframework.ldap:spring-ldap-odm:jar:1.3.1.RELEASE:compile
[INFO] |     |  |  \- commons-cli:commons-cli:jar:1.2:compile
[INFO] |     |  +- org.springframework.ldap:spring-ldap-ldif-core:jar:1.3.1.RELEASE:compile
[INFO] |     |  \- org.springframework.ldap:spring-ldap-ldif-batch:jar:1.3.1.RELEASE:compile
[INFO] |     |     +- org.springframework.batch:spring-batch-core:jar:2.0.3.RELEASE:compile
[INFO] |     |     |  \- org.codehaus.jettison:jettison:jar:1.0:compile
[INFO] |     |     \- org.springframework.batch:spring-batch-infrastructure:jar:2.0.3.RELEASE:compile
[INFO] |     +- com.fx.oa:sys-settings:jar:0.0.1-SNAPSHOT:compile
[INFO] |     \- com.fx.oa:per-jobgrade:jar:0.0.1-SNAPSHOT:compile
[INFO] |        \- com.fx.oa:per-jobgrade-api:jar:0.0.1-SNAPSHOT:compile
[INFO] +- c3p0:c3p0:jar:0.9.1.2:compile
[INFO] \- mysql:mysql-connector-java:jar:5.1.13:compile
[INFO]

   经对依赖树进行检索,发现有oa-workflow-interface下cxf-rt-frontend-jaxws的依赖eronimo-javamail_1.4_spec,於是根据maven的exclusion,排除传递依赖如下:

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.7.14</version>
<exclusions>
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-javamail_1.4_spec</artifactId>
</exclusion>
</exclusions>
</dependency>

    打包后经测试不再出现上述问题





用javamail 发邮件 报空指