提问者:小点点

Javamail使用Spring容器启动带有自签名证书的TLS


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

我已启用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个答案

匿名用户

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

请参阅: Spring Boot 1.2.5.RELEASE - 通过 Gmail SMTP 发送电子邮件

谢谢你的帮助。