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 

解决方案 »

  1.   

    参考一下
    http://www.javaworld.com.tw/jute/post/view?bid=7&id=265167
      

  2.   

    Your server is probably using a test certificate 
    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
      

  3.   

    可能是你要访问的服务器上的服务器证书内容和服务器不相符。也就是ssl验证没有通过
      

  4.   

    http://blog.csdn.net/cool_scorpion/archive/2008/05/16/2451632.aspx
    楼主看看这里
      

  5.   

    建立连接前 调用以下方法
            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());