本文来自: 高爽|Coder ,原文地址: http://blog.csdn.net/ghsau/article/details/17779165 ,转载请注明。
       以这个错误信息为文章标题是不是更醒目一点,这是JavaMail使用SSL的方式登录邮箱时抛出的异常。代码如:

[java] view plaincopy
  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. // SSL
  8. props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  9. props.setProperty("mail.smtp.socketFactory.fallback", "false");
  10. props.setProperty("mail.smtp.port", "465");
  11. props.setProperty("mail.smtp.socketFactory.port", "465");
  12. Session session = Session.getInstance(props);
  13. Message msg = new MimeMessage(session);
  14. msg.setText("你好吗?");
  15. msg.setFrom(new InternetAddress("发件箱地址"));
  16. Transport transport = session.getTransport();
  17. transport.connect("smtp.sina.com", "用户名", "密码");
  18. transport.sendMessage(msg, new Address[] {new InternetAddress("收件箱地址")});
  19. transport.close();
  20. }
  21. }

解决该问题方式:将端口号修改成465。通常服务器的SSL端口是443,邮箱服务器中的是465。修改后,运行程序,又会出现一个新的异常:

[java] view plaincopy
  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 plaincopy
  1. Loading KeyStore C:\Program Files\Java\jre7\lib\security\cacerts...
  2. Opening connection to smtp.sina.com:465...
  3. Starting SSL handshake...
  4. 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
  5. at sun.security.ssl.Alerts.getSSLException(Unknown Source)
  6. at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
  7. at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
  8. at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
  9. at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)
  10. at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
  11. at sun.security.ssl.Handshaker.processLoop(Unknown Source)
  12. at sun.security.ssl.Handshaker.process_record(Unknown Source)
  13. at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
  14. at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
  15. at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
  16. at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
  17. at InstallCert.main(InstallCert.java:97)
  18. Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
  19. at sun.security.validator.PKIXValidator.doBuild(Unknown Source)
  20. at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
  21. at sun.security.validator.Validator.validate(Unknown Source)
  22. at sun.security.ssl.X509TrustManagerImpl.validate(Unknown Source)
  23. at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)
  24. at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
  25. at InstallCert$SavingTrustManager.checkServerTrusted(InstallCert.java:192)
  26. at sun.security.ssl.AbstractTrustManagerWrapper.checkServerTrusted(Unknown Source)
  27. ... 9 more
  28. Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
  29. at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
  30. at java.security.cert.CertPathBuilder.build(Unknown Source)
  31. ... 17 more
  32. Server sent 1 certificate(s):
  33. 1 Subject CN=*.sina.com, O="Sina.com Technology(China)Co.,ltd", L=Beijing, ST=Beijing, C=CN, SERIALNUMBER=mL/iTnzl-0Pr1rH-6U2RZH/h3zFjZxoK
  34. Issuer  CN=GeoTrust SSL CA, O="GeoTrust, Inc.", C=US
  35. sha1    43 a9 5b bc 9b 86 85 99 e3 21 63 af b0 09 78 4a 67 25 d7 a1
  36. md5     90 c2 45 da 67 68 cd c2 44 56 21 ef ed c6 6b 5e
  37. 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 plaincopy
  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 plaincopy
  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. // 协议名称设置为smtps,会使用SSL
  7. props.setProperty("mail.transport.protocol", "smtps");
  8. Session session = Session.getInstance(props);
  9. Message msg = new MimeMessage(session);
  10. msg.setText("你好吗?");
  11. msg.setFrom(new InternetAddress("发件箱地址"));
  12. Transport transport = session.getTransport();
  13. transport.connect("smtp.sina.com", "用户名", "密码");
  14. transport.sendMessage(msg, new Address[] {new InternetAddress("收件箱地址")});
  15. transport.close();
  16. }
  17. }

这样,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 ,转载请注明。

javaMail(javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection)相关推荐

  1. javamail写邮件/邮箱验证报错javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection

    很久没更新博客了,今天记录一个问题. 我们开发了一个第三方邮箱的客户端,简单来说就是可以绑定邮箱,然后进行收发邮件.目前我还做不到像foxmail那样,用户填写了邮箱和密码,foxmail就可以自动识 ...

  2. WebSphere启动时遇到javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

    先说一下环境 一开始直接启动Eclipse,然后在Servers里启动WebSphere v9.0,遇到这个问题 SSL 接続を初期化できません. 無許可アクセスが拒否されたか.セキュリティー設定の有 ...

  3. javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection? HTTPS请求异常

    在Jmeter中压测https接口,请求抛出异常 javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection? ...

  4. jmeter 运行接口报javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection的解决方法

    今天上午,一位小伙伴(jmeter初学者)在自己抓包后,将接口放到jmeter中,进行接口测试时,出现这样的报错: javax.net.ssl.SSLException: Unrecognized S ...

  5. 政务外网部署 minio上传下载报错 javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

    起因:公司有个项目部署在客户服务器不能访问外网,使用的是http协议但是上传文件时候发现上传文件报错Unrecognized SSL message, plaintext connection? 原因 ...

  6. Unrecognized SSL message, plaintext connection?

    前言 昨天快下班了,一同事在群里发了一个异常日志,内容如下 发送邮件失败:{}javax.net.ssl.SSLException: Unrecognized SSL message, plainte ...

  7. 【Java异常】Unrecognized SSL message, plaintext connection?

    最近在项目开发过程中,使用sslsocket通信在测试中出现了Unrecognized SSL message, plaintext connection? 错误,写个博客记录一下 网上查询了这个问题 ...

  8. javamail发送SSL---------Unrecognized SSL message, plaintext connection

    Properties props = new Properties();props.setProperty("mail.smtp.auth", "true"); ...

  9. javamail发送SSL邮件报报Unrecognized SSL message, plaint

    2019独角兽企业重金招聘Python工程师标准>>> 发送邮件报如下错误.smtp服务器是smtp.live.com,有要求安全连接(SSL) 2009-04-27 10:35:3 ...

最新文章

  1. 计算机艺术未来发展趋势,计算机技术对现代艺术设计的影响
  2. python是什么编程教程-一个新手,什么编程都没学过怎么学好Python?
  3. python爬虫知识大全_Python爬虫知识点一
  4. 【Linux网络编程】并发服务器之多进程模型
  5. leetcode 4 --- 寻找两个有序数组的中位数
  6. linux shell的sleep指定延时单位
  7. PAT (Basic Level) Practice1014 福尔摩斯的约会
  8. PHPExcel 在LINUX环境中导出乱码的问题解决
  9. iPad,下载迅雷电影,迅雷HD出现“应版权方要求,文件无法下载”解决方法!
  10. web前端——常用的标签
  11. 提供一套基于SpringBoot-shiro-vue的权限管理思路.
  12. 300ETF期权和50ETF期权的区别
  13. 微信android登录协议分析,手机微信协议分析,请教··
  14. 萌新,C++、Java、Python 选择哪个好?
  15. Java面试——消息队列
  16. 什么叫计算机网络虫洞,虫洞:危险与希望并存
  17. java (零钱通)
  18. Jmeter javax.swing.text.BadLocationException: Position not represented by view 解决方法
  19. MeteoInfoLab脚本示例:获取气团轨迹每个节点的气象数据
  20. 笔记本计算机在桌面显示器,解决笔记本电脑屏幕出现条纹的五大方法

热门文章

  1. APP运行时Crash自动修复系统
  2. 一个最简单的Delphi2010的PNG异形窗口方法
  3. mysql怎么放入图片_怎么将图片添加到mysql中
  4. 用脑电波控制智能假肢:如何利用深度学习技术进行EGG数据分类
  5. 想要成为一名合格的数据分析师,需要学习哪些类型的书
  6. 包装exp是什么意思_包装上exp是什么意思?
  7. 微信小程登录功能和获取手机号
  8. win10计算机丢失msvcr,计算机中丢失msvcr110.dll怎么办?Win10系统中丢失msvcr110.dll解决方法...
  9. html dashed 属性,css虚线样式dotted和dashed
  10. php虚线_实现css虚线样式的两种方式:dotted和dashed(实例)