java通过SMTP发送QQ邮件的完全步骤

java通过SMTP发送QQ邮件的完全步骤

首先,我们需要打开QQ邮箱的SMTP服务,因为QQ邮箱对于一般的用户都是默认关闭SMTP服务的。

打开QQ邮箱,点击设置 
打开QQ邮箱,点击设置


点击帐户 
点击帐户


找到SMTP服务的选项,可以看到此处默认是关闭的,点击开启,然后腾讯会进行一些身份验证,身份验证通过以后,腾讯会给出一个用于使用SMTP的16位口令,此处这个口令一定牢记,因为后面要使用SMTP功能必须要用到这个口令,没有这个口令即使知道QQ邮箱密码也没有用,此处未给出口令的截图,毕竟为了隐私保密,不然大家都可以登录使用我的QQ邮箱SMTP服务了。后面我们将该口令记为SMTP口令。 
点击开启


接下就要开始写代码了 。 
首先,要使用java的邮箱功能需要javax.mail这个jar包,此处给出该jar包的百度盘地址。[http://pan.baidu.com/s/1dE8c0fv]


下面是发送邮件的具体java代码

<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 创建Properties 类用于记录邮箱的一些属性</span>
        Properties props <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">new</span> Properties();
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 表示SMTP发送邮件,必须进行身份验证</span>
        props<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>put(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"mail.smtp.auth"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"true"</span>);
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//此处填写SMTP服务器</span>
        props<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>put(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"mail.smtp.host"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"smtp.qq.com"</span>);
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//端口号,QQ邮箱给出了两个端口,但是另一个我一直使用不了,所以就给出这一个587</span>
        props<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>put(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"mail.smtp.port"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"587"</span>);
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 此处填写你的账号</span>
        props<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>put(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"mail.user"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"xxxxxxx@qq.com"</span>);
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 此处的密码就是前面说的16位STMP口令</span>
        props<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>put(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"mail.password"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"xxxxxxxxxxxxxxxxxxx"</span>);

        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 构建授权信息,用于进行SMTP进行身份验证</span>
        Authenticator authenticator <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">new</span> Authenticator() {

            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">protected</span> PasswordAuthentication getPasswordAuthentication() {
                <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 用户名、密码</span>
                <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String</span> userName <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> props<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>getProperty(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"mail.user"</span>);
                <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String</span> password <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> props<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>getProperty(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"mail.password"</span>);
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">new</span> PasswordAuthentication(userName, password);
            }
        };
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 使用环境属性和授权信息,创建邮件会话</span>
        Session mailSession <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> Session<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>getInstance(props, authenticator);
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 创建邮件消息</span>
        MimeMessage message <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">new</span> MimeMessage(mailSession);
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置发件人</span>
        InternetAddress form <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">new</span> InternetAddress(
                props<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>getProperty(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"mail.user"</span>));
        message<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>setFrom(form);

        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置收件人的邮箱</span>
        InternetAddress <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">new</span> InternetAddress(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"xxxxxxxx@qq.com"</span>);
        message<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>setRecipient(RecipientType<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">TO</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span>);

        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置邮件标题</span>
        message<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>setSubject(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"测试邮件"</span>);

        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置邮件的内容体</span>
        message<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>setContent(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"这是一封测试邮件"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"text/html;charset=UTF-8"</span>);

        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 最后当然就是发送邮件啦</span>
        Transport<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>send(message);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li></ul>

至此,邮件就应该能成功发出去啦。 
本文是日常开发的随手记录,如有问题,请博内留言以帮助我改正和完善,一起努力,一起学习,一起进步!如果觉得有帮助,记得赞一个哟。


Mr peter king 2016 04 29


转载地址: http://blog.csdn.net/qq422733429/article/details/51280020


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值