Java implementation of QQ mail group sending function

Posted by belick on Tue, 04 Jan 2022 13:53:45 +0100

In life, when we write java code, we usually try Catch wraps around the code block where exceptions may occur. However, if a java project is too large and a bug occurs at the same time, it is not conducive for us to check the error.

So we can use email, write an email tool class ourselves, and use try During catch processing, you can use the mail class to send an email to the specified mailbox every time a problem is found.

In addition to the above usage of e-mail tools, we can also write a QQ e-mail group.

First, you need to write out the mail tool class:

Here, using IDEA, first create a Maven project:

Then, a project directory will appear. Find POM XML file, open it, and add a piece of code in it:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""



<!--Add the following code-->


When the code is added, you can write your own mail sending tool class.

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
 * Custom mail sending tool class
public class SendMailTools {
     * @param toUser    Receiving mailbox
     * @param fromUser  Email address
     * @param fromUserPassWord  POP3/SMTP service password for outgoing mailbox
     * @param theme     Mail subject
     * @param content   Mail content
     * @return    Returns a prompt indicating whether the message was sent successfully (true/false)
    public static boolean sendMail(String toUser, String fromUser,
                                   String fromUserPassWord, String theme, 
                                   String content){
        //Instantiate mail resources
        SimpleEmail email = new SimpleEmail();
        //Set the port number of qq mail server
        //Use the fixed writing method of QQ mailbox
        //Set the sender's account and POP3/SMTP service password
        email.setAuthentication(fromUser, fromUserPassWord);
        //Coding mode
        try {
            //Mail subject
            //Mail content
            //send out
            return true;
        } catch (EmailException e) {
            return false;

Finally, it is to test the use of our mail tool class.

 * Using custom mail sending tool class to realize mass messaging
public class Test {
    public static void main(String[] args) {
        String toFrom = "";         //Sender
        String toFromPassWord = "xxxxxxxxxxxxxxxx";  //Sender mailbox POP3/SMTP service password
        String theme = "This is a test email";             //Mail subject
        String content = "hello word!~~~";           //Email details
             FileReader fs = new FileReader("toUser.txt");//Character stream, the default is the text file under the current project
             BufferedReader br = new BufferedReader(fs);  //Character decoration flow
            String ret = null;
            //Cyclic reading, one line at a time
            while((ret = br.readLine())!=null){
                //Send a prompt for success
                    System.out.println("Send successfully");
        }catch (Exception e){

What I use here is to read the recipient's mailbox from a txt text file and send it. The sender is fixed. When you are writing this code, those who are interested can also read the sender in the form of a file, and you can also read the subject and content in the form of a file and send it.

Program running results:

Note: since we use the service area of QQ mailbox here, it may be sent successfully at the same time, but some recipients may not receive it. To avoid this Bug, there are two methods:

1. Minimize the number of emails sent at the same time

2. Use your own server or a third-party server

Finally, where is the mailbox POP3/SMTP service password?

Topics: Java Back-end