javax.net.ssl.SSLHandshakeException 尝试使用 JavaMail SMTP 发送电子邮件时

javax.net.ssl.SSLHandshakeException when trying to send email with JavaMail SMTP

我查看了其他类似的问题,这些问题建议添加 properties.put("mail.smtp.ssl.trust", "smtp.gmail.com");,但仍然对我不起作用。我还看到一些建议注释掉的答案 properties.put("mail.smtp.starttls.enable", "true"); 这对我也不起作用。我没有除默认 windows 以外的防病毒软件,也没有其他特殊程序;为什么会这样?

代码

 public static void sendMail(String recipient) throws MessagingException {
        System.out.println("Preparing to send email...");
        Properties properties = new Properties();

        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.ssl.trust", "smtp.gmail.com");
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.debug", "true");
        properties.put("mail.smtp.port", "587");

        String myAccountEmail = "alexfarts05@gmail.com";
        String myAccountPassword = "alexIsFarting!";

        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(myAccountEmail, myAccountPassword);
            }
        });
        Message message = prepareMessage(session, myAccountEmail, recipient);
        Transport.send(message);
        System.out.println("Email sent!");
    }
    private static Message prepareMessage(Session session, String myAccountEmail, String recipient) {
        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(myAccountEmail));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
            message.setSubject("Hello, this is a test email!");
            message.setText("Big strong men");
            return message;
        } catch (Exception ex) {
            Logger.getLogger(SendMail.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

完全错误

Exception in thread "main" 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:253)
    at javax.mail.Transport.send(Transport.java:124)
    at SendMail.sendMail(SendMail.java:33)
    at Main.main(Main.java:5)
Caused by: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
    at java.base/sun.security.ssl.HandshakeContext.<init>(HandshakeContext.java:172)
    at java.base/sun.security.ssl.ClientHandshakeContext.<init>(ClientHandshakeContext.java:103)
    at java.base/sun.security.ssl.TransportContext.kickstart(TransportContext.java:239)
    at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:443)
    at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:421)
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:527)
    at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:464)
    at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1902)
    ... 8 more

我找到了解决方案,将这一行 properties.put("mail.smtp.ssl.protocols", "TLSv1.2" 添加到属性中,并为您正在使用的 google 帐户创建一个 app password 修复了它。您需要在账户上启用 2FA,然后在您的程序中使用创建的密码

完成最终属性

    properties.put("mail.smtp.host", "smtp.gmail.com");
    properties.put("mail.smtp.port", "587");
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.starttls.required", "true");
    properties.put("mail.smtp.ssl.protocols", "TLSv1.2");