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

前端技术 2023/08/31 Java

最近项目在做新闻爬虫,想实现这个功能:爬虫某个页面失败后,把这个页面的 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();
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助。

本文地址:https://www.stayed.cn/item/1037

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。