在执行下面的代码行时,我在连接到邮件服务器时遇到以下异常
transport.connect("test.mailserver.com",465,"test.user@test.com","testpwd");例外情况是:
(javax.mail.MessagingException) javax.mail.MessagingException: Exception reading response;
nested exception is:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: subject/issuer name chaining check failed代码如下:
protected static Session initializeSession(MailMessage p_msg) throws Exception{
//Get the SMTP Host
Properties prop = System.getProperties();
prop.put( "mail.smtps.host", "test.mailserver.com" );
prop.put("mail.transport.protocol", "smtps");
prop.put("mail.smtps.auth", true);
Session session = Session.getInstance(prop,null);
session.setDebug( p_msg.getDebugMode() );
return session;
}
protected static void sendMessage(MimeMessage p_msg) throws Exception{
Properties prop = System.getProperties();
Session session = Session.getDefaultInstance(prop, null);
Transport transport = session.getTransport("smtps");
transport.connect("test.mailserver.com",465,"test.user@test.com","testpwd");
transport.sendMessage(p_msg, p_msg.getAllRecipients());
transport.close();
}发布于 2010-12-16 14:49:56
我依稀记得我自己也遇到过这样的事情。事实证明,我错误地配置了SSL,将证书放在服务器的证书链中的顺序不正确。典型的web浏览器中的SSL堆栈并不关心这一点,但是(显然) Java中的客户端SSL引擎不能(或不会)处理以错误顺序呈现链的服务器。
因此,如果您没有得到其他答案,请尝试查看您在邮件服务器上安装SSL证书等的方式。
发布于 2010-12-16 13:52:38
要从java发送电子邮件,您需要以下jar:
mail.jargeronimo-javamail-transport-1.1.1.jargeronimo-javamail_1.3.1_spec-1.1.jar请尝试使用以下方法从java发送电子邮件。此方法将使用SSL身份验证发送电子邮件。在下面的方法中,有三个参数: List recipients :列出此邮件的所有收件人。messageToSend:此邮件的主题:邮件的消息体。
public void sendMail(List<String> recipents,String subject,String messageToSend)
{
setParameters();
try {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.debug", "true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
javax.mail.Session mailSession = javax.mail.Session.getDefaultInstance(props,new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(Your GmailID,your GMAIL Password);
}
});
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
InternetAddress addressFrom = new InternetAddress(fromEmailAddress);
MimeMessage message = new MimeMessage(mailSession);
message.setSender(addressFrom);
message.setSubject(subject);
message.setContent(messageToSend, "text/plain");
InternetAddress[] addressTo = new InternetAddress[recipents.size()];
for (int i = 0; i < recipents.size(); i++) {
addressTo[i] = new InternetAddress(recipents.get(i));
}
message.setRecipients(Message.RecipientType.TO, addressTo);
transport.connect();
transport.send(message);
transport.close();
} catch (MessagingException e) {
e.printStackTrace();
}
}谢谢,
https://stackoverflow.com/questions/4457731
复制相似问题