search
HomeJavajavaTutorialJava 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 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
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...

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...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

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

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software