• 解决javax.mail.MessagingException: Could not convert socket to TLS;


    之前一直采用jre-1.8和jdk-1.8,基本还顺利。今年升级了服务器硬件,顺便就升级了jre和jdk到版本17,然后就出现了一些问题。其中最棘手的就是这个javax.mail报的错:

    1. Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not convert socket to TLS;
    2. nested exception is:
    3. javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
    4. at util.EmailEncrypt.sendMessageToMe(EmailEncrypt.java:60)
    5. at util.EmailEncrypt.main(EmailEncrypt.java:17)
    6. Caused by: javax.mail.MessagingException: Could not convert socket to TLS;
    7. nested exception is:
    8. javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
    9. at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1907)
    10. at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:666)
    11. at javax.mail.Service.connect(Service.java:317)
    12. at javax.mail.Service.connect(Service.java:176)
    13. at javax.mail.Service.connect(Service.java:125)
    14. at javax.mail.Transport.send0(Transport.java:194)
    15. at javax.mail.Transport.send(Transport.java:124)
    16. at util.EmailEncrypt.sendMessageToMe(EmailEncrypt.java:56)
    17. ... 1 more
    18. Caused by: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
    19. at java.base/sun.security.ssl.HandshakeContext.(HandshakeContext.java:172)
    20. at java.base/sun.security.ssl.ClientHandshakeContext.(ClientHandshakeContext.java:103)
    21. at java.base/sun.security.ssl.TransportContext.kickstart(TransportContext.java:240)
    22. at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:448)
    23. at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:426)
    24. at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:549)
    25. at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:486)
    26. at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1902)
    27. ... 8 more

    原代码如下:

    1. public static void main(String[] args) {
    2. EmailEncrypt.sendMessageToMe("toMe", "toMeOnly");
    3. }
    4. public static void sendMessageToMe(String subject, String body) {
    5. // 服务器名称: smtp.office365.com
    6. // 端口: 587
    7. // 加密方法: STARTTLS
    8. final String username = "*****";
    9. final String password = "*****";
    10. Properties props = new Properties();
    11. props.put("mail.smtp.auth", "true");
    12. props.put("mail.smtp.starttls.enable", "true");
    13. props.put("mail.smtp.host", "smtp.office365.com");
    14. props.put("mail.smtp.port", "587");
    15. Session session = Session.getInstance(props,
    16. new javax.mail.Authenticator() {
    17. protected PasswordAuthentication getPasswordAuthentication() {
    18. return new PasswordAuthentication(username, password);
    19. }
    20. });
    21. try {
    22. Message message = new MimeMessage(session);
    23. message.setFrom(new InternetAddress("*****"));
    24. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("*****"));
    25. message.setSubject(subject);
    26. message.setText(body);
    27. Transport.send(message);
    28. System.out.println("Done");
    29. } catch (MessagingException e) {
    30. throw new RuntimeException(e);
    31. }
    32. }

    试过一些方法:

    一、在代码中增加props.put("mail.smtp.ssl.trust", "smtp-mail.outlook.com"),没成功。

    二、去/jdk-17.jdk/Contents/Home/lib/security调整文件,没找到对应的表述。

    三、问过C知道,建议我:“禁用TLS,在代码中设置mail.smtp.starttls.enable为false。”这就是扯淡,毕竟outlook要求采用STARTTLS,我如果不设置,怎么可能连得上服务器呢?

    四、问了星火,其中有一条:“检查您的JavaMail代码是否正确设置了TLS选项。确保您已正确指定了TLS端口和协议版本。例如,对于Gmail,TLS端口是587,协议版本是STARTTLS。”好像是有点意思,但我开始没有理解。

    最后,直接去了bing国际,看到一位外国友人给出以下方法:

    props.put("mail.smtp.ssl.protocols", "TLSv1.2");

    成功。

    再看星火给的答案是相对靠谱的,Java-17需要显式指定TLS的协议版本,才可以正确运行。

    有兴趣的同学,可以再试一下TLSv1.1、TLSv1.3之类的。

  • 相关阅读:
    使用 LoRA 和 QLoRA 对大型语言模型进行参数高效的微调
    HACKTHEBOX——Bank
    拼多多anti-token 字段加解密学习分析
    DDD/ABP/EF Core 实现值对象Value Object
    Docker入门
    Ubuntu下发送邮件
    【springboot整合ES】springboot整合ES
    【云原生 | Kubernetes 系列】----HPA自动伸缩
    动态内存管理
    js 谈谈Generator和[Symbol.iterator]
  • 原文地址:https://blog.csdn.net/tian_xuezhi/article/details/134088660