1024programmer Java Java implements sending emails to QQ mailbox based on JavaMail

Java implements sending emails to QQ mailbox based on JavaMail

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 email. The final rendering is as follows. Filter labels, failure status codes, etc. can be added later to facilitate classification and search exceptions.

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

  • Is the server down?
  • The content is still not parsed by the crawler’s Dom parsing?
  • Or the regular expression is not applicable to this page?

Enable SMTP service

Enable SMTP service in Settings->Account in QQ mailbox

Note that after opening, QQ mailbox will generate an authorization code. Use this authorization code instead of the original mailbox password to connect to the mailbox in the code, so as to avoid using plain text passwords.

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

 // Enable debugging
 props.setProperty("mail.debug", "true");
 // Sending server requires authentication
 props.setProperty("mail.smtp.auth", "true");
 //Set the mail server hostname
 props.setProperty("mail.host", "smtp.qq.com");
 //Send email protocol name
 props.setProperty("mail.transport.protocol", "smtp");

 Session session = Session.getInstance(props);

 //Email content part
 Message msg = new MimeMessage(session);
 msg.setSubject("seenews error");
 StringBuilder builder = new StringBuilder();
 builder.append("url = " + "http://blog.csdn.net/never_cxb/article/details/50524571");
 builder.append("Page crawler error");
 builder.append("\n data " + TimeTool.getCurrentTime());
 msg.setText(builder.toString());
 //mail sender
 msg.setFrom(new InternetAddress("**Sender's email address**"));

 //send email
 Transport transport = session.getTransport();
 transport.connect("smtp.qq.com", "**Sender's email address**", "**Your email password or authorization code**");

 transport.sendMessage(msg, new Address[] { new InternetAddress("**recipient's email address**") });
 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 required(such as ssl). More information at http://service.mail.qq.com/cgi-bin/help?id=28
 

Because the example code uses the 163 mailbox, and the author uses the QQ mailbox, the Log analysis shows that the QQ mailbox requires SSL encryption.

Turn on SSL encryption

After searching online, other mailboxes such as 163 and Sina do not require SSL encryption, so you can abandon 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 I 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);
 

Successful, 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
 

Complete code example

 public class MailTool {
  public static void main(String[] args) throws MessagingException, GeneralSecurityException {
  Properties props = new Properties();

  // Enable debugging
  props.setProperty("mail.debug", "true");
  // Sending server requires authentication
  props.setProperty("mail.smtp.auth", "true");
  //Set the mail server hostname
  props.setProperty("mail.host", "smtp.qq.com");
  //Send email protocol name
  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 error");
  StringBuilder builder = new StringBuilder();
  builder.append("url = " + "http://blog.csdn.net/never_cxb/article/details/50524571");
  builder.append("\nPage crawler error");
  builder.append("\nTime " + TimeTool.getCurrentTime());
  msg.setText(builder.toString());
  msg.setFrom(new InternetAddress("**Sender's email address**"));

  Transport transport = session.getTransport();
  transport.connect("smtp.qq.com", "**Sender's email address**", "**Your email password or authorization code**");

  transport.sendMessage(msg, new Address[] { new InternetAddress("**recipient's email address**") });
  transport.close();
  }
 }
 

The above is the entire content of this article, I hope it will be helpful to everyone’s study.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/703486

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索