开发者

java send mail on Linux: Error certification

I try to send mail by Java on Linux server. I setup to run on Tomcat 6, config SSL but I got an error message:

 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

My java code like below:

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

    Properties props = new Properties();
    props.pu开发者_高级运维t("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);
    }

If I config Tomcat without SSL -> It can send mail Success But within SSL -> I have above error

Please help fix error. Thank guys!


I think it needs a secure TLS connection , take a look at this link to know how to do it

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜