我尝试在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);
}
如果我在没有SSL的情况下配置Tomcat - >它可以发送邮件成功 但在SSL内 - >我有以上错误
请帮助解决错误。谢谢你们!