java在Linux上发送邮件: Error certification

标签 java

我尝试在Linux服务器上通过Java发送邮件。 我设置在 Tomcat 6 上运行,配置 SSL 但我收到一条错误消息:

 Can't send command to SMTP host 
 javax.mail.MessagingException: Can't send command to SMTP host;
  nested exception is: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

我的java代码如下:

    String host = "smtp.gmail.com";
    int port = 587;
    String username = "java.test@gmail.com";
    String password = "aabbcc";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");

    Session session = Session.getInstance(props);

    try {


        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("java.test@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("test504@gmail.com"));
        message.setSubject("Testing Subject");
        message.setText("Dear Mail Crawler," +
                "\n\n No spam to my email, please!");


        Transport transport = session.getTransport("smtp");
        transport.connect(host, port, username, password);


        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        System.out.println("ERROR: "+e.getMessage());
        System.out.println("ERROR: "+e.toString());

        throw new RuntimeException(e);
    }

如果我配置 Tomcat 没有 SSL -> 它可以发送邮件成功 但在 SSL 中 -> 我有上述错误

请帮忙修复错误。谢谢大家!

最佳答案

我认为它需要安全的 TLS 连接,请查看 this link知道怎么做

关于java在Linux上发送邮件: Error certification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7555752/

相关文章:

Java + Lombok + Guava + 验证

java - Spring boot - Snowflake JDBC - 应用程序加载时自动更改 session

java - 生产者消费者模式: on demand thread

java - Java 中的计划任务 - 使用哪个工具

java - JPA:将查询结果导出为CSV格式

java - 使用自定义值集合类型创建 Commons Collections MultiValueMap

java - 如何让maven在编译时只考虑一组重复文件中的一个?

java - Maven Clean 因签名异常而失败

java - 死锁检测的成本如何?

java - 如何验证字符串到日历输入?