Java 基于JavaMail实现向QQ邮箱发送邮件(未测试)

1

需求

最近项目在做新闻爬虫,想实现这个功能:爬虫某个页面失败后,把这个页面的 url 发到笔者的邮箱。最终实现的效果图如下,后期可以加上过滤标签、失败状态码等,方便分类搜索异常。

blog.csdn.net/never_cxb

开发人员可以根据邮件里的 url 和堆栈信息,分析爬虫失败的原因。

  • 是不是服务器 down 了?
  • 还是爬虫的 Dom 解析没有解析到内容?
  • 还是正则表达式对于这个页面不适用?

开启SMTP服务

在 QQ 邮箱里的 设置->账户里开启 SMTP 服务

blog.csdn.net/never_cxb

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

blog.csdn.net/never_cxb

下载依赖的 jar 包

官方下载地址 http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-eeplat-419426.html#javamail-1.4.7-oth-JPR
解压完之后,通常我们只需要其中的mail.jar,把它加到我们 java 工程的依赖包中。

代码

网上查了一下例子,根据这篇文章 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

blog.csdn.net/never_cxb

完整代码示例

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();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值