Java应用程序无法从命令行发送电子邮件,确定是Eclipse

时间:2015-10-15 02:02:55

标签: java spring email ssl javamail

我有一个使用Spring JavaMailSenderImpl的Java应用程序。当我在Eclipse下运行它时它发送邮件很好,但是当从Windows命令行运行它时我得到:

Exception in thread "main" org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
        java.net.SocketException: Connection reset. Failed messages: javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
        java.net.SocketException: Connection reset; message exception details (1) are:
Failed message 1:
javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
        java.net.SocketException: Connection reset
        at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1907)

邮件发件人bean定义为:

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="xxx" />
        <property name="username" value="xxx" />
        <property name="password" value="xxx" />
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.debug">true</prop>
                <prop key="mail.smtp.port">587</prop>
                <prop key="mail.smtp.starttls.enable">true</prop>
                <prop key="mail.smtp.ssl.trust">xxx</prop>
            </props>
        </property>
    </bean>

当我使用 -Djavax.net.debug = all 运行失败的应用程序时,我得到:

*** ClientHello, TLSv1
RandomCookie:  GMT: 1444872267 bytes = { 11, 176, 76, 50, 109, 72, 37, 170, 87, 187, 17, 29, 131, 177, 0, 222, 129, 223, 21, 127, 165, 248, 122, 127, 39, 133, 190, 14 }
Session ID:  {}
Cipher Suites: [TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, TLS_RSA_WITH_AES_128_CBC_SHA, TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDH_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_DSS_WITH_AES_128_CBC_SHA, TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA, T
LS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, SSL_RSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA, SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA, SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA, TLS_EMPTY_RENEGOTIATION_INFO_SCSV]
Compression Methods:  { 0 }
Extension elliptic_curves, curve names: {secp256r1, sect163k1, sect163r2, secp192r1, secp224r1, sect233k1, sect233r1, sect283k1, sect283r1, secp384r1, sect409k1, sect409r1, secp521r1, sect571k1, sect571r1, secp160k1, secp160r1, secp160r2, sect163r1, secp192k1, sect193r1, sect193r2, secp224k1, sect23
9k1, secp256k1}
Extension ec_point_formats, formats: [uncompressed]
Extension server_name, server_name: [type=host_name (0), value=    <cut>]
***
[write] MD5 and SHA1 hashes:  len = 171
0000: 01 00 00 A7 03 01 56 1F   00 4B 0B B0 4C 32 6D 48  ......V..K..L2mH
0010: 25 AA 57 BB 11 1D 83 B1   00 DE 81 DF 15 7F A5 F8  %.W.............
0020: 7A 7F 27 85 BE 0E 00 00   1E C0 09 C0 13 00 2F C0  z.'.........../.
0030: 04 C0 0E 00 33 00 32 C0   08 C0 12 00 0A C0 03 C0  ....3.2.........
0040: 0D 00 16 00 13 00 FF 01   00 00 60 00 0A 00 34 00  ..........`...4.
0050: 32 00 17 00 01 00 03 00   13 00 15 00 06 00 07 00  2...............
0060: 09 00 0A 00 18 00 0B 00   0C 00 19 00 0D 00 0E 00  ................
0070: 0F 00 10 00 11 00 02 00   12 00 04 00 05 00 14 00  ................
0080: 08 00 16 00 0B 00 02 01   00 00 00 00 1E 00 1C 00  ................
<cut>
main, WRITE: TLSv1 Handshake, length = 171
[Raw write]: length = 176
0000: 16 03 01 00 AB 01 00 00   A7 03 01 56 1F 00 4B 0B  ...........V..K.
0010: B0 4C 32 6D 48 25 AA 57   BB 11 1D 83 B1 00 DE 81  .L2mH%.W........
0020: DF 15 7F A5 F8 7A 7F 27   85 BE 0E 00 00 1E C0 09  .....z.'........
0030: C0 13 00 2F C0 04 C0 0E   00 33 00 32 C0 08 C0 12  .../.....3.2....
0040: 00 0A C0 03 C0 0D 00 16   00 13 00 FF 01 00 00 60  ...............`
0050: 00 0A 00 34 00 32 00 17   00 01 00 03 00 13 00 15  ...4.2..........
0060: 00 06 00 07 00 09 00 0A   00 18 00 0B 00 0C 00 19  ................
0070: 00 0D 00 0E 00 0F 00 10   00 11 00 02 00 12 00 04  ................
0080: 00 05 00 14 00 08 00 16   00 0B 00 02 01 00 00 00  ................
<cut>
main, handling exception: java.net.SocketException: Connection reset
main, SEND TLSv1.2 ALERT:  fatal, description = unexpected_message
main, WRITE: TLSv1.2 Alert, length = 2
main, Exception sending alert: java.net.SocketException: Connection reset by peer: socket write error
main, called closeSocket()
Exception in thread "main" org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
        java.net.SocketException: Connection reset. Failed messages: javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
        java.net.SocketException: Connection reset; message exception details (1) are:
Failed message 1:
javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
        java.net.SocketException: Connection reset
        at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1907)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:666)

由于缺少对Client_Hello的响应,我假设输出消息被阻止,但我尝试暂时禁用Windows防火墙和Avast防病毒软件而不做任何更改。 (并不完全惊讶,因为python应用程序可以使用相同的服务器发送邮件而没有问题。)

Wireshark跟踪似乎(我不是协议专家......)显示来自主机的拒绝消息:

wireshark trace

查看Client_Hellos我看到失败者中的密码套件列表要小得多: Failing Client_Hello

比成功的: OK Client_Hello

这可能是拒绝Client_Hello的原因吗?

从命令行运行时,可能导致应用程序的行为有何不同?

2 个答案:

答案 0 :(得分:0)

您似乎对是否使用端口587或端口465感到困惑。请尝试getting rid of all the socket factory settings并查看是否有帮助。

答案 1 :(得分:0)

您是否检查过Eclipse并且命令行中的两个都使用相同的javax.mail version?我使用1.5.something版本的堆栈跟踪几乎相同。它固定至少1.6.0。 试一试!