Javamail STARTTLS使用Spring容器的自签名证书

时间:2016-02-09 16:24:52

标签: ssl javamail self-signed starttls

我正在尝试使用已打开STARTTLS的端口25上的自签名证书连接到SMTP服务器(James 3)。

我已启用JavaMail属性以信任所有主机,但我仍然收到PKIX证书路径验证错误。我该如何摆脱错误?

请参阅下面的代码。

   //Trust all hosts
    MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable","true");
    props.put("mail.smtp.starttls.required", "true");
    props.put("mail.smtp.auth.mechanisms", "PLAIN");
    props.put("mail.smtp.socketFactory.fallback", "false");
    props.put("mail.smtp.ssl.socketFactory", sf);

    Session session = Session.getInstance(props, null);

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(ti.sutUserName));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(ti.sutEmailAddress));


        BodyPart messageBodyPart = new MimeBodyPart();

        messageBodyPart.setText("This is message body");

        Multipart multipart = new MimeMultipart();



        log.info("Sending Message");

        Transport transport = session.getTransport("smtp");
        transport.connect(ti.sutSmtpAddress, ti.sutUserName, ti.sutPassword);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();`

1 个答案:

答案 0 :(得分:1)

我在Spring Boot容器中使用Javamail API(Compact)1.4,默认情况下使用Javamail 1.5.3。将jar更改为1.5.3后,程序开始正常工作。

请参阅:Spring Boot 1.2.5.RELEASE - Sending E-mail via Gmail SMTP

感谢您的帮助。