【发布时间】:2014-05-01 22:07:03
【问题描述】:
我正在尝试通过我们的私人 smtp 服务器发送电子邮件,这是我的代码
public static void main(String args[]) throws MessagingException {
Properties props = System.getProperties();
props.put("mail.smtps.host", "gi-systems.net");
props.put("mail.smtps.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.port", "25");
Session session = Session.getInstance(props, null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("xxxx@gi-systems.net"));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("yyyy@hotmail.com, false));
msg.setSubject("Password Recovery");
msg.setText(pwd);
msg.setSentDate(new Date());
SMTPTransport t = (SMTPTransport) session.getTransport("smtps");
t.connect("smtp.gi-systems.net", "xxxx@gi-systems.net", "mypassword");
t.sendMessage(msg, msg.getAllRecipients());
System.out.println("Response: " + t.getLastServerResponse());
t.close();
}
但是当我运行这段代码时,我得到了这个异常
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.gi-systems.net, port: 465;
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 com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1706)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
at javax.mail.Service.connect(Service.java:291)
at javax.mail.Service.connect(Service.java:172)
at com.mycompany.sendmail.App.SendEmail(App.java:45)
at com.mycompany.sendmail.App.main(App.java:64)
Caused by: 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 sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1886)
......
Ps:当我使用 gmail smtp 和电子邮件地址时,它工作正常
我该如何解决这个异常???
【问题讨论】: