最近项目在做新闻爬虫,想实现这个功能:爬虫某个页面失败后,把这个页面的 url 发到邮箱。最终实现的效果图如下,后期可以加上过滤标签、失败状态码等,方便分类搜索异常。
开发人员可以根据邮件里的 url 和堆栈信息,分析爬虫失败的原因。
是不是服务器 down 了?
还是爬虫的 dom 解析没有解析到内容?
还是正则表达式对于这个页面不适用?
开启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 trace suppressed
debug smtp: auth login failed
exception in thread "main" javax.mail.authenticationfailedexception: 530
error: a secure connection is requiered(such as 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 = new mailsslsocketfactory();
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
...
data 2016-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();
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助。
更多java基于javamail实现向qq邮箱发送邮件。