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 mailbox. 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?
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
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.
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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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

Start Spring using IntelliJIDEAUltimate version...

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

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

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? When sorting users in groups, it is often necessary to convert the user's name into numbers so that it can be different...
