关于用javamail发送邮件时,由于邮件服务器要SSL,解决办法

关于用javamail发送邮件时,由于邮件服务器要SSL

发送邮件时得到异常如下:

 

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

 

 

Google一下,需要保存安装证书

 

官方的QA http://java.sun.com/products/javamail/FAQ.html#installcert

 

Q: When connecting to my mail server over SSL I get an exception like "unable to find valid certification path to requested target".
A: 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.

 

在这里 http://blogs.sun.com/andreas/entry/no_more_unable_to_find 有解决方案

 

用 http://blogs.sun.com/andreas/resource/InstallCert.java 的代码来安装证书

 

1.编译安装证书程序 javac InstallCert.java;

   运行安装证书生成证书       java InstallCert smtp.mailtest.com:465
2.将得到jssecacerts文件后复制到jdk1.6.0_16\jre\lib\security或jre1.6.0_06\lib\security\目录

3.编译测试程序 javac -cp ./mail.jar;./ SendMail.java

 

4.运行java -cp ./mail.jar;./ SendMail

 

根据提示信息查看邮件发送情况

 

注。smtp.mailtest.com 为测试的域名。根据实际情况修改测试程序及相关域名。

 

 

 

你可能感兴趣的:(关于用javamail发送邮件时,由于邮件服务器要SSL,解决办法)