Java基于JavaMail实现向QQ邮箱发送邮件
最近项目在做新闻爬虫,想实现这个功能:爬虫某个页面失败后,把这个页面的 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邮箱发送邮件相关文章请关注PHP中文网!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

公司安全软件导致部分应用无法正常运行的排查与解决方法许多公司为了保障内部网络安全,会部署安全软件。...

在使用MyBatis-Plus或其他ORM框架进行数据库操作时,经常需要根据实体类的属性名构造查询条件。如果每次都手动...

系统对接中的字段映射处理在进行系统对接时,常常会遇到一个棘手的问题:如何将A系统的接口字段有效地映�...

在使用IntelliJIDEAUltimate版本启动Spring...

将姓名转换为数字以实现排序的解决方案在许多应用场景中,用户可能需要在群组中进行排序,尤其是在一个用...

Java对象与数组的转换:深入探讨强制类型转换的风险与正确方法很多Java初学者会遇到将一个对象转换成数组的�...

在使用TKMyBatis进行数据库查询时,如何优雅地获取实体类变量名以构建查询条件,是一个常见的难题。本文将针...

Redis缓存方案如何实现产品排行榜列表的需求?在开发过程中,我们常常需要处理排行榜的需求,例如展示一个�...
