javax 无法将套接字转换为 TLS;

标签 java email gmail

我在使用 Javax 发送邮件时遇到问题。我们使用 gmail 大约 4 年从我们的 Java 软件发送邮件。现在我收到以下错误:

Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
    javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
    at SendEmail.sendMissingMailWeek(SendEmail.java:233)
    at main.negVerkaeufeMailenWeek(main.java:368)
    at main.main(main.java:79)
Caused by: javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
    javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
    at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1907)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:666)
    at javax.mail.Service.connect(Service.java:317)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)
    at javax.mail.Transport.send0(Transport.java:194)
    at javax.mail.Transport.send(Transport.java:124)
    at SendEmail.sendMissingMailWeek(SendEmail.java:226)
    ... 2 more
Caused by: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
    at sun.security.ssl.HandshakeContext.<init>(HandshakeContext.java:171)
    at sun.security.ssl.ClientHandshakeContext.<init>(ClientHandshakeContext.java:98)
    at sun.security.ssl.TransportContext.kickstart(TransportContext.java:220)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:428)
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:549)
    at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:486)
    at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1902)
    ... 9 more

Process finished with exit code 1
这些是我的 gmail 设置:
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.port", "587");
            props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
            props.put("mail.smtp.debug", "true");
我还尝试了其他邮件服务器(O365,Strato),但得到了同样的错误。

最佳答案

该错误表明与建立与邮件服务器的安全连接所需的 TLS 协议(protocol)相关的一些问题。
正如您在问题评论中看到的那样,问题可能是由很多不同的事情引起的。在不知道您的操作系统、JDK 和 JavaMail 库版本的情况下,我们只能访问真正的原因。
@g00se 给了你一个很好的建议:请尝试通过提供以下选项在启用 SSL 调试的情况下运行你的程序:

-Djavax.net.debug=all
此外,尝试在您的配置属性中明确包含 TLS 协议(protocol),它可能会有所帮助:
props.put("mail.smtp.ssl.protocols", "TLSv1.2");
无论如何,对于您指出的症状,并且由于 AFAIK Google 最近没有更改任何相关配置,您的问题可能与 a change 有关。在 JDK 发行版中引入,以便在默认情况下禁用不安全的 TLS 协议(protocol)版本:

security-libs/javax.net.ssl
Disable TLS 1.0 and 1.1

TLS 1.0 and 1.1 are versions of the TLS protocol that are no longer considered secure and have been superseded by more secure and modern versions (TLS 1.2 and 1.3).

These versions have now been disabled by default. If you encounter issues, you can, at your own risk, re-enable the versions by removing "TLSv1" and/or "TLSv1.1" from the jdk.tls.disabledAlgorithms security property in the java.security configuration file.


正如您在 bug description 中看到的那样,此更改已向后移植到不同的 JDK 版本。
也许您升级了 JDK 并提出了问题。
如果是这种情况,如报价中所示,请尝试编辑您的 java.security配置文件并删除TLSv1和/或 TLSv1.1来自 jdk.tls.disabledAlgorithms安全属性(property)。

关于javax 无法将套接字转换为 TLS;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68591048/

相关文章:

Android:如何从应用程序将图像作为电子邮件附件发送?

java - maven-jar-plugin 指定 mainClass

java - dijkstra算法的绘图图

java - 在哪里可以找到 IntelliJ IDEA 的启动错误日志?

java - 如何在JSF应用程序中检测客户端语言环境?

c# - 通过 smtp 的 gmail 对话

Outlook 中的 HTML 格式不正确

linux - 如何在Linux上发送邮件时使用别名?

javascript - 使用 AppScript 回复 Gmail 中更改收件人的电子邮件最终进入新线程

email - 在 Gmail 中,使用 Google Apps Script,是否可以将我收到的已翻译电子邮件转发到另一个电子邮件地址?