大家好我正在尝试使用网络逻辑服务器发送电子邮件..我可以通过运行java main的单个文件轻松发送电子邮件,但无法使用网络逻辑发送它。我得到了以下例外...(
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
这是我的代码:
Properties props = System.getProperties();
props.put("mail.smtp.user", SMTP_AUTH_USER);
props.put("mail.smtp.password", SMTP_AUTH_PWD);
props.setProperty("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.imaps.ssl.trust", "*");
props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(SMTP_AUTH_USER, SMTP_AUTH_PWD);
}
});
System.out.println("Created session");
session.setDebug(true);
Transport transport = session.getTransport("smtp");
System.out.println("Got Transport from session");
MimeMessage message = new MimeMessage(session);
message.setSubject("Movies Store Ticket Confirmation");
message.setContent(" Dear Farish,\n \n Your Ticket Booking is Confirmed. Thank you for booking the ticket.\n \n " +
"Movie name : EndGame \n " +
"Theater Name : Mstore \n " +
"Screen : Screen A \n " +
"Selected Seat: B04 B05 \n " +
"Amount : 160 \n " +
"Show Date : 05/07/2014 \n " +
"Show Time : 10:00 AM ", "text/plain");
message.setFrom(new InternetAddress("******@gmail.com"));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("********@gmail.com"));
System.out.println("Before Connecting");
transport.connect(SMTP_HOST_NAME,SMTP_AUTH_USER,SMTP_AUTH_PWD);
System.out.println("After Connecting");
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
System.out.println("After Sensing");
transport.close();
}
有人请帮帮我... 提前谢谢......
答案 0 :(得分:0)
查看您的例外情况,您尝试使用需要安全证书的安全服务器发送电子邮件。查看电子邮件服务器文档,了解如何在程序中启用安全问题。
答案 1 :(得分:0)
在transport.connect语句中给出端口号,如下所示。 transport.connect(SMTP_HOST_NAME,SMTP_PORT,SMTP_AUTH_USER,SMTP_AUTH_PWD);