java real battle: WeChat official account is sent by mail group, which contains java operation excel and java operation mysql

      After watching java in my spare time for a while, it's time to practice with practical applications. A friend needs to promote his official account by sending mail, why do I promote it by email? Everyone knows it is free. Now find a media to advertise, but it needs to invest the cost, and the final effect is not necessarily effective. E-mail is far less effective than before, but it's free after all.

      1, Demand analysis and function module positioning

      First of all, we should solve the problem that the same sender is blocked by the mail server after sending many emails. The idea is to prepare a wholesale mail account and make full use of the random method of each link to avoid the shielding of the mail server. Each sender account sends a limited number of emails every day and randomizes the starting time of sending scheduled tasks every day, Each account sends a random number of emails within the upper limit, the sending interval is generated randomly, the email title is read randomly from the database, and the email content is read randomly from the database. Anyway, everything is random within the preset range.

      After this idea is determined, the preparations are as follows: 1. Write an email sending class to be called; 2. Prepare the sender's email account, the more the better, and store it in excel table with specified format; 3. Prepare the database, store the recipient's target mailbox account, store the randomly read email title and email content, and how to generate the target mailbox. Take qq number as an example, row down one by one and understand it by yourself.

      2, Mail sending class implemented step by step

      1. Two jar packages, mail.jar and activatoron.jar, are introduced. In the idea development environment, maven management is used, and pom.xml dependency is added:

    <dependency>
      <groupId>javax.mail</groupId>
      <artifactId>mail</artifactId>
      <version>1.4.7</version>
    </dependency>

    <dependency>
      <groupId>javax.activation</groupId>
      <artifactId>activation</artifactId>
      <version>1.1.1</version>
    </dependency>

      2. SendMailSmtp class

/*
 * Class Name: SendMailSmtp
 * Description: Sending mail using smtp protocol depends on two jar packages: mail and activation; import javax.activation., import javax.mail
 *     There are four types of sending methods: send text only, send text and pictures, send text and attachments, send text, pictures and attachments, and only one attachment is supported.
 *     Using the method, first GetTransport() creates a connection with the mail server, then Sendxxxx() sends mail, and finally CloseTransport() closes the connection.
 * Date: 2021-09-03 13:30
 * Copyright: Qin Kunming
 */

package QinMing.Mail;

import java.io.FileOutputStream;
import java.util.Properties;
import java.util.Random;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

public class SendMailSmtp {

    String MailSmtpServer;
    String MailUser;
    String MailPass;
    String MailSender;
    Session session;
    Transport ts;

    public String getMailSmtpServer() {
        return MailSmtpServer;
    }

    public void setMailSmtpServer(String mailSmtpServer) {
        MailSmtpServer = mailSmtpServer;
    }

    public String getMailUser() {
        return MailUser;
    }

    public void setMailUser(String mailUser) {
        MailUser = mailUser;
    }

    public String getMailPass() {
        return MailPass;
    }

    public void setMailPass(String mailPass) {
        MailPass = mailPass;
    }

    public String getMailSender() {
        return MailSender;
    }

    public void setMailSender(String mailSender) {
        MailSender = mailSender;
    }

    /*
     *Connect to smtp mail server
     */
    public String GetTransport() {

        try {
            Properties ppt = new Properties();
            ppt.setProperty("mail.host", MailSmtpServer);
            ppt.setProperty("mail.transport.protocol", "smtp");
            ppt.setProperty("mail.smtp.auth", "true");

            //1. Create session
            session = Session.getInstance(ppt);
            //Turn on the debug mode of Session, so that you can view the running status of the Email sent by the program
            //session.setDebug(true);

            //2. Get the transport object through session
            ts = session.getTransport();

            //3. Connect to the mail server
            ts.connect(MailSmtpServer, MailUser, MailPass);

            return "success";
        }catch (MessagingException e){
            return e.toString();
        }
    }

    /*
     *Disconnect from smtp mail server
     */
    public String CloseTransport() {

        try {
            //5. Disconnect from mail server
            ts.close();
            return "success";
        }catch (MessagingException e){
            return e.toString();
        }

    }

    /*
     *Send text / html mail via smtp mail server without pictures and attachments
     * @MailReceiver addressee
     * @MailSubject Mail subject
     * @MailContent Mail content
     */
    public String SendText(String MailReceiver, String MailSubject, String MailContent){

        try {
            //4. Create a mail object and send mail
            MimeMessage mm = new MimeMessage(session);
            //Indicates the sender of the message
            mm.setFrom(new InternetAddress(MailSender));
            //Indicate the recipient of the mail. Now the sender and the recipient are the same, that is, send it to yourself
            mm.setRecipient(Message.RecipientType.TO, new InternetAddress(MailReceiver));
            //The title of the message
            mm.setSubject(MailSubject);
            //Text content of the message
            mm.setContent(MailContent, "text/html;charset=UTF-8");
            //Send mail
            ts.sendMessage(mm, mm.getAllRecipients());
            //success is returned after sending successfully
            return "success";
        }
        catch(MessagingException e){
            return e.toString();
        }

    }

    /*
     *Send text / html mail with pictures through smtp mail server
     * @MailReceiver addressee
     * @MailSubject Mail subject
     * @MailContent Mail content
     * @ImageFile Picture file
     */
    public String SendImage(String MailReceiver, String MailSubject, String MailContent, String ImageFile){

        try {
            //4. Create a mail object and send mail
            MimeMessage mm = new MimeMessage(session);
            //Indicates the sender of the message
            mm.setFrom(new InternetAddress(MailSender));
            //Indicate the recipient of the mail. Now the sender and the recipient are the same, that is, send it to yourself
            mm.setRecipient(Message.RecipientType.TO, new InternetAddress(MailReceiver));
            //The title of the message
            mm.setSubject(MailSubject);

            // Prepare mail related
            // Prepare the message body data. The specified name of the image should be consistent with that in the image.setContentID below
            MimeBodyPart text = new MimeBodyPart();
            text.setContent(MailContent + "<img src='cid:welcome.jpg'>", "text/html;charset=UTF-8");
            // Prepare picture data
            MimeBodyPart image = new MimeBodyPart();
            DataHandler dh = new DataHandler(new FileDataSource(ImageFile));
            image.setDataHandler(dh);
            image.setContentID("welcome.jpg");
            // Describe data relationships
            MimeMultipart mmt = new MimeMultipart();
            mmt.addBodyPart(text);
            mmt.addBodyPart(image);
            mmt.setSubType("related");

            mm.setContent(mmt);
            mm.saveChanges();

            //Send mail
            ts.sendMessage(mm, mm.getAllRecipients());
            //success is returned after sending successfully
            return "success";
        }
        catch(MessagingException e){
            return e.toString();
        }

    }

    /*
     *Send text / html mail with attachments through smtp mail server
     * @MailReceiver addressee
     * @MailSubject Mail subject
     * @MailContent Mail content
     * @AttachFile Attachment documents
     */
    public String SendAttach(String MailReceiver, String MailSubject, String MailContent, String AttachFile){

        try {
            //4. Create a mail object and send mail
            MimeMessage mm = new MimeMessage(session);
            //Indicates the sender of the message
            mm.setFrom(new InternetAddress(MailSender));
            //Indicate the recipient of the mail. Now the sender and the recipient are the same, that is, send it to yourself
            mm.setRecipient(Message.RecipientType.TO, new InternetAddress(MailReceiver));
            //The title of the message
            mm.setSubject(MailSubject);

            // Prepare mail related
            // Prepare the message body data. The specified name of the image should be consistent with that in the image.setContentID below
            MimeBodyPart text = new MimeBodyPart();
            text.setContent(MailContent, "text/html;charset=UTF-8");
            //Create mail attachment
            MimeBodyPart attach = new MimeBodyPart();
            DataHandler dh = new DataHandler(new FileDataSource(AttachFile));
            attach.setDataHandler(dh);
            attach.setFileName(dh.getName());
            // Describe data relationships
            MimeMultipart mmt = new MimeMultipart();
            mmt.addBodyPart(text);
            mmt.addBodyPart(attach);
            mmt.setSubType("mixed");

            mm.setContent(mmt);
            mm.saveChanges();

            //Send mail
            ts.sendMessage(mm, mm.getAllRecipients());
            //success is returned after sending successfully
            return "success";
        }
        catch(MessagingException e){
            return e.toString();
        }

    }

    /*
     *Send text / html mail with attachments through smtp mail server
     * @MailReceiver addressee
     * @MailSubject Mail subject
     * @MailContent Mail content
     * @ImageFile Picture file
     * @AttachFile Attachment documents
     */
    public String SendImageAndAttach(String MailReceiver, String MailSubject, String MailContent, String ImageFile, String AttachFile){

        try {
            //4. Create a mail object and send mail
            MimeMessage mm = new MimeMessage(session);
            //Indicates the sender of the message
            mm.setFrom(new InternetAddress(MailSender));
            //Indicate the recipient of the mail. Now the sender and the recipient are the same, that is, send it to yourself
            mm.setRecipient(Message.RecipientType.TO, new InternetAddress(MailReceiver));
            //The title of the message
            mm.setSubject(MailSubject);

            //Prepare mail related
            //text
            MimeBodyPart text = new MimeBodyPart();
            text.setContent(MailContent + "<img src='cid:welcome.jpg'>", "text/html;charset=UTF-8");

            //picture
            MimeBodyPart image = new MimeBodyPart();
            image.setDataHandler(new DataHandler(new FileDataSource(ImageFile)));
            image.setContentID("welcome.jpg");

            //enclosure
            MimeBodyPart attach = new MimeBodyPart();
            DataHandler dh = new DataHandler(new FileDataSource(AttachFile));
            attach.setDataHandler(dh);
            attach.setFileName(dh.getName());

            //Description relationship: text and picture
            MimeMultipart mmt1 = new MimeMultipart();
            mmt1.addBodyPart(text);
            mmt1.addBodyPart(image);
            mmt1.setSubType("related");

            //Description relationship: text and attachments
            MimeMultipart mmt2 = new MimeMultipart();
            mmt2.addBodyPart(attach);

            //Represents the body part of the body
            MimeBodyPart content = new MimeBodyPart();
            content.setContent(mmt1);
            mmt2.addBodyPart(content);
            mmt2.setSubType("mixed");

            mm.setContent(mmt2);
            mm.saveChanges();

            //Send mail
            ts.sendMessage(mm, mm.getAllRecipients());
            //success is returned after sending successfully
            return "success";
        }
        catch(MessagingException e){
            return e.toString();
        }

    }

}

      3. Use the above class to send test mail

      (1) Send text only messages

        //Send a text test email
        SendMailSmtp sms = new SendMailSmtp();
        sms.setMailSmtpServer("smtp.qq.com");
        sms.setMailSender("xxxx@qq.com");
        sms.setMailUser("xxxx");
        sms.setMailPass("mail pass");
        String mailcontent = "</br>honorific xxx: </br>Hello!</br>Your friend entrusts us to send you a value-added coupon. You can eat, drink and have fun. As long as you fold it up, you can enjoy it by scanning the code on wechat."
                + "</br>Here are some coupons for reference:</br></br>"
                + "<img src='http://url/test.jpg'/>";
        if(sms.GetTransport() == "success"){
            System.out.println(sms.SendText("xx@qq.com", "test mail", mailcontent));
            sms.CloseTransport();
        }

      (2) Send mail containing text and pictures

        //Send a text + picture test email
        SendMailSmtp sms = new SendMailSmtp();
        sms.setMailSmtpServer("smtp.qq.com");
        sms.setMailSender("xxxx@qq.com");
        sms.setMailUser("xxxx");
        sms.setMailPass("mail pass");
        String mailcontent = "</br>honorific xxx: </br>Hello!</br>Your friend entrusts us to send you a value-added coupon. You can eat, drink and have fun. As long as you fold it up, you can enjoy it by scanning the code on wechat."
                + "</br>Here are some coupons for reference:</br></br>"
                + "<img src='http://url/test.jpg'/>";
        if(sms.GetTransport() == "success"){
            System.out.println(sms.SendImage("xx@qq.com", "test image mail", mailcontent, "f:/test.jpg"));
            sms.CloseTransport();
        }

      (3) Send a message with text and attachments

        //Send a text + attachment test email
        SendMailSmtp sms = new SendMailSmtp();
        sms.setMailSmtpServer("smtp.qq.com");
        sms.setMailSender("xxxx@qq.com");
        sms.setMailUser("xxxx");
        sms.setMailPass("mail pass");
        String mailcontent = "</br>honorific xxx: </br>Hello!</br>Your friend entrusts us to send you a value-added coupon. You can eat, drink and have fun. As long as you fold it up, you can enjoy it by scanning the code on wechat."
                + "</br>Here are some coupons for reference:</br></br>"
                + "<img src='http://url/test.jpg'/>";
        if(sms.GetTransport() == "success"){
            System.out.println(sms.SendAttach("xx@qq.com", "test attach mail", mailcontent, "f:/test.txt"));
            sms.CloseTransport();
        }

      (4) Send mail containing text + picture + attachment

        //Send a text + picture + attachment test email
        SendMailSmtp sms = new SendMailSmtp();
        sms.setMailSmtpServer("smtp.qq.com");
        sms.setMailSender("xxxx@qq.com");
        sms.setMailUser("xxxx");
        sms.setMailPass("mail pass");
        String mailcontent = "</br>honorific xxx: </br>Hello!</br>Your friend entrusts us to send you a value-added coupon. You can eat, drink and have fun. As long as you fold it up, you can enjoy it by scanning the code on wechat."
                + "</br>Here are some coupons for reference:</br></br>"
                + "<img src='http://url/test.jpg'/>";
        if(sms.GetTransport() == "success"){
            System.out.println(sms.SendImageAndAttach("xx@qq.com", "test image and attach mail", mailcontent, "f:/test.jpg", "f:/test.txt"));
            sms.CloseTransport();
        }

      Well, the first step of sending e-mail is well prepared. The next step is how to formulate scheduled tasks and carry out mass sending. That's all for this article. For a complete application, please see the next article. Official account is what official account is idle away in seeking pleasure. The friends of the apes can use WeChat to scan the code below to pay attention to the public number, eat and play, and enjoy coupons. Everything has a minimum of ninety percent off. It is really super discount.

 

Tags: Java MySQL

Posted on Sat, 18 Sep 2021 23:51:00 -0400 by schme16