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

Java基于JavaMail实现向QQ邮箱发送邮件

2016-08-17 11:20 866 查看
作者:学习编程知识字体:[增加 减小]
类型:转载时间:2016-01-20 我要评论
这篇文章主要介绍了Java基于JavaMail实现向QQ邮箱发送邮件的相关资料,需要的朋友可以参考下
最近项目在做新闻爬虫,想实现这个功能:爬虫某个页面失败后,把这个页面的 url 发到邮箱。最终实现的效果图如下,后期可以加上过滤标签、失败状态码等,方便分类搜索异常。

开发人员可以根据邮件里的 url和堆栈信息,分析爬虫失败的原因。
1.   是不是服务器 down
了?

2.   还是爬虫的 Dom
解析没有解析到内容?

3.   还是正则表达式对于这个页面不适用?
开启SMTP服务
在 QQ 邮箱里的 设置->账户里开启SMTP 服务

注意开启完之后,QQ 邮箱会生成一个授权码,在代码里连接邮箱使用这个授权码而不是原始的邮箱密码,这样可以避免使用明文密码。

网上查了一下例子,根据这篇文章Java Mail(二):JavaMail介绍及发送一封简单邮件 的示例代码。

Properties props = new Properties();
 
// 开启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");
 
Session session = Session.getInstance(props);
 
//邮件内容部分
Message msg = new MimeMessage(session);
msg.setSubject("seenews 错误");
StringBuilder builder = new StringBuilder();
builder.append("url = " +"http://blog.csdn.net/never_cxb/article/details/50524571");
builder.append("页面爬虫错误");
builder.append("\n data " +TimeTool.getCurrentTime());
msg.setText(builder.toString());
//邮件发送者
msg.setFrom(new InternetAddress("**发送人的邮箱地址**"));
 
//发送邮件
Transport transport = session.getTransport();
transport.connect("smtp.qq.com","**发送人的邮箱地址**", "**你的邮箱密码或者授权码**");
 
transport.sendMessage(msg, new Address[] {new InternetAddress("**接收人的邮箱地址**") });
transport.close();
 

但是报错了

DEBUG SMTP: AUTH LOGIN command tracesuppressed
DEBUG SMTP: AUTH LOGIN failed
Exception in thread "main"javax.mail.AuthenticationFailedException: 530

Error: A secure connection is requiered(suchas ssl). More information at http://service.mail.qq.com/cgi-bin/help?id=28  

因为示例代码是用的163邮箱,而笔者是 QQ 邮箱,看 Log分析是 QQ 邮箱需要 SSL加密。
开启 SSL 加密
网上搜了一下,其他比如163、新浪邮箱不需要 SSL加密,可以放弃 QQ 邮箱。
网上还有种说法,把smtp.qq.com 换成 smtp.exmail.qq.com也不需要 SSL加密,但是笔者没有run成功。所以还是老老实实加上 SSL 加密吧。
下面的代码开启了 SSL加密

MailSSLSocketFactory sf = newMailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable","true");
props.put("mail.smtp.ssl.socketFactory",sf);
 

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

DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host"smtp.qq.com", port 465, isSSL true
220 smtp.qq.com Esmtp QQ Mail Server
DEBUG SMTP: connected to host"smtp.qq.com", port: 465
...
 data2016-01-19 17:00:44 Tue
.
250 Ok: queued as
QUIT
221 Bye
 

完整代码示例

public class MailTool {
 public static void main(String[] args) throws MessagingException,GeneralSecurityException {
   Properties props = new Properties();
 
    // 开启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 = new MailSSLSocketFactory();
   sf.setTrustAllHosts(true);
   props.put("mail.smtp.ssl.enable", "true");
    props.put("mail.smtp.ssl.socketFactory",sf);
 
   Session session = Session.getInstance(props);
 
   Message msg = new MimeMessage(session);
   msg.setSubject("seenews 错误");
   StringBuilder builder = new StringBuilder();
   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(new InternetAddress("**发送人的邮箱地址**"));
 
   Transport transport = session.getTransport();
   transport.connect("smtp.qq.com", "**发送人的邮箱地址**", "**你的邮箱密码或者授权码**");
 
   transport.sendMessage(msg, new Address[] { new InternetAddress("**接收人的邮箱地址**") });
   transport.close();
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助。
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: