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="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>e-mailSystem</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

<!--Add the following code-->
    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-email</artifactId>
            <version>1.5</version>
        </dependency>
    </dependencies>

</project>

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
        email.setSslSmtpPort("465");
        //Use the fixed writing method of QQ mailbox
        email.setHostName("smtp.qq.com");
        //Set the sender's account and POP3/SMTP service password
        email.setAuthentication(fromUser, fromUserPassWord);
        //Coding mode
        email.setCharset("UTF-8");
        try {
            //addressee
            email.addTo(toUser);
            //Sender
            email.setFrom(fromUser);
            //Mail subject
            email.setSubject(theme);
            //Mail content
            email.setMsg(content);
            //send out
            email.send();
            return true;
        } catch (EmailException e) {
            e.printStackTrace();
            return false;
        }
    }
}

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

import java.io.BufferedReader;
import java.io.FileReader;
/**
 * Using custom mail sending tool class to realize mass messaging
 */
public class Test {
    public static void main(String[] args) {
        String toFrom = "xxxxxxxxxx@qq.com";         //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
        try(
             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
                if(SendMailTools.sendMail(ret,toFrom,toFromPassWord,theme,content)){
                    System.out.println("Send successfully");
                }else{
                    System.out.println("Failed!!!");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

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