public class MailAction extends BaseAction {
private String smtphost = Constants.MAIL_SERVER_IP; // 发送邮件服务器IP
private int port = Constants.MAIL_SERVER_PORT; // 发送邮件服务器端口
private String user1 = Constants.MAIL_SERVER_USER; // 邮件服务器登录用户名
private String password = Constants.MAIL_SERVER_PASSWORD; // 邮件服务器登录密码
private String from = Constants.MAIL_SENDPASSWROD_SENDER_ADDRESS; // 发送人邮件地址
private String subject = "基础密钥修改通知!"; // 邮件标题
public String sendMail(String to, String body) {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties props = new Properties();
props.put("mail.smtp.host", smtphost);
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.port", port);
props.put("mail.smtp.socketFactory.port", port);
props.put("mail.smtp.auth", "true");
Session ssn = Session.getDefaultInstance(props, new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user1, password);
}});
ssn.setDebug(true);
MimeMessage message = new MimeMessage(ssn);
InternetAddress fromAddress;
try {
fromAddress = new InternetAddress(from);
message.setFrom(fromAddress);
InternetAddress toAddress = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO, toAddress);
message.setSubject(subject);
message.setText(body);
Transport transport = ssn.getTransport("smtp");
transport.connect(smtphost, port, user1, password);
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
log.info("mail sucess");
transport.close();
return "0";
} catch (AddressException e) {
// TODO Auto-generated catch block
log.error("mail failed1", e);
return "1";
} catch (MessagingException e) {
// TODO Auto-generated catch block
log.error("mail failed2", e);
return "1";
}
}
}
运行后出现下面的异常,请高手指点
DEBUG SMTP: exception reading response: 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
private String smtphost = Constants.MAIL_SERVER_IP; // 发送邮件服务器IP
private int port = Constants.MAIL_SERVER_PORT; // 发送邮件服务器端口
private String user1 = Constants.MAIL_SERVER_USER; // 邮件服务器登录用户名
private String password = Constants.MAIL_SERVER_PASSWORD; // 邮件服务器登录密码
private String from = Constants.MAIL_SENDPASSWROD_SENDER_ADDRESS; // 发送人邮件地址
private String subject = "基础密钥修改通知!"; // 邮件标题
public String sendMail(String to, String body) {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties props = new Properties();
props.put("mail.smtp.host", smtphost);
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.port", port);
props.put("mail.smtp.socketFactory.port", port);
props.put("mail.smtp.auth", "true");
Session ssn = Session.getDefaultInstance(props, new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user1, password);
}});
ssn.setDebug(true);
MimeMessage message = new MimeMessage(ssn);
InternetAddress fromAddress;
try {
fromAddress = new InternetAddress(from);
message.setFrom(fromAddress);
InternetAddress toAddress = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO, toAddress);
message.setSubject(subject);
message.setText(body);
Transport transport = ssn.getTransport("smtp");
transport.connect(smtphost, port, user1, password);
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
log.info("mail sucess");
transport.close();
return "0";
} catch (AddressException e) {
// TODO Auto-generated catch block
log.error("mail failed1", e);
return "1";
} catch (MessagingException e) {
// TODO Auto-generated catch block
log.error("mail failed2", e);
return "1";
}
}
}
运行后出现下面的异常,请高手指点
DEBUG SMTP: exception reading response: 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
http://www.javaworld.com.tw/jute/post/view?bid=7&id=265167
or self-signed certificate instead of a certificate signed by a commercial Certificate Authority.
You'll need to install the server's certificate into your trust store.
The InstallCert program will help
楼主看看这里
javax.net.ssl.TrustManager[] trustAllCerts =new javax.net.ssl.TrustManager[1];
javax.net.ssl.TrustManager tm = new miTM();
trustAllCerts[0] = tm;
javax.net.ssl.SSLContext sc =javax.net.ssl.SSLContext.getInstance("SSL");
sc.init(null,trustAllCerts,null);
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());