Spring 邮件。 javax.net.ssl.SSLHandshakeException: 握手期间远程主机关闭连接

Spring Mail. javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake

我在未加密的端口上通过 smtp 协议连接到邮件服务器。 我收到错误

"Mail server connection failed; nested exception is javax.mail.MessagingException: Could not convert socket to TLS;\n  nested exception is:\n\tjavax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake. Failed messages: javax.mail.MessagingException: Could not convert socket to TLS;\n  nested exception is:\n\tjavax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake"

我的 bean 配置

@Bean
public JavaMailSender javaMailService() {
    JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
    javaMailSender.setHost(host);
    javaMailSender.setProtocol(protocol);
    javaMailSender.setUsername(from);
    javaMailSender.setPassword(password);
    javaMailSender.setPort(port);
    javaMailSender.setDefaultEncoding(encoding);
    Properties javaMailProperties = new Properties();
    javaMailProperties.put("mail.smtp.starttls.enable", "true");
    javaMailProperties.put("mail.smtp.auth", "true");
    javaMailProperties.put("mail.transport.protocol", "smtp");
    javaMailProperties.put("mail.debug", "true");
    javaMailProperties.put("mail.smtp.localhost", "127.0.0.1");
    javaMailProperties.put("mail.smtp.ssl.trust", "*");
    System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
    javaMailSender.setJavaMailProperties(javaMailProperties);

    return javaMailSender;
}

我可以在它起作用之前说一个。有什么问题吗?

需要移除 javaMailProperties.put("mail.smtp.starttls.enable", "true");

对于客户端和服务器都支持哪些 TLS 版本或密码套件,可能存在分歧。例如,如果您升级了 JDK,那可能已经改变了。 https.protocols 属性 未被 JavaMail 使用,但如果您出于其他原因需要设置它,则可能需要设置相应的 JavaMail 属性,例如 mail.smtp.ssl.protocols

您可能需要按照 SSLNOTES.txt 中的调试提示进行操作才能准确找出问题所在。