发送邮件的时候首先报错ssl shakehands失败,报错没有找到可用的证书,网上搜到相关的靠谱的方案是
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
} catch (GeneralSecurityException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
sf.setTrustAllHosts(true);
// pro.put("mail.smtp.ssl.enable", "true");
// pro.put("mail.smtp.ssl.trust", "*"); // 不影响
pro.put("mail.smtp.ssl.socketFactory", sf); //这句加上后才发送成功,不加的话就发送不成功
pro.put("mail.smtp.socketFactory.fallback", "false");
后来网上搜到通过InstallCert.java来从服务器上下载证书,下载的过程是编译InstallCert.java文件,然后运行java InstallCert SMTP.partner.outlook.cn:587 会提示server发送过来几个证书,输入选项下载对应的证书。
根据这个方法下载证书的时候,发现会有报错如下:
javax.net.ssl.SSLException: java.lang.UnsupportedOperationException
at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1886)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1844)
at sun.security.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1827)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1346)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323)
at org.vangen.auth.InstallCert.main(InstallCert.java:81)
Caused by: java.lang.UnsupportedOperationException
at org.vangen.auth.InstallCert$SavingTrustManager.getAcceptedIssuers(InstallCert.java:168)
at sun.security.ssl.AbstractTrustManagerWrapper.checkAlgorithmConstraints(SSLContextImpl.java:926)
at sun.security.ssl.AbstractTrustManagerWrapper.checkAdditionalTrust(SSLContextImpl.java:872)
at sun.security.ssl.AbstractTrustManagerWrapper.checkServerTrusted(SSLContextImpl.java:814)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1323)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:153)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:868)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:804)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1016)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
... 2 more
后来搜到
http://my.oschina.net/uniquejava/blog/377514这里说名是要使用1.5或者1.6的jdk才可以,1.7的就会报错这个,换用1.6的jdk后果然就不再报错这个了。但是还会继续报错
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2249)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1740)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1239)
at javax.mail.Transport.send0(Transport.java:255)
at javax.mail.Transport.send(Transport.java:124)
at com.pello.mail.SimpleMailSender.sendTextMail(SimpleMailSender.java:80)
at com.pello.mail.SendMailDemo.main(SendMailDemo.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
然后调试的时候发现是没有开启tls,开启后就发送成功了。
这个时候的我就发现比较神奇了,难道真的是下载下来的证书才可以,开启了各种调试模式,最终发现并不是下载下来的证书有什么用,而是我为了下载证书,把jdk换成了1.6版本的原因,我之前添加设置的时候由于jdk是1.7的,所以没有发送成功。
最终的结果就是,原来就是最开始加的那部分设置就可以了,不过在jdk1.7下不行,要在jdk1.6下才可以,这真是一个忧伤的故事。
不管如何,问题搞定了,网上也搜了很多,完全没有看到有关提到jdk版本的说明,这里做个记录吧。
InstallCert.java文件见附件。
把本地调试的发邮件的代码也附件传一下吧