在执行以下行
时连接到我的邮件服务器时,我收到以下异常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();
}
答案 0 :(得分:1)
因此,如果您对其他答案没有运气,请尝试查看您在邮件服务器上安装SSL证书的方式等。
答案 1 :(得分:0)
要从java发送电子邮件,您需要以下jars:
mail.jar
强> geronimo-javamail-transport-1.1.1.jar
强> geronimo-javamail_1.3.1_spec-1.1.jar
强> 请尝试使用以下方法从java发送电子邮件。此方法将使用SSL身份验证发送电子邮件。在下面的方法中,有三个参数: 列出收件人:列出此邮件的所有收件人。 主题:这封邮件的主题 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();
}
}
谢谢,