我在使用Java向Zimbra邮件服务器发送电子邮件时遇到RuntimeException。
以下是我的Java
课程Test.java
public class Test {
public static void main(String[] args) {
sendMail("receiver@domain.com","Test","Test Message",null);
}
public static Properties getMailProperties() {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.bevmi.com");
props.put("mail.smtp.port", "587");
return props;
}
public static boolean sendMail(String to, String subject,String text,String cc) {
final String username = "user@domain.com;
final String password = "password";
Properties props = getMailProperties();
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSender(new InternetAddress("sender@domain.com"));
if ((cc!=null) && (!cc.isEmpty())) {
message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
}
message.setSubject(subject);
message.setText(text, "UTF-8", "html");
System.out.println("Sending Message to :" + to);
Transport.send(message);
System.out.println("Email Sent");
return true;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
我在“主”线程中获得了以下RuntimeException堆栈跟踪。
java.lang.RuntimeException: javax.mail.MessagingException: Could not convert socket to TLS; 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 Test.sendMail(Test.java:57) at Test.main(Test.java:13)
答案 0 :(得分:0)
你看过this了吗? 这可能是您的解决方案。您的设置似乎缺少了一些东西。
在我给你的链接中,通过添加此代码行解决了同样的问题。
props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
在这种情况下,如果它是您真正的主机,则不应包含"smtp.gmail.com"
,"smtp.benvi.com"
。
我希望能提供帮助。