通过SMTP发送邮件时出现“MessagingException:无法将套接字转换为TLS”

时间:2016-04-08 15:42:40

标签: java smtp javamail starttls

使用 JavaMail 通过 SMTP(+ STARTTLS)发送邮件时,会引发以下异常:

javax.mail.MessagingException: Could not convert socket to TLS;
nested exception is:
javax.net.ssl.SSLException: java.lang.ArrayIndexOutOfBoundsException

这里的多个帖子(在stackoverflow上)和其他地方建议邮件传输会话的一些属性,即

props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.ssl.trust","*");

在解决这个问题上发挥了作用,但我已经尝试了这些参数的所有组合(存在/不存在),并且它们都没有解决问题。

有人知道如何解决这个问题吗?

其他信息/详情

Java版本:1.6.0

JavaMail版本:1.5.2

邮件传输会话的属性:

props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.host","XXX");
props.put("mail.smtp.port","587");
props.put("mail.smtp.auth","true");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.ssl.trust","*");
props.put("mail.smtp.user","XXX");
props.put("mail.smtp.password","XXX");
props.put("mail.smtp.connectiontimeout","60000");
props.put("mail.smtp.timeout","600000");
props.put("mail.smtp.writetimeout","60000");

从JavaMail调试输出(在引发异常之前):

DEBUG: JavaMail version 1.5.2
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.
MTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Mi
rosystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,
mtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax
mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: need username and password for authentication
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "XXX", port 587, isSSL false
220 bsmtp.bon.at ESMTP (1)
DEBUG SMTP: connected to host "XXX", port: 587
EHLO ZZZ
250-bsmtp.bon.at
250-PIPELINING
250-SIZE 104857600
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250 8BITMIME
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE", arg "104857600"
DEBUG SMTP: Found extension "ETRN", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
STARTTLS
220 2.0.0 Ready to start TLS

异常的完整堆栈跟踪:

javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
    javax.net.ssl.SSLException: java.lang.ArrayIndexOutOfBoundsException
    at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1999)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:709)
    at javax.mail.Service.connect(Service.java:386)
    at javax.mail.Service.connect(Service.java:245)
    at javax.mail.Service.connect(Service.java:194)
    at com.safebook.MailMessage.send(MailMessage.java:144)
    at com.safebook.MailMessage.send(MailMessage.java:80)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:611)
    at com.ibm.jscript.types.JavaAccessObject.call(JavaAccessObject.java:321)
    at com.ibm.jscript.types.FBSObject.call(FBSObject.java:161)
    at com.ibm.jscript.ASTTree.ASTCall.interpret(ASTCall.java:197)
    at com.ibm.jscript.ASTTree.ASTAssign.interpret(ASTAssign.java:91)
    at com.ibm.jscript.ASTTree.ASTBlock.interpret(ASTBlock.java:100)
    at com.ibm.jscript.ASTTree.ASTTry.interpret(ASTTry.java:109)
    at com.ibm.jscript.std.FunctionObject._executeFunction(FunctionObject.java:261)
    at com.ibm.jscript.std.FunctionObject.executeFunction(FunctionObject.java:185)
    at com.ibm.jscript.std.FunctionObject.call(FunctionObject.java:171)
    at com.ibm.jscript.types.FBSObject.call(FBSObject.java:161)
    at com.ibm.jscript.ASTTree.ASTCall.interpret(ASTCall.java:197)
    at com.ibm.jscript.ASTTree.ASTIf.interpret(ASTIf.java:85)
    at com.ibm.jscript.ASTTree.ASTProgram.interpret(ASTProgram.java:119)
    at com.ibm.jscript.types.FBSGlobalObject$GlobalMethod.call(FBSGlobalObject.java:280)
    at com.ibm.jscript.types.FBSObject.call(FBSObject.java:161)
    at com.ibm.jscript.types.FBSGlobalObject$GlobalMethod.call(FBSGlobalObject.java:219)
    at com.ibm.jscript.ASTTree.ASTCall.interpret(ASTCall.java:197)
    at com.ibm.jscript.ASTTree.ASTBlock.interpret(ASTBlock.java:100)
    at com.ibm.jscript.ASTTree.ASTTry.interpret(ASTTry.java:109)
    at com.ibm.jscript.ASTTree.ASTProgram.interpret(ASTProgram.java:119)
    at com.ibm.jscript.ASTTree.ASTProgram.interpretEx(ASTProgram.java:139)
    at com.ibm.jscript.JSExpression._interpretExpression(JSExpression.java:435)
    at com.ibm.jscript.JSExpression.access$1(JSExpression.java:424)
    at com.ibm.jscript.JSExpression$2.run(JSExpression.java:414)
    at java.security.AccessController.doPrivileged(AccessController.java:448)
    at com.ibm.jscript.JSExpression.interpretExpression(JSExpression.java:410)
    at com.ibm.jscript.JSExpression.evaluateValue(JSExpression.java:251)
    at com.ibm.jscript.JSExpression.evaluateValue(JSExpression.java:234)
    at com.ibm.xsp.javascript.JavaScriptInterpreter.interpret(JavaScriptInterpreter.java:222)
    at com.ibm.xsp.binding.javascript.JavaScriptMethodBinding.invoke(JavaScriptMethodBinding.java:111)
    at com.ibm.xsp.component.UIViewRootEx.initBeforeContents(UIViewRootEx.java:1621)
    at com.ibm.xsp.page.compiled.AbstractCompiledPage.initComponent(AbstractCompiledPage.java:333)
    at com.ibm.xsp.page.compiled.AbstractCompiledPage.createTree(AbstractCompiledPage.java:256)
    at com.ibm.xsp.page.compiled.AbstractCompiledPage.createViewRoot(AbstractCompiledPage.java:167)
    at com.ibm.xsp.application.ViewHandlerExImpl._createViewRoot(ViewHandlerExImpl.java:521)
    at com.ibm.xsp.application.ViewHandlerExImpl.createViewRoot(ViewHandlerExImpl.java:567)
    at com.ibm.xsp.application.ViewHandlerExImpl.doCreateView(ViewHandlerExImpl.java:142)
    at com.ibm.xsp.application.ViewHandlerEx.createView(ViewHandlerEx.java:90)
    at com.ibm.xsp.webapp.FacesServlet.serviceView(FacesServlet.java:251)
    at com.ibm.xsp.webapp.FacesServletEx.serviceView(FacesServletEx.java:157)
    at com.ibm.xsp.webapp.FacesServlet.service(FacesServlet.java:160)
    at com.ibm.xsp.webapp.FacesServletEx.service(FacesServletEx.java:138)
    at com.ibm.xsp.webapp.DesignerFacesServlet.service(DesignerFacesServlet.java:103)
    at com.ibm.designer.runtime.domino.adapter.ComponentModule.invokeServlet(ComponentModule.java:576)
    at com.ibm.domino.xsp.module.nsf.NSFComponentModule.invokeServlet(NSFComponentModule.java:1335)
    at com.ibm.designer.runtime.domino.adapter.ComponentModule$AdapterInvoker.invokeServlet(ComponentModule.java:853)
    at com.ibm.designer.runtime.domino.adapter.ComponentModule$ServletInvoker.doService(ComponentModule.java:796)
    at com.ibm.designer.runtime.domino.adapter.ComponentModule.doService(ComponentModule.java:565)
    at com.ibm.domino.xsp.module.nsf.NSFComponentModule.doService(NSFComponentModule.java:1319)
    at com.ibm.domino.xsp.module.nsf.NSFService.doServiceInternal(NSFService.java:662)
    at com.ibm.domino.xsp.module.nsf.NSFService.doService(NSFService.java:482)
    at com.ibm.designer.runtime.domino.adapter.LCDEnvironment.doService(LCDEnvironment.java:357)
    at com.ibm.designer.runtime.domino.adapter.LCDEnvironment.service(LCDEnvironment.java:313)
    at com.ibm.domino.xsp.bridge.http.engine.XspCmdManager.service(XspCmdManager.java:272)
Caused by: 
javax.net.ssl.SSLException: java.lang.ArrayIndexOutOfBoundsException
    at com.ibm.jsse2.o.a(o.java:26)
    at com.ibm.jsse2.SSLSocketImpl.a(SSLSocketImpl.java:340)
    at com.ibm.jsse2.SSLSocketImpl.a(SSLSocketImpl.java:225)
    at com.ibm.jsse2.SSLSocketImpl.a(SSLSocketImpl.java:367)
    at com.ibm.jsse2.SSLSocketImpl.a(SSLSocketImpl.java:32)
    at com.ibm.jsse2.SSLSocketImpl.startHandshake(SSLSocketImpl.java:95)
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:543)
    at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:480)
    at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1994)
    ... 64 more
Caused by: 
java.lang.ArrayIndexOutOfBoundsException
    at com.ibm.crypto.provider.yc.a(Unknown Source)
    at com.ibm.crypto.provider.yc.a(Unknown Source)
    at com.ibm.crypto.provider.yc.a(Unknown Source)
    at com.ibm.crypto.provider.TlsMasterSecretGenerator.engineGenerateKey(Unknown Source)
    at javax.crypto.KeyGenerator.generateKey(Unknown Source)
    at com.ibm.jsse2.kb.b(kb.java:114)
    at com.ibm.jsse2.kb.a(kb.java:138)
    at com.ibm.jsse2.lb.a(lb.java:113)
    at com.ibm.jsse2.lb.a(lb.java:120)
    at com.ibm.jsse2.kb.t(kb.java:227)
    at com.ibm.jsse2.kb.a(kb.java:425)
    at com.ibm.jsse2.SSLSocketImpl.a(SSLSocketImpl.java:785)
    at com.ibm.jsse2.SSLSocketImpl.h(SSLSocketImpl.java:675)
    at com.ibm.jsse2.SSLSocketImpl.a(SSLSocketImpl.java:669)
    ... 68 more

2 个答案:

答案 0 :(得分:1)

看起来JDK的SSL支持出了问题。也许来自服务器的证书有些令人困惑。您正在使用相当旧版本的JDK。我会尝试升级到当前版本,看看是否能解决问题。您还可以尝试在JDK中启用一些低级SSL调试输出,如JavaMail SSLNOTES.txt中的链接所述。

答案 1 :(得分:0)

也许您的JavaMail库(mail.jar或javax.mail.jar)太旧了。 从此处下载最新版本:https://javaee.github.io/javamail/