当前位置:   article > 正文

javaMail(javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection)_javaxnetsslsslexception: unrecognized ssl message,

javaxnetsslsslexception: unrecognized ssl message, plaintext connection?
  本文来自: 高爽|Coder ,原文地址: http://blog.csdn.net/ghsau/article/details/17779165 ,转载请注明。
       以这个错误信息为文章标题是不是更醒目一点,这是JavaMail使用SSL的方式登录邮箱时抛出的异常。代码如:
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class JavaMailTest1 {  
  2.     public static void main(String[] args) throws MessagingException {  
  3.         Properties props = new Properties();  
  4.         props.setProperty("mail.debug""true");  
  5.         props.setProperty("mail.smtp.auth""true");  
  6.         props.setProperty("mail.transport.protocol""smtp");  
  7.           
  8.         // SSL  
  9.         props.setProperty("mail.smtp.socketFactory.class""javax.net.ssl.SSLSocketFactory");  
  10.         props.setProperty("mail.smtp.socketFactory.fallback""false");  
  11.         props.setProperty("mail.smtp.port""465");  
  12.         props.setProperty("mail.smtp.socketFactory.port""465");  
  13.           
  14.         Session session = Session.getInstance(props);  
  15.           
  16.         Message msg = new MimeMessage(session);  
  17.         msg.setText("你好吗?");  
  18.         msg.setFrom(new InternetAddress("发件箱地址"));  
  19.           
  20.         Transport transport = session.getTransport();  
  21.         transport.connect("smtp.sina.com""用户名""密码");  
  22.         transport.sendMessage(msg, new Address[] {new InternetAddress("收件箱地址")});  
  23.         transport.close();  
  24.     }  
  25. }  
       解决该问题方式:将端口号修改成465。通常服务器的SSL端口是443,邮箱服务器中的是465。修改后,运行程序,又会出现一个新的异常:
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 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  
  2.     at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972)  
  3.     at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)  
  4.     at javax.mail.Service.connect(Service.java:295)  
  5.     at javax.mail.Service.connect(Service.java:176)  
       这通常意味着是该服务器使用的是测试证书(使用密钥工具可能产生的),而不是从一个著名的商业证书颁发机构如Verisign或GoDaddy的证书。 在此情况下,但由于JSSE( Java(TM)SecureSocketExtension )不能假定一个交互式的用户存在,它只是在默认情况下会抛出异常。解决该问题的思路就是将你要连接的SSL服务器的证书添加为JSSE受信任的证书。那如何产生证书呢?通过下面的代码(如果没有看到请刷新页面):
<a target=_blank id="L1" href="http://blog.csdn.net/ghsau/article/details/17779165#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">   1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/ghsau/article/details/17779165#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">   2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/ghsau/article/details/17779165#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">   3</a>
<a target=_blank id="L4" href="http://blog.csdn.net/ghsau/article/details/17779165#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">   4</a>
<a target=_blank id="L5" href="http://blog.csdn.net/ghsau/article/details/17779165#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">   5</a>
<a target=_blank id="L6" href="http://blog.csdn.net/ghsau/article/details/17779165#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">   6</a>
<a target=_blank id="L7" href="http://blog.csdn.net/ghsau/article/details/17779165#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">   7</a>
<a target=_blank id="L8" href="http://blog.csdn.net/ghsau/article/details/17779165#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">   8</a>
<a target=_blank id="L9" href="http://blog.csdn.net/ghsau/article/details/17779165#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">   9</a>
<a target=_blank id="L10" href="http://blog.csdn.net/ghsau/article/details/17779165#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;">  10</a>
<a target=_blank id="L11" href="http://blog.csdn.net/ghsau/article/details/17779165#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;">  11</a>
<a target=_blank id="L12" href="http://blog.csdn.net/ghsau/article/details/17779165#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;">  12</a>
<a target=_blank id="L13" href="http://blog.csdn.net/ghsau/article/details/17779165#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;">  13</a>
<a target=_blank id="L14" href="http://blog.csdn.net/ghsau/article/details/17779165#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;">  14</a>
<a target=_blank id="L15" href="http://blog.csdn.net/ghsau/article/details/17779165#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;">  15</a>
<a target=_blank id="L16" href="http://blog.csdn.net/ghsau/article/details/17779165#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;">  16</a>
<a target=_blank id="L17" href="http://blog.csdn.net/ghsau/article/details/17779165#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;">  17</a>
<a target=_blank id="L18" href="http://blog.csdn.net/ghsau/article/details/17779165#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;">  18</a>
<a target=_blank id="L19" href="http://blog.csdn.net/ghsau/article/details/17779165#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;">  19</a>
<a target=_blank id="L20" href="http://blog.csdn.net/ghsau/article/details/17779165#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;">  20</a>
<a target=_blank id="L21" href="http://blog.csdn.net/ghsau/article/details/17779165#L21" rel="#L21" style="color: rgb(102, 102, 102); text-decoration: none;">  21</a>
<a target=_blank id="L22" href="http://blog.csdn.net/ghsau/article/details/17779165#L22" rel="#L22" style="color: rgb(102, 102, 102); text-decoration: none;">  22</a>
<a target=_blank id="L23" href="http://blog.csdn.net/ghsau/article/details/17779165#L23" rel="#L23" style="color: rgb(102, 102, 102); text-decoration: none;">  23</a>
<a target=_blank id="L24" href="http://blog.csdn.net/ghsau/article/details/17779165#L24" rel="#L24" style="color: rgb(102, 102, 102); text-decoration: none;">  24</a>
<a target=_blank id="L25" href="http://blog.csdn.net/ghsau/article/details/17779165#L25" rel="#L25" style="color: rgb(102, 102, 102); text-decoration: none;">  25</a>
<a target=_blank id="L26" href="http://blog.csdn.net/ghsau/article/details/17779165#L26" rel="#L26" style="color: rgb(102, 102, 102); text-decoration: none;">  26</a>
<a target=_blank id="L27" href="http://blog.csdn.net/ghsau/article/details/17779165#L27" rel="#L27" style="color: rgb(102, 102, 102); text-decoration: none;">  27</a>
<a target=_blank id="L28" href="http://blog.csdn.net/ghsau/article/details/17779165#L28" rel="#L28" style="color: rgb(102, 102, 102); text-decoration: none;">  28</a>
<a target=_blank id="L29" href="http://blog.csdn.net/ghsau/article/details/17779165#L29" rel="#L29" style="color: rgb(102, 102, 102); text-decoration: none;">  29</a>
<a target=_blank id="L30" href="http://blog.csdn.net/ghsau/article/details/17779165#L30" rel="#L30" style="color: rgb(102, 102, 102); text-decoration: none;">  30</a>
<a target=_blank id="L31" href="http://blog.csdn.net/ghsau/article/details/17779165#L31" rel="#L31" style="color: rgb(102, 102, 102); text-decoration: none;">  31</a>
<a target=_blank id="L32" href="http://blog.csdn.net/ghsau/article/details/17779165#L32" rel="#L32" style="color: rgb(102, 102, 102); text-decoration: none;">  32</a>
<a target=_blank id="L33" href="http://blog.csdn.net/ghsau/article/details/17779165#L33" rel="#L33" style="color: rgb(102, 102, 102); text-decoration: none;">  33</a>
<a target=_blank id="L34" href="http://blog.csdn.net/ghsau/article/details/17779165#L34" rel="#L34" style="color: rgb(102, 102, 102); text-decoration: none;">  34</a>
<a target=_blank id="L35" href="http://blog.csdn.net/ghsau/article/details/17779165#L35" rel="#L35" style="color: rgb(102, 102, 102); text-decoration: none;">  35</a>
<a target=_blank id="L36" href="http://blog.csdn.net/ghsau/article/details/17779165#L36" rel="#L36" style="color: rgb(102, 102, 102); text-decoration: none;">  36</a>
<a target=_blank id="L37" href="http://blog.csdn.net/ghsau/article/details/17779165#L37" rel="#L37" style="color: rgb(102, 102, 102); text-decoration: none;">  37</a>
<a target=_blank id="L38" href="http://blog.csdn.net/ghsau/article/details/17779165#L38" rel="#L38" style="color: rgb(102, 102, 102); text-decoration: none;">  38</a>
<a target=_blank id="L39" href="http://blog.csdn.net/ghsau/article/details/17779165#L39" rel="#L39" style="color: rgb(102, 102, 102); text-decoration: none;">  39</a>
<a target=_blank id="L40" href="http://blog.csdn.net/ghsau/article/details/17779165#L40" rel="#L40" style="color: rgb(102, 102, 102); text-decoration: none;">  40</a>
<a target=_blank id="L41" href="http://blog.csdn.net/ghsau/article/details/17779165#L41" rel="#L41" style="color: rgb(102, 102, 102); text-decoration: none;">  41</a>
<a target=_blank id="L42" href="http://blog.csdn.net/ghsau/article/details/17779165#L42" rel="#L42" style="color: rgb(102, 102, 102); text-decoration: none;">  42</a>
<a target=_blank id="L43" href="http://blog.csdn.net/ghsau/article/details/17779165#L43" rel="#L43" style="color: rgb(102, 102, 102); text-decoration: none;">  43</a>
<a target=_blank id="L44" href="http://blog.csdn.net/ghsau/article/details/17779165#L44" rel="#L44" style="color: rgb(102, 102, 102); text-decoration: none;">  44</a>
<a target=_blank id="L45" href="http://blog.csdn.net/ghsau/article/details/17779165#L45" rel="#L45" style="color: rgb(102, 102, 102); text-decoration: none;">  45</a>
<a target=_blank id="L46" href="http://blog.csdn.net/ghsau/article/details/17779165#L46" rel="#L46" style="color: rgb(102, 102, 102); text-decoration: none;">  46</a>
<a target=_blank id="L47" href="http://blog.csdn.net/ghsau/article/details/17779165#L47" rel="#L47" style="color: rgb(102, 102, 102); text-decoration: none;">  47</a>
<a target=_blank id="L48" href="http://blog.csdn.net/ghsau/article/details/17779165#L48" rel="#L48" style="color: rgb(102, 102, 102); text-decoration: none;">  48</a>
<a target=_blank id="L49" href="http://blog.csdn.net/ghsau/article/details/17779165#L49" rel="#L49" style="color: rgb(102, 102, 102); text-decoration: none;">  49</a>
<a target=_blank id="L50" href="http://blog.csdn.net/ghsau/article/details/17779165#L50" rel="#L50" style="color: rgb(102, 102, 102); text-decoration: none;">  50</a>
<a target=_blank id="L51" href="http://blog.csdn.net/ghsau/article/details/17779165#L51" rel="#L51" style="color: rgb(102, 102, 102); text-decoration: none;">  51</a>
<a target=_blank id="L52" href="http://blog.csdn.net/ghsau/article/details/17779165#L52" rel="#L52" style="color: rgb(102, 102, 102); text-decoration: none;">  52</a>
<a target=_blank id="L53" href="http://blog.csdn.net/ghsau/article/details/17779165#L53" rel="#L53" style="color: rgb(102, 102, 102); text-decoration: none;">  53</a>
<a target=_blank id="L54" href="http://blog.csdn.net/ghsau/article/details/17779165#L54" rel="#L54" style="color: rgb(102, 102, 102); text-decoration: none;">  54</a>
<a target=_blank id="L55" href="http://blog.csdn.net/ghsau/article/details/17779165#L55" rel="#L55" style="color: rgb(102, 102, 102); text-decoration: none;">  55</a>
<a target=_blank id="L56" href="http://blog.csdn.net/ghsau/article/details/17779165#L56" rel="#L56" style="color: rgb(102, 102, 102); text-decoration: none;">  56</a>
<a target=_blank id="L57" href="http://blog.csdn.net/ghsau/article/details/17779165#L57" rel="#L57" style="color: rgb(102, 102, 102); text-decoration: none;">  57</a>
<a target=_blank id="L58" href="http://blog.csdn.net/ghsau/article/details/17779165#L58" rel="#L58" style="color: rgb(102, 102, 102); text-decoration: none;">  58</a>
<a target=_blank id="L59" href="http://blog.csdn.net/ghsau/article/details/17779165#L59" rel="#L59" style="color: rgb(102, 102, 102); text-decoration: none;">  59</a>
<a target=_blank id="L60" href="http://blog.csdn.net/ghsau/article/details/17779165#L60" rel="#L60" style="color: rgb(102, 102, 102); text-decoration: none;">  60</a>
<a target=_blank id="L61" href="http://blog.csdn.net/ghsau/article/details/17779165#L61" rel="#L61" style="color: rgb(102, 102, 102); text-decoration: none;">  61</a>
<a target=_blank id="L62" href="http://blog.csdn.net/ghsau/article/details/17779165#L62" rel="#L62" style="color: rgb(102, 102, 102); text-decoration: none;">  62</a>
<a target=_blank id="L63" href="http://blog.csdn.net/ghsau/article/details/17779165#L63" rel="#L63" style="color: rgb(102, 102, 102); text-decoration: none;">  63</a>
<a target=_blank id="L64" href="http://blog.csdn.net/ghsau/article/details/17779165#L64" rel="#L64" style="color: rgb(102, 102, 102); text-decoration: none;">  64</a>
<a target=_blank id="L65" href="http://blog.csdn.net/ghsau/article/details/17779165#L65" rel="#L65" style="color: rgb(102, 102, 102); text-decoration: none;">  65</a>
<a target=_blank id="L66" href="http://blog.csdn.net/ghsau/article/details/17779165#L66" rel="#L66" style="color: rgb(102, 102, 102); text-decoration: none;">  66</a>
<a target=_blank id="L67" href="http://blog.csdn.net/ghsau/article/details/17779165#L67" rel="#L67" style="color: rgb(102, 102, 102); text-decoration: none;">  67</a>
<a target=_blank id="L68" href="http://blog.csdn.net/ghsau/article/details/17779165#L68" rel="#L68" style="color: rgb(102, 102, 102); text-decoration: none;">  68</a>
<a target=_blank id="L69" href="http://blog.csdn.net/ghsau/article/details/17779165#L69" rel="#L69" style="color: rgb(102, 102, 102); text-decoration: none;">  69</a>
<a target=_blank id="L70" href="http://blog.csdn.net/ghsau/article/details/17779165#L70" rel="#L70" style="color: rgb(102, 102, 102); text-decoration: none;">  70</a>
<a target=_blank id="L71" href="http://blog.csdn.net/ghsau/article/details/17779165#L71" rel="#L71" style="color: rgb(102, 102, 102); text-decoration: none;">  71</a>
<a target=_blank id="L72" href="http://blog.csdn.net/ghsau/article/details/17779165#L72" rel="#L72" style="color: rgb(102, 102, 102); text-decoration: none;">  72</a>
<a target=_blank id="L73" href="http://blog.csdn.net/ghsau/article/details/17779165#L73" rel="#L73" style="color: rgb(102, 102, 102); text-decoration: none;">  73</a>
<a target=_blank id="L74" href="http://blog.csdn.net/ghsau/article/details/17779165#L74" rel="#L74" style="color: rgb(102, 102, 102); text-decoration: none;">  74</a>
<a target=_blank id="L75" href="http://blog.csdn.net/ghsau/article/details/17779165#L75" rel="#L75" style="color: rgb(102, 102, 102); text-decoration: none;">  75</a>
<a target=_blank id="L76" href="http://blog.csdn.net/ghsau/article/details/17779165#L76" rel="#L76" style="color: rgb(102, 102, 102); text-decoration: none;">  76</a>
<a target=_blank id="L77" href="http://blog.csdn.net/ghsau/article/details/17779165#L77" rel="#L77" style="color: rgb(102, 102, 102); text-decoration: none;">  77</a>
<a target=_blank id="L78" href="http://blog.csdn.net/ghsau/article/details/17779165#L78" rel="#L78" style="color: rgb(102, 102, 102); text-decoration: none;">  78</a>
<a target=_blank id="L79" href="http://blog.csdn.net/ghsau/article/details/17779165#L79" rel="#L79" style="color: rgb(102, 102, 102); text-decoration: none;">  79</a>
<a target=_blank id="L80" href="http://blog.csdn.net/ghsau/article/details/17779165#L80" rel="#L80" style="color: rgb(102, 102, 102); text-decoration: none;">  80</a>
<a target=_blank id="L81" href="http://blog.csdn.net/ghsau/article/details/17779165#L81" rel="#L81" style="color: rgb(102, 102, 102); text-decoration: none;">  81</a>
<a target=_blank id="L82" href="http://blog.csdn.net/ghsau/article/details/17779165#L82" rel="#L82" style="color: rgb(102, 102, 102); text-decoration: none;">  82</a>
<a target=_blank id="L83" href="http://blog.csdn.net/ghsau/article/details/17779165#L83" rel="#L83" style="color: rgb(102, 102, 102); text-decoration: none;">  83</a>
<a target=_blank id="L84" href="http://blog.csdn.net/ghsau/article/details/17779165#L84" rel="#L84" style="color: rgb(102, 102, 102); text-decoration: none;">  84</a>
<a target=_blank id="L85" href="http://blog.csdn.net/ghsau/article/details/17779165#L85" rel="#L85" style="color: rgb(102, 102, 102); text-decoration: none;">  85</a>
<a target=_blank id="L86" href="http://blog.csdn.net/ghsau/article/details/17779165#L86" rel="#L86" style="color: rgb(102, 102, 102); text-decoration: none;">  86</a>
<a target=_blank id="L87" href="http://blog.csdn.net/ghsau/article/details/17779165#L87" rel="#L87" style="color: rgb(102, 102, 102); text-decoration: none;">  87</a>
<a target=_blank id="L88" href="http://blog.csdn.net/ghsau/article/details/17779165#L88" rel="#L88" style="color: rgb(102, 102, 102); text-decoration: none;">  88</a>
<a target=_blank id="L89" href="http://blog.csdn.net/ghsau/article/details/17779165#L89" rel="#L89" style="color: rgb(102, 102, 102); text-decoration: none;">  89</a>
<a target=_blank id="L90" href="http://blog.csdn.net/ghsau/article/details/17779165#L90" rel="#L90" style="color: rgb(102, 102, 102); text-decoration: none;">  90</a>
<a target=_blank id="L91" href="http://blog.csdn.net/ghsau/article/details/17779165#L91" rel="#L91" style="color: rgb(102, 102, 102); text-decoration: none;">  91</a>
<a target=_blank id="L92" href="http://blog.csdn.net/ghsau/article/details/17779165#L92" rel="#L92" style="color: rgb(102, 102, 102); text-decoration: none;">  92</a>
<a target=_blank id="L93" href="http://blog.csdn.net/ghsau/article/details/17779165#L93" rel="#L93" style="color: rgb(102, 102, 102); text-decoration: none;">  93</a>
<a target=_blank id="L94" href="http://blog.csdn.net/ghsau/article/details/17779165#L94" rel="#L94" style="color: rgb(102, 102, 102); text-decoration: none;">  94</a>
<a target=_blank id="L95" href="http://blog.csdn.net/ghsau/article/details/17779165#L95" rel="#L95" style="color: rgb(102, 102, 102); text-decoration: none;">  95</a>
<a target=_blank id="L96" href="http://blog.csdn.net/ghsau/article/details/17779165#L96" rel="#L96" style="color: rgb(102, 102, 102); text-decoration: none;">  96</a>
<a target=_blank id="L97" href="http://blog.csdn.net/ghsau/article/details/17779165#L97" rel="#L97" style="color: rgb(102, 102, 102); text-decoration: none;">  97</a>
<a target=_blank id="L98" href="http://blog.csdn.net/ghsau/article/details/17779165#L98" rel="#L98" style="color: rgb(102, 102, 102); text-decoration: none;">  98</a>
<a target=_blank id="L99" href="http://blog.csdn.net/ghsau/article/details/17779165#L99" rel="#L99" style="color: rgb(102, 102, 102); text-decoration: none;">  99</a>
<a target=_blank id="L100" href="http://blog.csdn.net/ghsau/article/details/17779165#L100" rel="#L100" style="color: rgb(102, 102, 102); text-decoration: none;"> 100</a>
<a target=_blank id="L101" href="http://blog.csdn.net/ghsau/article/details/17779165#L101" rel="#L101" style="color: rgb(102, 102, 102); text-decoration: none;"> 101</a>
<a target=_blank id="L102" href="http://blog.csdn.net/ghsau/article/details/17779165#L102" rel="#L102" style="color: rgb(102, 102, 102); text-decoration: none;"> 102</a>
<a target=_blank id="L103" href="http://blog.csdn.net/ghsau/article/details/17779165#L103" rel="#L103" style="color: rgb(102, 102, 102); text-decoration: none;"> 103</a>
<a target=_blank id="L104" href="http://blog.csdn.net/ghsau/article/details/17779165#L104" rel="#L104" style="color: rgb(102, 102, 102); text-decoration: none;"> 104</a>
<a target=_blank id="L105" href="http://blog.csdn.net/ghsau/article/details/17779165#L105" rel="#L105" style="color: rgb(102, 102, 102); text-decoration: none;"> 105</a>
<a target=_blank id="L106" href="http://blog.csdn.net/ghsau/article/details/17779165#L106" rel="#L106" style="color: rgb(102, 102, 102); text-decoration: none;"> 106</a>
<a target=_blank id="L107" href="http://blog.csdn.net/ghsau/article/details/17779165#L107" rel="#L107" style="color: rgb(102, 102, 102); text-decoration: none;"> 107</a>
<a target=_blank id="L108" href="http://blog.csdn.net/ghsau/article/details/17779165#L108" rel="#L108" style="color: rgb(102, 102, 102); text-decoration: none;"> 108</a>
<a target=_blank id="L109" href="http://blog.csdn.net/ghsau/article/details/17779165#L109" rel="#L109" style="color: rgb(102, 102, 102); text-decoration: none;"> 109</a>
<a target=_blank id="L110" href="http://blog.csdn.net/ghsau/article/details/17779165#L110" rel="#L110" style="color: rgb(102, 102, 102); text-decoration: none;"> 110</a>
<a target=_blank id="L111" href="http://blog.csdn.net/ghsau/article/details/17779165#L111" rel="#L111" style="color: rgb(102, 102, 102); text-decoration: none;"> 111</a>
<a target=_blank id="L112" href="http://blog.csdn.net/ghsau/article/details/17779165#L112" rel="#L112" style="color: rgb(102, 102, 102); text-decoration: none;"> 112</a>
<a target=_blank id="L113" href="http://blog.csdn.net/ghsau/article/details/17779165#L113" rel="#L113" style="color: rgb(102, 102, 102); text-decoration: none;"> 113</a>
<a target=_blank id="L114" href="http://blog.csdn.net/ghsau/article/details/17779165#L114" rel="#L114" style="color: rgb(102, 102, 102); text-decoration: none;"> 114</a>
<a target=_blank id="L115" href="http://blog.csdn.net/ghsau/article/details/17779165#L115" rel="#L115" style="color: rgb(102, 102, 102); text-decoration: none;"> 115</a>
<a target=_blank id="L116" href="http://blog.csdn.net/ghsau/article/details/17779165#L116" rel="#L116" style="color: rgb(102, 102, 102); text-decoration: none;"> 116</a>
<a target=_blank id="L117" href="http://blog.csdn.net/ghsau/article/details/17779165#L117" rel="#L117" style="color: rgb(102, 102, 102); text-decoration: none;"> 117</a>
<a target=_blank id="L118" href="http://blog.csdn.net/ghsau/article/details/17779165#L118" rel="#L118" style="color: rgb(102, 102, 102); text-decoration: none;"> 118</a>
<a target=_blank id="L119" href="http://blog.csdn.net/ghsau/article/details/17779165#L119" rel="#L119" style="color: rgb(102, 102, 102); text-decoration: none;"> 119</a>
<a target=_blank id="L120" href="http://blog.csdn.net/ghsau/article/details/17779165#L120" rel="#L120" style="color: rgb(102, 102, 102); text-decoration: none;"> 120</a>
<a target=_blank id="L121" href="http://blog.csdn.net/ghsau/article/details/17779165#L121" rel="#L121" style="color: rgb(102, 102, 102); text-decoration: none;"> 121</a>
<a target=_blank id="L122" href="http://blog.csdn.net/ghsau/article/details/17779165#L122" rel="#L122" style="color: rgb(102, 102, 102); text-decoration: none;"> 122</a>
<a target=_blank id="L123" href="http://blog.csdn.net/ghsau/article/details/17779165#L123" rel="#L123" style="color: rgb(102, 102, 102); text-decoration: none;"> 123</a>
<a target=_blank id="L124" href="http://blog.csdn.net/ghsau/article/details/17779165#L124" rel="#L124" style="color: rgb(102, 102, 102); text-decoration: none;"> 124</a>
<a target=_blank id="L125" href="http://blog.csdn.net/ghsau/article/details/17779165#L125" rel="#L125" style="color: rgb(102, 102, 102); text-decoration: none;"> 125</a>
<a target=_blank id="L126" href="http://blog.csdn.net/ghsau/article/details/17779165#L126" rel="#L126" style="color: rgb(102, 102, 102); text-decoration: none;"> 126</a>
<a target=_blank id="L127" href="http://blog.csdn.net/ghsau/article/details/17779165#L127" rel="#L127" style="color: rgb(102, 102, 102); text-decoration: none;"> 127</a>
<a target=_blank id="L128" href="http://blog.csdn.net/ghsau/article/details/17779165#L128" rel="#L128" style="color: rgb(102, 102, 102); text-decoration: none;"> 128</a>
<a target=_blank id="L129" href="http://blog.csdn.net/ghsau/article/details/17779165#L129" rel="#L129" style="color: rgb(102, 102, 102); text-decoration: none;"> 129</a>
<a target=_blank id="L130" href="http://blog.csdn.net/ghsau/article/details/17779165#L130" rel="#L130" style="color: rgb(102, 102, 102); text-decoration: none;"> 130</a>
<a target=_blank id="L131" href="http://blog.csdn.net/ghsau/article/details/17779165#L131" rel="#L131" style="color: rgb(102, 102, 102); text-decoration: none;"> 131</a>
<a target=_blank id="L132" href="http://blog.csdn.net/ghsau/article/details/17779165#L132" rel="#L132" style="color: rgb(102, 102, 102); text-decoration: none;"> 132</a>
<a target=_blank id="L133" href="http://blog.csdn.net/ghsau/article/details/17779165#L133" rel="#L133" style="color: rgb(102, 102, 102); text-decoration: none;"> 133</a>
<a target=_blank id="L134" href="http://blog.csdn.net/ghsau/article/details/17779165#L134" rel="#L134" style="color: rgb(102, 102, 102); text-decoration: none;"> 134</a>
<a target=_blank id="L135" href="http://blog.csdn.net/ghsau/article/details/17779165#L135" rel="#L135" style="color: rgb(102, 102, 102); text-decoration: none;"> 135</a>
<a target=_blank id="L136" href="http://blog.csdn.net/ghsau/article/details/17779165#L136" rel="#L136" style="color: rgb(102, 102, 102); text-decoration: none;"> 136</a>
<a target=_blank id="L137" href="http://blog.csdn.net/ghsau/article/details/17779165#L137" rel="#L137" style="color: rgb(102, 102, 102); text-decoration: none;"> 137</a>
<a target=_blank id="L138" href="http://blog.csdn.net/ghsau/article/details/17779165#L138" rel="#L138" style="color: rgb(102, 102, 102); text-decoration: none;"> 138</a>
<a target=_blank id="L139" href="http://blog.csdn.net/ghsau/article/details/17779165#L139" rel="#L139" style="color: rgb(102, 102, 102); text-decoration: none;"> 139</a>
<a target=_blank id="L140" href="http://blog.csdn.net/ghsau/article/details/17779165#L140" rel="#L140" style="color: rgb(102, 102, 102); text-decoration: none;"> 140</a>
<a target=_blank id="L141" href="http://blog.csdn.net/ghsau/article/details/17779165#L141" rel="#L141" style="color: rgb(102, 102, 102); text-decoration: none;"> 141</a>
<a target=_blank id="L142" href="http://blog.csdn.net/ghsau/article/details/17779165#L142" rel="#L142" style="color: rgb(102, 102, 102); text-decoration: none;"> 142</a>
<a target=_blank id="L143" href="http://blog.csdn.net/ghsau/article/details/17779165#L143" rel="#L143" style="color: rgb(102, 102, 102); text-decoration: none;"> 143</a>
<a target=_blank id="L144" href="http://blog.csdn.net/ghsau/article/details/17779165#L144" rel="#L144" style="color: rgb(102, 102, 102); text-decoration: none;"> 144</a>
<a target=_blank id="L145" href="http://blog.csdn.net/ghsau/article/details/17779165#L145" rel="#L145" style="color: rgb(102, 102, 102); text-decoration: none;"> 145</a>
<a target=_blank id="L146" href="http://blog.csdn.net/ghsau/article/details/17779165#L146" rel="#L146" style="color: rgb(102, 102, 102); text-decoration: none;"> 146</a>
<a target=_blank id="L147" href="http://blog.csdn.net/ghsau/article/details/17779165#L147" rel="#L147" style="color: rgb(102, 102, 102); text-decoration: none;"> 147</a>
<a target=_blank id="L148" href="http://blog.csdn.net/ghsau/article/details/17779165#L148" rel="#L148" style="color: rgb(102, 102, 102); text-decoration: none;"> 148</a>
<a target=_blank id="L149" href="http://blog.csdn.net/ghsau/article/details/17779165#L149" rel="#L149" style="color: rgb(102, 102, 102); text-decoration: none;"> 149</a>
<a target=_blank id="L150" href="http://blog.csdn.net/ghsau/article/details/17779165#L150" rel="#L150" style="color: rgb(102, 102, 102); text-decoration: none;"> 150</a>
<a target=_blank id="L151" href="http://blog.csdn.net/ghsau/article/details/17779165#L151" rel="#L151" style="color: rgb(102, 102, 102); text-decoration: none;"> 151</a>
<a target=_blank id="L152" href="http://blog.csdn.net/ghsau/article/details/17779165#L152" rel="#L152" style="color: rgb(102, 102, 102); text-decoration: none;"> 152</a>
<a target=_blank id="L153" href="http://blog.csdn.net/ghsau/article/details/17779165#L153" rel="#L153" style="color: rgb(102, 102, 102); text-decoration: none;"> 153</a>
<a target=_blank id="L154" href="http://blog.csdn.net/ghsau/article/details/17779165#L154" rel="#L154" style="color: rgb(102, 102, 102); text-decoration: none;"> 154</a>
<a target=_blank id="L155" href="http://blog.csdn.net/ghsau/article/details/17779165#L155" rel="#L155" style="color: rgb(102, 102, 102); text-decoration: none;"> 155</a>
<a target=_blank id="L156" href="http://blog.csdn.net/ghsau/article/details/17779165#L156" rel="#L156" style="color: rgb(102, 102, 102); text-decoration: none;"> 156</a>
<a target=_blank id="L157" href="http://blog.csdn.net/ghsau/article/details/17779165#L157" rel="#L157" style="color: rgb(102, 102, 102); text-decoration: none;"> 157</a>
<a target=_blank id="L158" href="http://blog.csdn.net/ghsau/article/details/17779165#L158" rel="#L158" style="color: rgb(102, 102, 102); text-decoration: none;"> 158</a>
<a target=_blank id="L159" href="http://blog.csdn.net/ghsau/article/details/17779165#L159" rel="#L159" style="color: rgb(102, 102, 102); text-decoration: none;"> 159</a>
<a target=_blank id="L160" href="http://blog.csdn.net/ghsau/article/details/17779165#L160" rel="#L160" style="color: rgb(102, 102, 102); text-decoration: none;"> 160</a>
<a target=_blank id="L161" href="http://blog.csdn.net/ghsau/article/details/17779165#L161" rel="#L161" style="color: rgb(102, 102, 102); text-decoration: none;"> 161</a>
<a target=_blank id="L162" href="http://blog.csdn.net/ghsau/article/details/17779165#L162" rel="#L162" style="color: rgb(102, 102, 102); text-decoration: none;"> 162</a>
<a target=_blank id="L163" href="http://blog.csdn.net/ghsau/article/details/17779165#L163" rel="#L163" style="color: rgb(102, 102, 102); text-decoration: none;"> 163</a>
<a target=_blank id="L164" href="http://blog.csdn.net/ghsau/article/details/17779165#L164" rel="#L164" style="color: rgb(102, 102, 102); text-decoration: none;"> 164</a>
<a target=_blank id="L165" href="http://blog.csdn.net/ghsau/article/details/17779165#L165" rel="#L165" style="color: rgb(102, 102, 102); text-decoration: none;"> 165</a>
<a target=_blank id="L166" href="http://blog.csdn.net/ghsau/article/details/17779165#L166" rel="#L166" style="color: rgb(102, 102, 102); text-decoration: none;"> 166</a>
<a target=_blank id="L167" href="http://blog.csdn.net/ghsau/article/details/17779165#L167" rel="#L167" style="color: rgb(102, 102, 102); text-decoration: none;"> 167</a>
<a target=_blank id="L168" href="http://blog.csdn.net/ghsau/article/details/17779165#L168" rel="#L168" style="color: rgb(102, 102, 102); text-decoration: none;"> 168</a>
<a target=_blank id="L169" href="http://blog.csdn.net/ghsau/article/details/17779165#L169" rel="#L169" style="color: rgb(102, 102, 102); text-decoration: none;"> 169</a>
<a target=_blank id="L170" href="http://blog.csdn.net/ghsau/article/details/17779165#L170" rel="#L170" style="color: rgb(102, 102, 102); text-decoration: none;"> 170</a>
<a target=_blank id="L171" href="http://blog.csdn.net/ghsau/article/details/17779165#L171" rel="#L171" style="color: rgb(102, 102, 102); text-decoration: none;"> 171</a>
<a target=_blank id="L172" href="http://blog.csdn.net/ghsau/article/details/17779165#L172" rel="#L172" style="color: rgb(102, 102, 102); text-decoration: none;"> 172</a>
<a target=_blank id="L173" href="http://blog.csdn.net/ghsau/article/details/17779165#L173" rel="#L173" style="color: rgb(102, 102, 102); text-decoration: none;"> 173</a>
<a target=_blank id="L174" href="http://blog.csdn.net/ghsau/article/details/17779165#L174" rel="#L174" style="color: rgb(102, 102, 102); text-decoration: none;"> 174</a>
<a target=_blank id="L175" href="http://blog.csdn.net/ghsau/article/details/17779165#L175" rel="#L175" style="color: rgb(102, 102, 102); text-decoration: none;"> 175</a>
<a target=_blank id="L176" href="http://blog.csdn.net/ghsau/article/details/17779165#L176" rel="#L176" style="color: rgb(102, 102, 102); text-decoration: none;"> 176</a>
<a target=_blank id="L177" href="http://blog.csdn.net/ghsau/article/details/17779165#L177" rel="#L177" style="color: rgb(102, 102, 102); text-decoration: none;"> 177</a>
<a target=_blank id="L178" href="http://blog.csdn.net/ghsau/article/details/17779165#L178" rel="#L178" style="color: rgb(102, 102, 102); text-decoration: none;"> 178</a>
<a target=_blank id="L179" href="http://blog.csdn.net/ghsau/article/details/17779165#L179" rel="#L179" style="color: rgb(102, 102, 102); text-decoration: none;"> 179</a>
<a target=_blank id="L180" href="http://blog.csdn.net/ghsau/article/details/17779165#L180" rel="#L180" style="color: rgb(102, 102, 102); text-decoration: none;"> 180</a>
<a target=_blank id="L181" href="http://blog.csdn.net/ghsau/article/details/17779165#L181" rel="#L181" style="color: rgb(102, 102, 102); text-decoration: none;"> 181</a>
<a target=_blank id="L182" href="http://blog.csdn.net/ghsau/article/details/17779165#L182" rel="#L182" style="color: rgb(102, 102, 102); text-decoration: none;"> 182</a>
<a target=_blank id="L183" href="http://blog.csdn.net/ghsau/article/details/17779165#L183" rel="#L183" style="color: rgb(102, 102, 102); text-decoration: none;"> 183</a>
<a target=_blank id="L184" href="http://blog.csdn.net/ghsau/article/details/17779165#L184" rel="#L184" style="color: rgb(102, 102, 102); text-decoration: none;"> 184</a>
<a target=_blank id="L185" href="http://blog.csdn.net/ghsau/article/details/17779165#L185" rel="#L185" style="color: rgb(102, 102, 102); text-decoration: none;"> 185</a>
<a target=_blank id="L186" href="http://blog.csdn.net/ghsau/article/details/17779165#L186" rel="#L186" style="color: rgb(102, 102, 102); text-decoration: none;"> 186</a>
<a target=_blank id="L187" href="http://blog.csdn.net/ghsau/article/details/17779165#L187" rel="#L187" style="color: rgb(102, 102, 102); text-decoration: none;"> 187</a>
<a target=_blank id="L188" href="http://blog.csdn.net/ghsau/article/details/17779165#L188" rel="#L188" style="color: rgb(102, 102, 102); text-decoration: none;"> 188</a>
<a target=_blank id="L189" href="http://blog.csdn.net/ghsau/article/details/17779165#L189" rel="#L189" style="color: rgb(102, 102, 102); text-decoration: none;"> 189</a>
<a target=_blank id="L190" href="http://blog.csdn.net/ghsau/article/details/17779165#L190" rel="#L190" style="color: rgb(102, 102, 102); text-decoration: none;"> 190</a>
<a target=_blank id="L191" href="http://blog.csdn.net/ghsau/article/details/17779165#L191" rel="#L191" style="color: rgb(102, 102, 102); text-decoration: none;"> 191</a>
<a target=_blank id="L192" href="http://blog.csdn.net/ghsau/article/details/17779165#L192" rel="#L192" style="color: rgb(102, 102, 102); text-decoration: none;"> 192</a>
<a target=_blank id="L193" href="http://blog.csdn.net/ghsau/article/details/17779165#L193" rel="#L193" style="color: rgb(102, 102, 102); text-decoration: none;"> 193</a>
<a target=_blank id="L194" href="http://blog.csdn.net/ghsau/article/details/17779165#L194" rel="#L194" style="color: rgb(102, 102, 102); text-decoration: none;"> 194</a>
<a target=_blank id="L195" href="http://blog.csdn.net/ghsau/article/details/17779165#L195" rel="#L195" style="color: rgb(102, 102, 102); text-decoration: none;"> 195</a>
<a target=_blank id="L196" href="http://blog.csdn.net/ghsau/article/details/17779165#L196" rel="#L196" style="color: rgb(102, 102, 102); text-decoration: none;"> 196</a>
          
          
/*
* Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Sun Microsystems nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* http://blogs.sun.com/andreas/resource/InstallCert.java
* Use:
* java InstallCert hostname
* Example:
*% java InstallCert ecc.fedora.redhat.com
*/
import javax.net.ssl.* ;
import java.io.* ;
import java.security.KeyStore ;
import java.security.MessageDigest ;
import java.security.cert.CertificateException ;
import java.security.cert.X509Certificate ;
/**
* Class used to add the server's certificate to the KeyStore
* with your trusted certificates.
*/
public class InstallCert {
public static void main ( String [] args ) throws Exception {
String host ;
int port ;
char [] passphrase ;
if (( args . length == 1 ) || ( args . length == 2 )) {
String [] c = args [ 0 ]. split ( ":" );
host = c [ 0 ];
port = ( c . length == 1 ) ? 443 : Integer . parseInt ( c [ 1 ]);
String p = ( args . length == 1 ) ? "changeit" : args [ 1 ];
passphrase = p . toCharArray ();
} else {
System . out . println ( "Usage: java InstallCert <host>[:port] [passphrase]" );
return ;
}
File file = new File ( "jssecacerts" );
if ( file . isFile () == false ) {
char SEP = File . separatorChar ;
File dir = new File ( System . getProperty ( "java.home" ) + SEP
+ "lib" + SEP + "security" );
file = new File ( dir , "jssecacerts" );
if ( file . isFile () == false ) {
file = new File ( dir , "cacerts" );
}
}
System . out . println ( "Loading KeyStore " + file + "..." );
InputStream in = new FileInputStream ( file );
KeyStore ks = KeyStore . getInstance ( KeyStore . getDefaultType ());
ks . load ( in , passphrase );
in . close ();
SSLContext context = SSLContext . getInstance ( "TLS" );
TrustManagerFactory tmf =
TrustManagerFactory . getInstance ( TrustManagerFactory . getDefaultAlgorithm ());
tmf . init ( ks );
X509TrustManager defaultTrustManager = ( X509TrustManager ) tmf . getTrustManagers ()[ 0 ];
SavingTrustManager tm = new SavingTrustManager ( defaultTrustManager );
context . init ( null , new TrustManager []{ tm }, null );
SSLSocketFactory factory = context . getSocketFactory ();
System . out . println ( "Opening connection to " + host + ":" + port + "..." );
SSLSocket socket = ( SSLSocket ) factory . createSocket ( host , port );
socket . setSoTimeout ( 10000 );
try {
System . out . println ( "Starting SSL handshake..." );
socket . startHandshake ();
socket . close ();
System . out . println ();
System . out . println ( "No errors, certificate is already trusted" );
} catch ( SSLException e ) {
System . out . println ();
e . printStackTrace ( System . out );
}
X509Certificate [] chain = tm . chain ;
if ( chain == null ) {
System . out . println ( "Could not obtain server certificate chain" );
return ;
}
BufferedReader reader =
new BufferedReader ( new InputStreamReader ( System . in ));
System . out . println ();
System . out . println ( "Server sent " + chain . length + " certificate(s):" );
System . out . println ();
MessageDigest sha1 = MessageDigest . getInstance ( "SHA1" );
MessageDigest md5 = MessageDigest . getInstance ( "MD5" );
for ( int i = 0 ; i < chain . length ; i ++) {
X509Certificate cert = chain [ i ];
System . out . println
( " " + ( i + 1 ) + " Subject " + cert . getSubjectDN ());
System . out . println ( " Issuer " + cert . getIssuerDN ());
sha1 . update ( cert . getEncoded ());
System . out . println ( " sha1 " + toHexString ( sha1 . digest ()));
md5 . update ( cert . getEncoded ());
System . out . println ( " md5 " + toHexString ( md5 . digest ()));
System . out . println ();
}
System . out . println ( "Enter certificate to add to trusted keystore or 'q' to quit: [1]" );
String line = reader . readLine (). trim ();
int k ;
try {
k = ( line . length () == 0 ) ? 0 : Integer . parseInt ( line ) - 1 ;
} catch ( NumberFormatException e ) {
System . out . println ( "KeyStore not changed" );
return ;
}
X509Certificate cert = chain [ k ];
String alias = host + "-" + ( k + 1 );
ks . setCertificateEntry ( alias , cert );
OutputStream out = new FileOutputStream ( "jssecacerts" );
ks . store ( out , passphrase );
out . close ();
System . out . println ();
System . out . println ( cert );
System . out . println ();
System . out . println
( "Added certificate to keystore 'jssecacerts' using alias '"
+ alias + "'" );
}
private static final char [] HEXDIGITS = "0123456789abcdef" . toCharArray ();
private static String toHexString ( byte [] bytes ) {
StringBuilder sb = new StringBuilder ( bytes . length * 3 );
for ( int b : bytes ) {
b &= 0xff ;
sb . append ( HEXDIGITS [ b >> 4 ]);
sb . append ( HEXDIGITS [ b & 15 ]);
sb . append ( ' ' );
}
return sb . toString ();
}
private static class SavingTrustManager implements X509TrustManager {
private final X509TrustManager tm ;
private X509Certificate [] chain ;
SavingTrustManager ( X509TrustManager tm ) {
this . tm = tm ;
}
public X509Certificate [] getAcceptedIssuers () {
throw new UnsupportedOperationException ();
}
public void checkClientTrusted ( X509Certificate [] chain , String authType )
throws CertificateException {
throw new UnsupportedOperationException ();
}
public void checkServerTrusted ( X509Certificate [] chain , String authType )
throws CertificateException {
this . chain = chain ;
tm . checkServerTrusted ( chain , authType );
}
}
}
 来自CODE的代码片
InstallCert.java
        编译该类并运行,运行时要传一个参数:SSL服务器域名:端口号,如:java InstallCert smtp.sina.com:465,不输端口的话默认为443。运行之后:
[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. Loading KeyStore C:\Program Files\Java\jre7\lib\security\cacerts...  
  2. Opening connection to smtp.sina.com:465...  
  3. Starting SSL handshake...  
  4.   
  5. 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  
  6.         at sun.security.ssl.Alerts.getSSLException(Unknown Source)  
  7.         at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)  
  8.         at sun.security.ssl.Handshaker.fatalSE(Unknown Source)  
  9.         at sun.security.ssl.Handshaker.fatalSE(Unknown Source)  
  10.         at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)  
  11.         at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)  
  12.         at sun.security.ssl.Handshaker.processLoop(Unknown Source)  
  13.         at sun.security.ssl.Handshaker.process_record(Unknown Source)  
  14.         at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)  
  15.         at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)  
  16.         at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)  
  17.         at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)  
  18.         at InstallCert.main(InstallCert.java:97)  
  19. Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target  
  20.         at sun.security.validator.PKIXValidator.doBuild(Unknown Source)  
  21.         at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)  
  22.         at sun.security.validator.Validator.validate(Unknown Source)  
  23.         at sun.security.ssl.X509TrustManagerImpl.validate(Unknown Source)  
  24.         at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)  
  25.         at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)  
  26.         at InstallCert$SavingTrustManager.checkServerTrusted(InstallCert.java:192)  
  27.         at sun.security.ssl.AbstractTrustManagerWrapper.checkServerTrusted(Unknown Source)  
  28.         ... 9 more  
  29. Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target  
  30.         at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)  
  31.         at java.security.cert.CertPathBuilder.build(Unknown Source)  
  32.         ... 17 more  
  33.   
  34. Server sent 1 certificate(s):  
  35.   
  36.  1 Subject CN=*.sina.com, O="Sina.com Technology(China)Co.,ltd", L=Beijing, ST=Beijing, C=CN, SERIALNUMBER=mL/iTnzl-0Pr1rH-6U2RZH/h3zFjZxoK  
  37.    Issuer  CN=GeoTrust SSL CA, O="GeoTrust, Inc.", C=US  
  38.    sha1    43 a9 5b bc 9b 86 85 99 e3 21 63 af b0 09 78 4a 67 25 d7 a1  
  39.    md5     90 c2 45 da 67 68 cd c2 44 56 21 ef ed c6 6b 5e  
  40.   
  41. Enter certificate to add to trusted keystore or 'q' to quit: [1]  
       抛出了与我们程序中同样的错误,这里我们输入1,回车。它又输出了一堆信息,这里不粘了,这时证书已经生成了,在哪呢?在InstallCert.java所在的目录中,有一个名为jssecacerts的文件,这就是我们要的证书。将其放到我们程序所在的JSSE中,$JAVA_HOME/jre/lib/security。这时,再次运行我们的发邮件程序,发送成功。
        更新2014-01-04
       今天无意中看到了JavaMail有这样的协议支持描述:
[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. Protocol    Store or    Uses    Supports  
  2. Name        Transport?  SSL?    STARTTLS?  
  3. -------------------------------------------------  
  4. imap        Store       No      Yes  
  5. imaps       Store       Yes     N/A  
  6. pop3        Store       No      Yes  
  7. pop3s       Store       Yes     N/A  
  8. smtp        Transport   No      Yes  
  9. smtps       Transport   Yes     N/A  
       Transport使用SSL连接邮箱协议名称需要使用smtps,而不是smtp,那前面提到的程序只需这样:
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class JavaMailTest1 {  
  2.     public static void main(String[] args) throws MessagingException {  
  3.         Properties props = new Properties();  
  4.         props.setProperty("mail.debug""true");  
  5.         props.setProperty("mail.smtp.auth""true");  
  6.           
  7.         // 协议名称设置为smtps,会使用SSL  
  8.         props.setProperty("mail.transport.protocol""smtps");  
  9.           
  10.         Session session = Session.getInstance(props);  
  11.           
  12.         Message msg = new MimeMessage(session);  
  13.         msg.setText("你好吗?");  
  14.         msg.setFrom(new InternetAddress("发件箱地址"));  
  15.           
  16.         Transport transport = session.getTransport();  
  17.         transport.connect("smtp.sina.com""用户名""密码");  
  18.         transport.sendMessage(msg, new Address[] {new InternetAddress("收件箱地址")});  
  19.         transport.close();  
  20.     }  
  21. }  
       这样,JavaMail会自动使用SSL,并且使用465端口。
       参考: https://java.net/projects/javamail/pages/InstallCert   http://infposs.blogspot.com/2013/06/installcert-and-java-7.html
       本文来自: 高爽|Coder ,原文地址: http://blog.csdn.net/ghsau/article/details/17779165 ,转载请注明。
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/650951
推荐阅读
相关标签
  

闽ICP备14008679号