Home Java javaTutorial Java implements sending emails to QQ mailbox based on JavaMail

Java implements sending emails to QQ mailbox based on JavaMail

Jan 16, 2017 am 09:49 AM

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();
Copy after login

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
Copy after login

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);
Copy after login

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
Copy after login

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

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

What is the difference between memory leaks in Java programs on ARM and x86 architecture CPUs? What is the difference between memory leaks in Java programs on ARM and x86 architecture CPUs? Apr 19, 2025 pm 11:18 PM

Analysis of memory leak phenomenon of Java programs on different architecture CPUs. This article will discuss a case where a Java program exhibits different memory behaviors on ARM and x86 architecture CPUs...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to correctly divide business logic and non-business logic in hierarchical architecture in back-end development? How to correctly divide business logic and non-business logic in hierarchical architecture in back-end development? Apr 19, 2025 pm 07:15 PM

Discussing the hierarchical architecture problem in back-end development. In back-end development, common hierarchical architectures include controller, service and dao...

In Java remote debugging, how to correctly obtain constant values ​​on remote servers? In Java remote debugging, how to correctly obtain constant values ​​on remote servers? Apr 19, 2025 pm 01:54 PM

Questions and Answers about constant acquisition in Java Remote Debugging When using Java for remote debugging, many developers may encounter some difficult phenomena. It...

How to convert names to numbers to implement sorting within groups? How to convert names to numbers to implement sorting within groups? Apr 19, 2025 pm 01:57 PM

How to convert names to numbers to implement sorting within groups? When sorting users in groups, it is often necessary to convert the user's name into numbers so that it can be different...

See all articles