您的位置:首页 > 编程语言 > Java开发

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

2014-10-27 18:09 501 查看
本文来自:高爽|Coder,原文地址:/article/1421626.html,转载请注明。

以这个错误信息为文章标题是不是更醒目一点,这是JavaMail使用SSL的方式登录邮箱时抛出的异常。代码如:

[java] view
plaincopy





public class JavaMailTest1 {

public static void main(String[] args) throws MessagingException {

Properties props = new Properties();

props.setProperty("mail.debug", "true");

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

props.setProperty("mail.transport.protocol", "smtp");

// SSL

props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

props.setProperty("mail.smtp.socketFactory.fallback", "false");

props.setProperty("mail.smtp.port", "465");

props.setProperty("mail.smtp.socketFactory.port", "465");

Session session = Session.getInstance(props);

Message msg = new MimeMessage(session);

msg.setText("你好吗?");

msg.setFrom(new InternetAddress("发件箱地址"));

Transport transport = session.getTransport();

transport.connect("smtp.sina.com", "用户名", "密码");

transport.sendMessage(msg, new Address[] {new InternetAddress("收件箱地址")});

transport.close();

}

}

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

[java] view
plaincopy





javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972)

at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)

at javax.mail.Service.connect(Service.java:295)

at javax.mail.Service.connect(Service.java:176)

这通常意味着是该服务器使用的是测试证书(使用密钥工具可能产生的),而不是从一个著名的商业证书颁发机构如Verisign或GoDaddy的证书。 在此情况下,但由于JSSE(Java(TM)SecureSocketExtension)不能假定一个交互式的用户存在,它只是在默认情况下会抛出异常。解决该问题的思路就是将你要连接的SSL服务器的证书添加为JSSE受信任的证书。那如何产生证书呢?通过下面的代码(如果没有看到请刷新页面):

<a target=_blank id="L1" href="/article/1421626.html#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">   1</a>
<a target=_blank id="L2" href="/article/1421626.html#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">   2</a>
<a target=_blank id="L3" href="/article/1421626.html#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">   3</a>
<a target=_blank id="L4" href="/article/1421626.html#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">   4</a>
<a target=_blank id="L5" href="/article/1421626.html#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">   5</a>
<a target=_blank id="L6" href="/article/1421626.html#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">   6</a>
<a target=_blank id="L7" href="/article/1421626.html#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">   7</a>
<a target=_blank id="L8" href="/article/1421626.html#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">   8</a>
<a target=_blank id="L9" href="/article/1421626.html#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">   9</a>
<a target=_blank id="L10" href="/article/1421626.html#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;">  10</a>
<a target=_blank id="L11" href="/article/1421626.html#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;">  11</a>
<a target=_blank id="L12" href="/article/1421626.html#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;">  12</a>
<a target=_blank id="L13" href="/article/1421626.html#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;">  13</a>
<a target=_blank id="L14" href="/article/1421626.html#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;">  14</a>
<a target=_blank id="L15" href="/article/1421626.html#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;">  15</a>
<a target=_blank id="L16" href="/article/1421626.html#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;">  16</a>
<a target=_blank id="L17" href="/article/1421626.html#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;">  17</a>
<a target=_blank id="L18" href="/article/1421626.html#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;">  18</a>
<a target=_blank id="L19" href="/article/1421626.html#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;">  19</a>
<a target=_blank id="L20" href="/article/1421626.html#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;">  20</a>
<a target=_blank id="L21" href="/article/1421626.html#L21" rel="#L21" style="color: rgb(102, 102, 102); text-decoration: none;">  21</a>
<a target=_blank id="L22" href="/article/1421626.html#L22" rel="#L22" style="color: rgb(102, 102, 102); text-decoration: none;">  22</a>
<a target=_blank id="L23" href="/article/1421626.html#L23" rel="#L23" style="color: rgb(102, 102, 102); text-decoration: none;">  23</a>
<a target=_blank id="L24" href="/article/1421626.html#L24" rel="#L24" style="color: rgb(102, 102, 102); text-decoration: none;">  24</a>
<a target=_blank id="L25" href="/article/1421626.html#L25" rel="#L25" style="color: rgb(102, 102, 102); text-decoration: none;">  25</a>
<a target=_blank id="L26" href="/article/1421626.html#L26" rel="#L26" style="color: rgb(102, 102, 102); text-decoration: none;">  26</a>
<a target=_blank id="L27" href="/article/1421626.html#L27" rel="#L27" style="color: rgb(102, 102, 102); text-decoration: none;">  27</a>
<a target=_blank id="L28" href="/article/1421626.html#L28" rel="#L28" style="color: rgb(102, 102, 102); text-decoration: none;">  28</a>
<a target=_blank id="L29" href="/article/1421626.html#L29" rel="#L29" style="color: rgb(102, 102, 102); text-decoration: none;">  29</a>
<a target=_blank id="L30" href="/article/1421626.html#L30" rel="#L30" style="color: rgb(102, 102, 102); text-decoration: none;">  30</a>
<a target=_blank id="L31" href="/article/1421626.html#L31" rel="#L31" style="color: rgb(102, 102, 102); text-decoration: none;">  31</a>
<a target=_blank id="L32" href="/article/1421626.html#L32" rel="#L32" style="color: rgb(102, 102, 102); text-decoration: none;">  32</a>
<a target=_blank id="L33" href="/article/1421626.html#L33" rel="#L33" style="color: rgb(102, 102, 102); text-decoration: none;">  33</a>
<a target=_blank id="L34" href="/article/1421626.html#L34" rel="#L34" style="color: rgb(102, 102, 102); text-decoration: none;">  34</a>
<a target=_blank id="L35" href="/article/1421626.html#L35" rel="#L35" style="color: rgb(102, 102, 102); text-decoration: none;">  35</a>
<a target=_blank id="L36" href="/article/1421626.html#L36" rel="#L36" style="color: rgb(102, 102, 102); text-decoration: none;">  36</a>
<a target=_blank id="L37" href="/article/1421626.html#L37" rel="#L37" style="color: rgb(102, 102, 102); text-decoration: none;">  37</a>
<a target=_blank id="L38" href="/article/1421626.html#L38" rel="#L38" style="color: rgb(102, 102, 102); text-decoration: none;">  38</a>
<a target=_blank id="L39" href="/article/1421626.html#L39" rel="#L39" style="color: rgb(102, 102, 102); text-decoration: none;">  39</a>
<a target=_blank id="L40" href="/article/1421626.html#L40" rel="#L40" style="color: rgb(102, 102, 102); text-decoration: none;">  40</a>
<a target=_blank id="L41" href="/article/1421626.html#L41" rel="#L41" style="color: rgb(102, 102, 102); text-decoration: none;">  41</a>
<a target=_blank id="L42" href="/article/1421626.html#L42" rel="#L42" style="color: rgb(102, 102, 102); text-decoration: none;">  42</a>
<a target=_blank id="L43" href="/article/1421626.html#L43" rel="#L43" style="color: rgb(102, 102, 102); text-decoration: none;">  43</a>
<a target=_blank id="L44" href="/article/1421626.html#L44" rel="#L44" style="color: rgb(102, 102, 102); text-decoration: none;">  44</a>
<a target=_blank id="L45" href="/article/1421626.html#L45" rel="#L45" style="color: rgb(102, 102, 102); text-decoration: none;">  45</a>
<a target=_blank id="L46" href="/article/1421626.html#L46" rel="#L46" style="color: rgb(102, 102, 102); text-decoration: none;">  46</a>
<a target=_blank id="L47" href="/article/1421626.html#L47" rel="#L47" style="color: rgb(102, 102, 102); text-decoration: none;">  47</a>
<a target=_blank id="L48" href="/article/1421626.html#L48" rel="#L48" style="color: rgb(102, 102, 102); text-decoration: none;">  48</a>
<a target=_blank id="L49" href="/article/1421626.html#L49" rel="#L49" style="color: rgb(102, 102, 102); text-decoration: none;">  49</a>
<a target=_blank id="L50" href="/article/1421626.html#L50" rel="#L50" style="color: rgb(102, 102, 102); text-decoration: none;">  50</a>
<a target=_blank id="L51" href="/article/1421626.html#L51" rel="#L51" style="color: rgb(102, 102, 102); text-decoration: none;">  51</a>
<a target=_blank id="L52" href="/article/1421626.html#L52" rel="#L52" style="color: rgb(102, 102, 102); text-decoration: none;">  52</a>
<a target=_blank id="L53" href="/article/1421626.html#L53" rel="#L53" style="color: rgb(102, 102, 102); text-decoration: none;">  53</a>
<a target=_blank id="L54" href="/article/1421626.html#L54" rel="#L54" style="color: rgb(102, 102, 102); text-decoration: none;">  54</a>
<a target=_blank id="L55" href="/article/1421626.html#L55" rel="#L55" style="color: rgb(102, 102, 102); text-decoration: none;">  55</a>
<a target=_blank id="L56" href="/article/1421626.html#L56" rel="#L56" style="color: rgb(102, 102, 102); text-decoration: none;">  56</a>
<a target=_blank id="L57" href="/article/1421626.html#L57" rel="#L57" style="color: rgb(102, 102, 102); text-decoration: none;">  57</a>
<a target=_blank id="L58" href="/article/1421626.html#L58" rel="#L58" style="color: rgb(102, 102, 102); text-decoration: none;">  58</a>
<a target=_blank id="L59" href="/article/1421626.html#L59" rel="#L59" style="color: rgb(102, 102, 102); text-decoration: none;">  59</a>
<a target=_blank id="L60" href="/article/1421626.html#L60" rel="#L60" style="color: rgb(102, 102, 102); text-decoration: none;">  60</a>
<a target=_blank id="L61" href="/article/1421626.html#L61" rel="#L61" style="color: rgb(102, 102, 102); text-decoration: none;">  61</a>
<a target=_blank id="L62" href="/article/1421626.html#L62" rel="#L62" style="color: rgb(102, 102, 102); text-decoration: none;">  62</a>
<a target=_blank id="L63" href="/article/1421626.html#L63" rel="#L63" style="color: rgb(102, 102, 102); text-decoration: none;">  63</a>
<a target=_blank id="L64" href="/article/1421626.html#L64" rel="#L64" style="color: rgb(102, 102, 102); text-decoration: none;">  64</a>
<a target=_blank id="L65" href="/article/1421626.html#L65" rel="#L65" style="color: rgb(102, 102, 102); text-decoration: none;">  65</a>
<a target=_blank id="L66" href="/article/1421626.html#L66" rel="#L66" style="color: rgb(102, 102, 102); text-decoration: none;">  66</a>
<a target=_blank id="L67" href="/article/1421626.html#L67" rel="#L67" style="color: rgb(102, 102, 102); text-decoration: none;">  67</a>
<a target=_blank id="L68" href="/article/1421626.html#L68" rel="#L68" style="color: rgb(102, 102, 102); text-decoration: none;">  68</a>
<a target=_blank id="L69" href="/article/1421626.html#L69" rel="#L69" style="color: rgb(102, 102, 102); text-decoration: none;">  69</a>
<a target=_blank id="L70" href="/article/1421626.html#L70" rel="#L70" style="color: rgb(102, 102, 102); text-decoration: none;">  70</a>
<a target=_blank id="L71" href="/article/1421626.html#L71" rel="#L71" style="color: rgb(102, 102, 102); text-decoration: none;">  71</a>
<a target=_blank id="L72" href="/article/1421626.html#L72" rel="#L72" style="color: rgb(102, 102, 102); text-decoration: none;">  72</a>
<a target=_blank id="L73" href="/article/1421626.html#L73" rel="#L73" style="color: rgb(102, 102, 102); text-decoration: none;">  73</a>
<a target=_blank id="L74" href="/article/1421626.html#L74" rel="#L74" style="color: rgb(102, 102, 102); text-decoration: none;">  74</a>
<a target=_blank id="L75" href="/article/1421626.html#L75" rel="#L75" style="color: rgb(102, 102, 102); text-decoration: none;">  75</a>
<a target=_blank id="L76" href="/article/1421626.html#L76" rel="#L76" style="color: rgb(102, 102, 102); text-decoration: none;">  76</a>
<a target=_blank id="L77" href="/article/1421626.html#L77" rel="#L77" style="color: rgb(102, 102, 102); text-decoration: none;">  77</a>
<a target=_blank id="L78" href="/article/1421626.html#L78" rel="#L78" style="color: rgb(102, 102, 102); text-decoration: none;">  78</a>
<a target=_blank id="L79" href="/article/1421626.html#L79" rel="#L79" style="color: rgb(102, 102, 102); text-decoration: none;">  79</a>
<a target=_blank id="L80" href="/article/1421626.html#L80" rel="#L80" style="color: rgb(102, 102, 102); text-decoration: none;">  80</a>
<a target=_blank id="L81" href="/article/1421626.html#L81" rel="#L81" style="color: rgb(102, 102, 102); text-decoration: none;">  81</a>
<a target=_blank id="L82" href="/article/1421626.html#L82" rel="#L82" style="color: rgb(102, 102, 102); text-decoration: none;">  82</a>
<a target=_blank id="L83" href="/article/1421626.html#L83" rel="#L83" style="color: rgb(102, 102, 102); text-decoration: none;">  83</a>
<a target=_blank id="L84" href="/article/1421626.html#L84" rel="#L84" style="color: rgb(102, 102, 102); text-decoration: none;">  84</a>
<a target=_blank id="L85" href="/article/1421626.html#L85" rel="#L85" style="color: rgb(102, 102, 102); text-decoration: none;">  85</a>
<a target=_blank id="L86" href="/article/1421626.html#L86" rel="#L86" style="color: rgb(102, 102, 102); text-decoration: none;">  86</a>
<a target=_blank id="L87" href="/article/1421626.html#L87" rel="#L87" style="color: rgb(102, 102, 102); text-decoration: none;">  87</a>
<a target=_blank id="L88" href="/article/1421626.html#L88" rel="#L88" style="color: rgb(102, 102, 102); text-decoration: none;">  88</a>
<a target=_blank id="L89" href="/article/1421626.html#L89" rel="#L89" style="color: rgb(102, 102, 102); text-decoration: none;">  89</a>
<a target=_blank id="L90" href="/article/1421626.html#L90" rel="#L90" style="color: rgb(102, 102, 102); text-decoration: none;">  90</a>
<a target=_blank id="L91" href="/article/1421626.html#L91" rel="#L91" style="color: rgb(102, 102, 102); text-decoration: none;">  91</a>
<a target=_blank id="L92" href="/article/1421626.html#L92" rel="#L92" style="color: rgb(102, 102, 102); text-decoration: none;">  92</a>
<a target=_blank id="L93" href="/article/1421626.html#L93" rel="#L93" style="color: rgb(102, 102, 102); text-decoration: none;">  93</a>
<a target=_blank id="L94" href="/article/1421626.html#L94" rel="#L94" style="color: rgb(102, 102, 102); text-decoration: none;">  94</a>
<a target=_blank id="L95" href="/article/1421626.html#L95" rel="#L95" style="color: rgb(102, 102, 102); text-decoration: none;">  95</a>
<a target=_blank id="L96" href="/article/1421626.html#L96" rel="#L96" style="color: rgb(102, 102, 102); text-decoration: none;">  96</a>
<a target=_blank id="L97" href="/article/1421626.html#L97" rel="#L97" style="color: rgb(102, 102, 102); text-decoration: none;">  97</a>
<a target=_blank id="L98" href="/article/1421626.html#L98" rel="#L98" style="color: rgb(102, 102, 102); text-decoration: none;">  98</a>
<a target=_blank id="L99" href="/article/1421626.html#L99" rel="#L99" style="color: rgb(102, 102, 102); text-decoration: none;">  99</a>
<a target=_blank id="L100" href="/article/1421626.html#L100" rel="#L100" style="color: rgb(102, 102, 102); text-decoration: none;"> 100</a>
<a target=_blank id="L101" href="/article/1421626.html#L101" rel="#L101" style="color: rgb(102, 102, 102); text-decoration: none;"> 101</a>
<a target=_blank id="L102" href="/article/1421626.html#L102" rel="#L102" style="color: rgb(102, 102, 102); text-decoration: none;"> 102</a>
<a target=_blank id="L103" href="/article/1421626.html#L103" rel="#L103" style="color: rgb(102, 102, 102); text-decoration: none;"> 103</a>
<a target=_blank id="L104" href="/article/1421626.html#L104" rel="#L104" style="color: rgb(102, 102, 102); text-decoration: none;"> 104</a>
<a target=_blank id="L105" href="/article/1421626.html#L105" rel="#L105" style="color: rgb(102, 102, 102); text-decoration: none;"> 105</a>
<a target=_blank id="L106" href="/article/1421626.html#L106" rel="#L106" style="color: rgb(102, 102, 102); text-decoration: none;"> 106</a>
<a target=_blank id="L107" href="/article/1421626.html#L107" rel="#L107" style="color: rgb(102, 102, 102); text-decoration: none;"> 107</a>
<a target=_blank id="L108" href="/article/1421626.html#L108" rel="#L108" style="color: rgb(102, 102, 102); text-decoration: none;"> 108</a>
<a target=_blank id="L109" href="/article/1421626.html#L109" rel="#L109" style="color: rgb(102, 102, 102); text-decoration: none;"> 109</a>
<a target=_blank id="L110" href="/article/1421626.html#L110" rel="#L110" style="color: rgb(102, 102, 102); text-decoration: none;"> 110</a>
<a target=_blank id="L111" href="/article/1421626.html#L111" rel="#L111" style="color: rgb(102, 102, 102); text-decoration: none;"> 111</a>
<a target=_blank id="L112" href="/article/1421626.html#L112" rel="#L112" style="color: rgb(102, 102, 102); text-decoration: none;"> 112</a>
<a target=_blank id="L113" href="/article/1421626.html#L113" rel="#L113" style="color: rgb(102, 102, 102); text-decoration: none;"> 113</a>
<a target=_blank id="L114" href="/article/1421626.html#L114" rel="#L114" style="color: rgb(102, 102, 102); text-decoration: none;"> 114</a>
<a target=_blank id="L115" href="/article/1421626.html#L115" rel="#L115" style="color: rgb(102, 102, 102); text-decoration: none;"> 115</a>
<a target=_blank id="L116" href="/article/1421626.html#L116" rel="#L116" style="color: rgb(102, 102, 102); text-decoration: none;"> 116</a>
<a target=_blank id="L117" href="/article/1421626.html#L117" rel="#L117" style="color: rgb(102, 102, 102); text-decoration: none;"> 117</a>
<a target=_blank id="L118" href="/article/1421626.html#L118" rel="#L118" style="color: rgb(102, 102, 102); text-decoration: none;"> 118</a>
<a target=_blank id="L119" href="/article/1421626.html#L119" rel="#L119" style="color: rgb(102, 102, 102); text-decoration: none;"> 119</a>
<a target=_blank id="L120" href="/article/1421626.html#L120" rel="#L120" style="color: rgb(102, 102, 102); text-decoration: none;"> 120</a>
<a target=_blank id="L121" href="/article/1421626.html#L121" rel="#L121" style="color: rgb(102, 102, 102); text-decoration: none;"> 121</a>
<a target=_blank id="L122" href="/article/1421626.html#L122" rel="#L122" style="color: rgb(102, 102, 102); text-decoration: none;"> 122</a>
<a target=_blank id="L123" href="/article/1421626.html#L123" rel="#L123" style="color: rgb(102, 102, 102); text-decoration: none;"> 123</a>
<a target=_blank id="L124" href="/article/1421626.html#L124" rel="#L124" style="color: rgb(102, 102, 102); text-decoration: none;"> 124</a>
<a target=_blank id="L125" href="/article/1421626.html#L125" rel="#L125" style="color: rgb(102, 102, 102); text-decoration: none;"> 125</a>
<a target=_blank id="L126" href="/article/1421626.html#L126" rel="#L126" style="color: rgb(102, 102, 102); text-decoration: none;"> 126</a>
<a target=_blank id="L127" href="/article/1421626.html#L127" rel="#L127" style="color: rgb(102, 102, 102); text-decoration: none;"> 127</a>
<a target=_blank id="L128" href="/article/1421626.html#L128" rel="#L128" style="color: rgb(102, 102, 102); text-decoration: none;"> 128</a>
<a target=_blank id="L129" href="/article/1421626.html#L129" rel="#L129" style="color: rgb(102, 102, 102); text-decoration: none;"> 129</a>
<a target=_blank id="L130" href="/article/1421626.html#L130" rel="#L130" style="color: rgb(102, 102, 102); text-decoration: none;"> 130</a>
<a target=_blank id="L131" href="/article/1421626.html#L131" rel="#L131" style="color: rgb(102, 102, 102); text-decoration: none;"> 131</a>
<a target=_blank id="L132" href="/article/1421626.html#L132" rel="#L132" style="color: rgb(102, 102, 102); text-decoration: none;"> 132</a>
<a target=_blank id="L133" href="/article/1421626.html#L133" rel="#L133" style="color: rgb(102, 102, 102); text-decoration: none;"> 133</a>
<a target=_blank id="L134" href="/article/1421626.html#L134" rel="#L134" style="color: rgb(102, 102, 102); text-decoration: none;"> 134</a>
<a target=_blank id="L135" href="/article/1421626.html#L135" rel="#L135" style="color: rgb(102, 102, 102); text-decoration: none;"> 135</a>
<a target=_blank id="L136" href="/article/1421626.html#L136" rel="#L136" style="color: rgb(102, 102, 102); text-decoration: none;"> 136</a>
<a target=_blank id="L137" href="/article/1421626.html#L137" rel="#L137" style="color: rgb(102, 102, 102); text-decoration: none;"> 137</a>
<a target=_blank id="L138" href="/article/1421626.html#L138" rel="#L138" style="color: rgb(102, 102, 102); text-decoration: none;"> 138</a>
<a target=_blank id="L139" href="/article/1421626.html#L139" rel="#L139" style="color: rgb(102, 102, 102); text-decoration: none;"> 139</a>
<a target=_blank id="L140" href="/article/1421626.html#L140" rel="#L140" style="color: rgb(102, 102, 102); text-decoration: none;"> 140</a>
<a target=_blank id="L141" href="/article/1421626.html#L141" rel="#L141" style="color: rgb(102, 102, 102); text-decoration: none;"> 141</a>
<a target=_blank id="L142" href="/article/1421626.html#L142" rel="#L142" style="color: rgb(102, 102, 102); text-decoration: none;"> 142</a>
<a target=_blank id="L143" href="/article/1421626.html#L143" rel="#L143" style="color: rgb(102, 102, 102); text-decoration: none;"> 143</a>
<a target=_blank id="L144" href="/article/1421626.html#L144" rel="#L144" style="color: rgb(102, 102, 102); text-decoration: none;"> 144</a>
<a target=_blank id="L145" href="/article/1421626.html#L145" rel="#L145" style="color: rgb(102, 102, 102); text-decoration: none;"> 145</a>
<a target=_blank id="L146" href="/article/1421626.html#L146" rel="#L146" style="color: rgb(102, 102, 102); text-decoration: none;"> 146</a>
<a target=_blank id="L147" href="/article/1421626.html#L147" rel="#L147" style="color: rgb(102, 102, 102); text-decoration: none;"> 147</a>
<a target=_blank id="L148" href="/article/1421626.html#L148" rel="#L148" style="color: rgb(102, 102, 102); text-decoration: none;"> 148</a>
<a target=_blank id="L149" href="/article/1421626.html#L149" rel="#L149" style="color: rgb(102, 102, 102); text-decoration: none;"> 149</a>
<a target=_blank id="L150" href="/article/1421626.html#L150" rel="#L150" style="color: rgb(102, 102, 102); text-decoration: none;"> 150</a>
<a target=_blank id="L151" href="/article/1421626.html#L151" rel="#L151" style="color: rgb(102, 102, 102); text-decoration: none;"> 151</a>
<a target=_blank id="L152" href="/article/1421626.html#L152" rel="#L152" style="color: rgb(102, 102, 102); text-decoration: none;"> 152</a>
<a target=_blank id="L153" href="/article/1421626.html#L153" rel="#L153" style="color: rgb(102, 102, 102); text-decoration: none;"> 153</a>
<a target=_blank id="L154" href="/article/1421626.html#L154" rel="#L154" style="color: rgb(102, 102, 102); text-decoration: none;"> 154</a>
<a target=_blank id="L155" href="/article/1421626.html#L155" rel="#L155" style="color: rgb(102, 102, 102); text-decoration: none;"> 155</a>
<a target=_blank id="L156" href="/article/1421626.html#L156" rel="#L156" style="color: rgb(102, 102, 102); text-decoration: none;"> 156</a>
<a target=_blank id="L157" href="/article/1421626.html#L157" rel="#L157" style="color: rgb(102, 102, 102); text-decoration: none;"> 157</a>
<a target=_blank id="L158" href="/article/1421626.html#L158" rel="#L158" style="color: rgb(102, 102, 102); text-decoration: none;"> 158</a>
<a target=_blank id="L159" href="/article/1421626.html#L159" rel="#L159" style="color: rgb(102, 102, 102); text-decoration: none;"> 159</a>
<a target=_blank id="L160" href="/article/1421626.html#L160" rel="#L160" style="color: rgb(102, 102, 102); text-decoration: none;"> 160</a>
<a target=_blank id="L161" href="/article/1421626.html#L161" rel="#L161" style="color: rgb(102, 102, 102); text-decoration: none;"> 161</a>
<a target=_blank id="L162" href="/article/1421626.html#L162" rel="#L162" style="color: rgb(102, 102, 102); text-decoration: none;"> 162</a>
<a target=_blank id="L163" href="/article/1421626.html#L163" rel="#L163" style="color: rgb(102, 102, 102); text-decoration: none;"> 163</a>
<a target=_blank id="L164" href="/article/1421626.html#L164" rel="#L164" style="color: rgb(102, 102, 102); text-decoration: none;"> 164</a>
<a target=_blank id="L165" href="/article/1421626.html#L165" rel="#L165" style="color: rgb(102, 102, 102); text-decoration: none;"> 165</a>
<a target=_blank id="L166" href="/article/1421626.html#L166" rel="#L166" style="color: rgb(102, 102, 102); text-decoration: none;"> 166</a>
<a target=_blank id="L167" href="/article/1421626.html#L167" rel="#L167" style="color: rgb(102, 102, 102); text-decoration: none;"> 167</a>
<a target=_blank id="L168" href="/article/1421626.html#L168" rel="#L168" style="color: rgb(102, 102, 102); text-decoration: none;"> 168</a>
<a target=_blank id="L169" href="/article/1421626.html#L169" rel="#L169" style="color: rgb(102, 102, 102); text-decoration: none;"> 169</a>
<a target=_blank id="L170" href="/article/1421626.html#L170" rel="#L170" style="color: rgb(102, 102, 102); text-decoration: none;"> 170</a>
<a target=_blank id="L171" href="/article/1421626.html#L171" rel="#L171" style="color: rgb(102, 102, 102); text-decoration: none;"> 171</a>
<a target=_blank id="L172" href="/article/1421626.html#L172" rel="#L172" style="color: rgb(102, 102, 102); text-decoration: none;"> 172</a>
<a target=_blank id="L173" href="/article/1421626.html#L173" rel="#L173" style="color: rgb(102, 102, 102); text-decoration: none;"> 173</a>
<a target=_blank id="L174" href="/article/1421626.html#L174" rel="#L174" style="color: rgb(102, 102, 102); text-decoration: none;"> 174</a>
<a target=_blank id="L175" href="/article/1421626.html#L175" rel="#L175" style="color: rgb(102, 102, 102); text-decoration: none;"> 175</a>
<a target=_blank id="L176" href="/article/1421626.html#L176" rel="#L176" style="color: rgb(102, 102, 102); text-decoration: none;"> 176</a>
<a target=_blank id="L177" href="/article/1421626.html#L177" rel="#L177" style="color: rgb(102, 102, 102); text-decoration: none;"> 177</a>
<a target=_blank id="L178" href="/article/1421626.html#L178" rel="#L178" style="color: rgb(102, 102, 102); text-decoration: none;"> 178</a>
<a target=_blank id="L179" href="/article/1421626.html#L179" rel="#L179" style="color: rgb(102, 102, 102); text-decoration: none;"> 179</a>
<a target=_blank id="L180" href="/article/1421626.html#L180" rel="#L180" style="color: rgb(102, 102, 102); text-decoration: none;"> 180</a>
<a target=_blank id="L181" href="/article/1421626.html#L181" rel="#L181" style="color: rgb(102, 102, 102); text-decoration: none;"> 181</a>
<a target=_blank id="L182" href="/article/1421626.html#L182" rel="#L182" style="color: rgb(102, 102, 102); text-decoration: none;"> 182</a>
<a target=_blank id="L183" href="/article/1421626.html#L183" rel="#L183" style="color: rgb(102, 102, 102); text-decoration: none;"> 183</a>
<a target=_blank id="L184" href="/article/1421626.html#L184" rel="#L184" style="color: rgb(102, 102, 102); text-decoration: none;"> 184</a>
<a target=_blank id="L185" href="/article/1421626.html#L185" rel="#L185" style="color: rgb(102, 102, 102); text-decoration: none;"> 185</a>
<a target=_blank id="L186" href="/article/1421626.html#L186" rel="#L186" style="color: rgb(102, 102, 102); text-decoration: none;"> 186</a>
<a target=_blank id="L187" href="/article/1421626.html#L187" rel="#L187" style="color: rgb(102, 102, 102); text-decoration: none;"> 187</a>
<a target=_blank id="L188" href="/article/1421626.html#L188" rel="#L188" style="color: rgb(102, 102, 102); text-decoration: none;"> 188</a>
<a target=_blank id="L189" href="/article/1421626.html#L189" rel="#L189" style="color: rgb(102, 102, 102); text-decoration: none;"> 189</a>
<a target=_blank id="L190" href="/article/1421626.html#L190" rel="#L190" style="color: rgb(102, 102, 102); text-decoration: none;"> 190</a>
<a target=_blank id="L191" href="/article/1421626.html#L191" rel="#L191" style="color: rgb(102, 102, 102); text-decoration: none;"> 191</a>
<a target=_blank id="L192" href="/article/1421626.html#L192" rel="#L192" style="color: rgb(102, 102, 102); text-decoration: none;"> 192</a>
<a target=_blank id="L193" href="/article/1421626.html#L193" rel="#L193" style="color: rgb(102, 102, 102); text-decoration: none;"> 193</a>
<a target=_blank id="L194" href="/article/1421626.html#L194" rel="#L194" style="color: rgb(102, 102, 102); text-decoration: none;"> 194</a>
<a target=_blank id="L195" href="/article/1421626.html#L195" rel="#L195" style="color: rgb(102, 102, 102); text-decoration: none;"> 195</a>
<a target=_blank id="L196" href="/article/1421626.html#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





Loading KeyStore C:\Program Files\Java\jre7\lib\security\cacerts...

Opening connection to smtp.sina.com:465...

Starting SSL handshake...

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

at sun.security.ssl.Alerts.getSSLException(Unknown Source)

at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)

at sun.security.ssl.Handshaker.fatalSE(Unknown Source)

at sun.security.ssl.Handshaker.fatalSE(Unknown Source)

at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)

at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)

at sun.security.ssl.Handshaker.processLoop(Unknown Source)

at sun.security.ssl.Handshaker.process_record(Unknown Source)

at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)

at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)

at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)

at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)

at InstallCert.main(InstallCert.java:97)

Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

at sun.security.validator.PKIXValidator.doBuild(Unknown Source)

at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)

at sun.security.validator.Validator.validate(Unknown Source)

at sun.security.ssl.X509TrustManagerImpl.validate(Unknown Source)

at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)

at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)

at InstallCert$SavingTrustManager.checkServerTrusted(InstallCert.java:192)

at sun.security.ssl.AbstractTrustManagerWrapper.checkServerTrusted(Unknown Source)

... 9 more

Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)

at java.security.cert.CertPathBuilder.build(Unknown Source)

... 17 more

Server sent 1 certificate(s):

1 Subject CN=*.sina.com, O="Sina.com Technology(China)Co.,ltd", L=Beijing, ST=Beijing, C=CN, SERIALNUMBER=mL/iTnzl-0Pr1rH-6U2RZH/h3zFjZxoK

Issuer CN=GeoTrust SSL CA, O="GeoTrust, Inc.", C=US

sha1 43 a9 5b bc 9b 86 85 99 e3 21 63 af b0 09 78 4a 67 25 d7 a1

md5 90 c2 45 da 67 68 cd c2 44 56 21 ef ed c6 6b 5e

Enter certificate to add to trusted keystore or 'q' to quit: [1]

抛出了与我们程序中同样的错误,这里我们输入1,回车。它又输出了一堆信息,这里不粘了,这时证书已经生成了,在哪呢?在InstallCert.java所在的目录中,有一个名为jssecacerts的文件,这就是我们要的证书。将其放到我们程序所在的JSSE中,$JAVA_HOME/jre/lib/security。这时,再次运行我们的发邮件程序,发送成功。

更新2014-01-04:

今天无意中看到了JavaMail有这样的协议支持描述:

[plain] view
plaincopy





Protocol Store or Uses Supports

Name Transport? SSL? STARTTLS?

-------------------------------------------------

imap Store No Yes

imaps Store Yes N/A

pop3 Store No Yes

pop3s Store Yes N/A

smtp Transport No Yes

smtps Transport Yes N/A

Transport使用SSL连接邮箱协议名称需要使用smtps,而不是smtp,那前面提到的程序只需这样:

[java] view
plaincopy





public class JavaMailTest1 {

public static void main(String[] args) throws MessagingException {

Properties props = new Properties();

props.setProperty("mail.debug", "true");

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

// 协议名称设置为smtps,会使用SSL

props.setProperty("mail.transport.protocol", "smtps");

Session session = Session.getInstance(props);

Message msg = new MimeMessage(session);

msg.setText("你好吗?");

msg.setFrom(new InternetAddress("发件箱地址"));

Transport transport = session.getTransport();

transport.connect("smtp.sina.com", "用户名", "密码");

transport.sendMessage(msg, new Address[] {new InternetAddress("收件箱地址")});

transport.close();

}

}

这样,JavaMail会自动使用SSL,并且使用465端口。

参考:https://java.net/projects/javamail/pages/InstallCert http://infposs.blogspot.com/2013/06/installcert-and-java-7.html

本文来自:高爽|Coder,原文地址:/article/1421626.html,转载请注明。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐