【问题标题】:Java unable to find valid certificationJava 找不到有效的证书
【发布时间】:2017-07-07 06:12:40
【问题描述】:

我在使用 Java 向 Zimbra 邮件服务器发送电子邮件时遇到了 RuntimeException。

这是我的JavaTest.java

public class Test {

    public static void main(String[] args) {
        sendMail("receiver@domain.com","Test","Test Message",null); 
    }

    public static Properties getMailProperties() {
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.bevmi.com");
        props.put("mail.smtp.port", "587");
        return props;
    }

    public static boolean sendMail(String to, String subject,String text,String cc)  {
        final String username = "user@domain.com;
        final String password = "password";
        Properties props = getMailProperties();
        Session session = Session.getInstance(props,
        new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            message.setSender(new InternetAddress("sender@domain.com"));
            if ((cc!=null) && (!cc.isEmpty())) {
                message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
            }
            message.setSubject(subject);
            message.setText(text,  "UTF-8", "html");
            System.out.println("Sending Message to :" + to);
            Transport.send(message);
            System.out.println("Email Sent");
            return true;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}

我在“主”线程中得到了以下 RuntimeException 堆栈跟踪。

java.lang.RuntimeException: javax.mail.MessagingException: Could not convert socket to TLS; 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 at Test.sendMail(Test.java:57) at Test.main(Test.java:13)

【问题讨论】:

    标签: java email ssl zimbra


    【解决方案1】:

    你看过this吗? 这可能是您的解决方案。您的设置似乎缺少某些内容。

    在我给你的链接中,通过添加此代码行解决了同样的问题。

    props.put("mail.smtp.ssl.trust", "smtp.gmail.com");

    在这种情况下不应该包含"smtp.gmail.com",而是"smtp.benvi.com",如果那是你真正的主机的话。

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2015-07-16
      • 2015-02-01
      • 2017-06-12
      • 2020-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-20
      • 1970-01-01
      相关资源
      最近更新 更多