Home  >  Article  >  Java  >  Java implements sending emails to QQ mailbox based on JavaMail

Java implements sending emails to QQ mailbox based on JavaMail

高洛峰
高洛峰Original
2017-01-16 09:49:471834browse

Recently, I am working on a news crawler project, and I want to implement this function: after crawling a certain page, send the URL of this page to the mailbox. The final rendering is as follows. Filter labels, failure status codes, etc. can be added later to facilitate classification and search exceptions.

Java implements sending emails to QQ mailbox based on JavaMail

#Developers can analyze the reasons for crawler failure based on the url and stack information in the email.

Is the server down?

Or is the crawler’s Dom parsing not parsing the content?

Or is the regular expression not applicable to this page?

Turn on the SMTP service

Enable SMTP service in Settings->Account in QQ Mailbox

Java implements sending emails to QQ mailbox based on JavaMail

Note that after opening, QQ Mailbox will generate an authorization code. In the code Use this authorization code instead of the original email password to connect to the email. This avoids using clear text passwords.

Java implements sending emails to QQ mailbox based on JavaMail

I checked online for examples, and based on this article Java Mail (2): Introduction to JavaMail and sample code for sending a simple email

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();

But an error was reported

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

Because the sample code uses the 163 mailbox, and the author uses the QQ mailbox, see Log The analysis is that QQ mailbox requires SSL encryption.

Enable SSL encryption

After searching online, other mailboxes such as 163 and Sina do not require SSL encryption, so you can give up QQ mailbox.

There is also a saying on the Internet that replacing smtp.qq.com with smtp.exmail.qq.com does not require SSL encryption, but the author did not run it successfully. So let’s just add SSL encryption.

The following code enables SSL encryption

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

Successfully, the console output Log and renderings are as follows

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

Java implements sending emails to QQ mailbox based on JavaMail

Complete code example

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();
  }
}

The above is the entire content of this article, I hope it will be useful for everyone’s learning helped.

For more Java-based JavaMail implementation of sending emails to QQ mailboxes, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn