我尝试在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中->我有上述错误
请帮助解决错误。谢谢大家!
最佳答案
我认为它需要安全的TLS连接,请查看this link知道如何进行
关于java - Java在Linux上发送邮件:错误认证,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7555752/