使用qq邮箱发送邮件

开启 SSL 加密

网上搜了一下,其他比如163、新浪邮箱不需要 SSL 加密,可以放弃 QQ 邮箱。

网上还有种说法,把 smtp.qq.com 换成 smtp.exmail.qq.com也不需要 SSL加密,但是笔者没有run成功。所以还是老老实实加上 SSL 加密吧。

下面的代码开启了 SSL 加密

?
1
2
3
4
MailSSLSocketFactory sf = newMailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable","true");
props.put("mail.smtp.ssl.socketFactory", sf);

成功了,控制台输出 Log 和效果图如下

?
1
2
3
4
5
6
7
8
9
10
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.qq.com", port 465, isSSL true
220smtp.qq.com Esmtp QQ Mail Server
DEBUG SMTP: connected to host "smtp.qq.com", port: 465
...
 data2016-01-1917:00:44Tue
.
250Ok: queued as 
QUIT
221Bye

完整代码示例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
publicclass MailTool {
  publicstatic void main(String[] args) throwsMessagingException, GeneralSecurityException {
    Properties props = newProperties();
 
    // 开启debug调试
    props.setProperty("mail.debug","true");
    // 发送服务器需要身份验证
    props.setProperty("mail.smtp.auth","true");
    // 设置邮件服务器主机名
    props.setProperty("mail.host","smtp.qq.com");
    // 发送邮件协议名称
    props.setProperty("mail.transport.protocol","smtp");
 
    MailSSLSocketFactory sf = newMailSSLSocketFactory();
    sf.setTrustAllHosts(true);
    props.put("mail.smtp.ssl.enable","true");
    props.put("mail.smtp.ssl.socketFactory", sf);
 
    Session session = Session.getInstance(props);
 
    Message msg = newMimeMessage(session);
    msg.setSubject("seenews 错误");
    StringBuilder builder = newStringBuilder();
    builder.append("url = " + "http://blog.csdn.net/never_cxb/article/details/50524571");
    builder.append("\n页面爬虫错误");
    builder.append("\n时间 " + TimeTool.getCurrentTime());
    msg.setText(builder.toString());
    msg.setFrom(newInternetAddress("**发送人的邮箱地址**"));
 
    Transport transport = session.getTransport();
    transport.connect("smtp.qq.com","**发送人的邮箱地址**","**你的邮箱密码或者授权码**");
 
    transport.sendMessage(msg,newAddress[] { newInternetAddress("**接收人的邮箱地址**") });
    transport.close();
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值