JavaMailSendernested异常为javax.mail.MessagingException:无法将套接字转换为TLS

时间:2020-09-11 13:16:47

标签: spring ssl jakarta-mail

我对java和spring不熟悉。 我阅读了有关我的PB的所有帖子,并尝试了所有解决方案,但是没有一个...

我的代码: UserController.java


@GetMapping("/forgot_password/{email}")
    public ResponseEntity<User> forgotPwd(@PathVariable String email) throws ResourceNotFoundException {
        //user caller exist?
        System.out.println(email);
        var caller = userService.findByEmail(email);
        //if not, return null
        if (caller == null) {
            return null;
        }
        
        //if yes, send him an email
        System.out.println("-------------------------------------");
        System.out.println("                     #2 /MimeMessage");

        
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        // mailSender.setDefaultEncoding("utf-8");
        mailSender.setHost("smtp.office365.com");
        mailSender.setPort(587);// 25 or 587?
        mailSender.setUsername("noreply@domain.io");
        mailSender.setPassword("my_password");
    
        Properties properties = new Properties();
        properties.put("mail.transport.protocol", "smtp");
        properties.put("mail.mime.charset", "utf-8");
        properties.put("mail.smtp.allow8bitmime", "true");
        properties.put("mail.smtps.allow8bitmime", "true");
        properties.put("mail.smtps.allow8bitmime", "true");
        properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.debug", "true");
        properties.put("mail.smtp.timeout", 30000);
        properties.setProperty("mail.smtp.auth", "true");
        properties.setProperty("mail.smtp.auth", "true");
        mailSender.setJavaMailProperties(properties);
        
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, "utf-8");
        
        try {
            System.out.println("------------------- #2.1 T R Y  ------------------");

            String contents = new String(Files.readAllBytes(Path.of(rootLocationString, "/emailContent.html")));
            mimeMessage.setContent(contents.replaceAll("firstname",caller.firstName), "text/html");
            helper.setTo(caller.email);
            helper.setSubject("Zelin: reset pwd");
            helper.setFrom("noreply@zelin.io");

            System.out.println("------------------- send  ------------------");

            mailSender.send(mimeMessage);

            System.out.println("---------- !!!!!!!! -----------  end #2.1 ------------   !!!!!!!!   ----");

        } catch (Exception err) {
            System.out.println("xxxxxxxxxxxxxxxxxxxxxxx      #2.2 C A T C H       xxxxxxxxxxxxxxxxxxxxxxx");
            System.out.println("\nMail fail:\n" + err.getMessage());
            System.out.println("xxxxxxxxxxxxxxxxxxxxxxx      end #2.2     xxxxxxxxxxxxxxxxxxxxxxx");
        }
        return ResponseEntity.ok(userService.findByEmail(email));
    }

您可以看到我尝试了其他文章中提供的所有属性和配置。

但是我仍然有这个错误:

终端:

Mail fail:
Mail server connection failed; nested exception is javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
        javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target. Failed messages: javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
        javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

完整回报:

-------------------------------------
                     #2 /MimeMessage
DEBUG: JavaMail version 1.6.2
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
------------------- #2.1 T R Y  ------------------
------------------- send  ------------------
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.office365.com", port 587, isSSL false
220 ZRAP278CA0002.outlook.office365.com Microsoft ESMTP MAIL Service ready at Fri, 11 Sep 2020 12:57:25 +0000
DEBUG SMTP: connected to host "smtp.office365.com", port: 587
EHLO DESKTOP-G3NCTGE
250-ZRAP278CA0002.outlook.office365.com Hello [141.0.205.31]
250-SIZE 157286400
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-8BITMIME
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "157286400"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
STARTTLS
220 2.0.0 SMTP server ready
xxxxxxxxxxxxxxxxxxxxxxx      #2.2 C A T C H       xxxxxxxxxxxxxxxxxxxxxxx

Mail fail:
Mail server connection failed; nested exception is javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
        javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target. Failed messages: javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
        javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
xxxxxxxxxxxxxxxxxxxxxxx      end #2.2     xxxxxxxxxxxxxxxxxxxxxxx

请有人可以帮助我吗?

0 个答案:

没有答案
相关问题