- public class JavaMailTest1 {
- public static void main(String[] args) throws MessagingException {
- Properties props = new Properties();
- props.setProperty("mail.debug", "true");
- props.setProperty("mail.smtp.auth", "true");
- props.setProperty("mail.transport.protocol", "smtp");
- // SSL
- props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
- props.setProperty("mail.smtp.socketFactory.fallback", "false");
- props.setProperty("mail.smtp.port", "465");
- props.setProperty("mail.smtp.socketFactory.port", "465");
- Session session = Session.getInstance(props);
- Message msg = new MimeMessage(session);
- msg.setText("你好吗?");
- msg.setFrom(new InternetAddress("发件箱地址"));
- Transport transport = session.getTransport();
- transport.connect("smtp.sina.com", "用户名", "密码");
- transport.sendMessage(msg, new Address[] {new InternetAddress("收件箱地址")});
- transport.close();
- }
- }
- 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
- at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972)
- at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
- at javax.mail.Service.connect(Service.java:295)
- at javax.mail.Service.connect(Service.java:176)
|
来自CODE的代码片
InstallCert.java
- Loading KeyStore C:\Program Files\Java\jre7\lib\security\cacerts...
- Opening connection to smtp.sina.com:465...
- Starting SSL handshake...
- 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
- at sun.security.ssl.Alerts.getSSLException(Unknown Source)
- at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
- at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
- at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
- at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)
- at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
- at sun.security.ssl.Handshaker.processLoop(Unknown Source)
- at sun.security.ssl.Handshaker.process_record(Unknown Source)
- at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
- at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
- at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
- at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
- at InstallCert.main(InstallCert.java:97)
- Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
- at sun.security.validator.PKIXValidator.doBuild(Unknown Source)
- at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
- at sun.security.validator.Validator.validate(Unknown Source)
- at sun.security.ssl.X509TrustManagerImpl.validate(Unknown Source)
- at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)
- at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
- at InstallCert$SavingTrustManager.checkServerTrusted(InstallCert.java:192)
- at sun.security.ssl.AbstractTrustManagerWrapper.checkServerTrusted(Unknown Source)
- ... 9 more
- Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
- at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
- at java.security.cert.CertPathBuilder.build(Unknown Source)
- ... 17 more
- Server sent 1 certificate(s):
- 1 Subject CN=*.sina.com, O="Sina.com Technology(China)Co.,ltd", L=Beijing, ST=Beijing, C=CN, SERIALNUMBER=mL/iTnzl-0Pr1rH-6U2RZH/h3zFjZxoK
- Issuer CN=GeoTrust SSL CA, O="GeoTrust, Inc.", C=US
- sha1 43 a9 5b bc 9b 86 85 99 e3 21 63 af b0 09 78 4a 67 25 d7 a1
- md5 90 c2 45 da 67 68 cd c2 44 56 21 ef ed c6 6b 5e
- Enter certificate to add to trusted keystore or 'q' to quit: [1]
更新2014-01-04:
今天无意中看到了JavaMail有这样的协议支持描述:
- Protocol Store or Uses Supports
- Name Transport? SSL? STARTTLS?
- -------------------------------------------------
- imap Store No Yes
- imaps Store Yes N/A
- pop3 Store No Yes
- pop3s Store Yes N/A
- smtp Transport No Yes
- smtps Transport Yes N/A
- public class JavaMailTest1 {
- public static void main(String[] args) throws MessagingException {
- Properties props = new Properties();
- props.setProperty("mail.debug", "true");
- props.setProperty("mail.smtp.auth", "true");
- // 协议名称设置为smtps,会使用SSL
- props.setProperty("mail.transport.protocol", "smtps");
- Session session = Session.getInstance(props);
- Message msg = new MimeMessage(session);
- msg.setText("你好吗?");
- msg.setFrom(new InternetAddress("发件箱地址"));
- Transport transport = session.getTransport();
- transport.connect("smtp.sina.com", "用户名", "密码");
- transport.sendMessage(msg, new Address[] {new InternetAddress("收件箱地址")});
- transport.close();
- }
- }