`

javamail 关于SSL的问题找了我一个星期之久

阅读更多

关于javamail 使用SSl方式发送邮件的问题。我在官网上面终于找到答案了。

一直连接不上stmp.**.com:465的服务器时因为需要证书,而网上提供的生成证书的一个..java根本就没有用。

最后在官网上面看到了这段话。

 

In earlier releases it was necessary to explicitly set a socket
factory property to enable use of SSL.  In almost all cases, this
is no longer necessary.  SSL support is built in.  However, there
is one case where a special socket factory may be needed.

JavaMail now includes a special SSL socket factory that can simplify
dealing with servers with self-signed certificates.  While the
recommended approach is to include the certificate in your keystore
as described above, the following approach may be simpler in some cases.

The class com.sun.mail.util.MailSSLSocketFactory can be used as a
simple socket factory that allows trusting all hosts or a specific set
of hosts.  For example:

    MailSSLSocketFactory sf = new MailSSLSocketFactory();
    sf.setTrustAllHosts(true);
    // or
    // sf.setTrustedHosts(new String[] { "my-server" });
    props.put("mail.smtp.ssl.enable", "true");
    // also use following for additional safety
    //props.put("mail.smtp.ssl.checkserveridentity", "true");
    props.put("mail.smtp.ssl.socketFactory", sf);

Use of MailSSLSocketFactory avoids the need to add the certificate to
your keystore as described above, or configure your own TrustManager
as described below.(使用MailSSLSocketFactory避免了需要添加证书,你的密钥库如上所述,或配置自己的TrustManager。如下所述。

 实在是高兴啊。使用这个方法后马上使用SSL发送邮件的方式,发送成功了

官网还提供了一个自己实现的加密管理类,可以使用。在这里我就不贴上来了。直接上官网的地址。

http://www.oracle.com/technetwork/java/javamail145sslnotes-1562622.html

 

本人英语水凭能力不行,不能翻译。如果英语水平好。翻译出来,绝对是一个很好的资源。

 

分享到:
评论
1 楼 小雪椰 2015-04-27  
请问一下 MailSSLSocketFactory是在那个包里面? 要下JAF吗?

相关推荐

Global site tag (gtag.js) - Google Analytics