Send QQ email notification after Ping to Google

Time:2020-1-18

Preface

During the national day, it is said that in order to prevent people from making improper comments on major festivals, many IP addresses that can be kxsw are blocked, but when will they be unsealed? You can’t Ping them if you have nothing to do. So I wrote a regular task to Ping Google server regularly. If you Ping, you can send an email notice to see how to do it! <! –more–>

Ping tool class

                      ping www.google.com

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
* @author: zp
* @Date: 2019-10-08 11:31
* @Description:
*/
public class PingUtils {

        public static boolean ping02(String ipAddress){
                //Read row information
                String line =line = null;
                //Equivalent to CMD service
                Process exec = null;
                //Ping results
                boolean res = true;
                try {
                        exec = Runtime.getRuntime().exec("ping " + ipAddress);
                        BufferedReader br = new BufferedReader(new InputStreamReader(exec.getInputStream()));
                        //Up to three seconds
                        long endTime = System.currentTimeMillis()+3000;
                        //Test whether there is TTL string in the output line. If yes, Ping is OK
                        while ((res=true)==true&&(line = br.readLine()).indexOf("ttl")<0){
                                System.out.println("line = " + line);
                                res = false;
                                //Three seconds or Ping failure, give up trying
                                if(System.currentTimeMillis()>endTime){
                                        break;
                                }
                        }
                        System.out.println("line = " + line);
                } catch (IOException e) {
                        e.printStackTrace();
                } finally {
                        exec.destroy();
                        return res;
                }
        }

}

Tool class for sending mail

To send an email, you need to introduce two jar packages:

  • maven
<dependency>
        <groupId>javax.mail</groupId>
        <artifactId>javax.mail-api</artifactId>
        <version>1.6.2</version>
</dependency>
<dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
        <version>1.6.2</version>
</dependency>
  • gradle
compile group: 'javax.mail', name: 'javax.mail-api', version: '1.6.2'
compile group: 'com.sun.mail', name: 'javax.mail', version: '1.6.2'

It is worth noting that the email password is the client authorization code obtained after the STMP service opened by the QQ email!!! It is similar to sjhabsjhdabasf.

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
* @author: zp
* @Date: 2019-10-08 14:13
* @Description:
*/
public class MailUtils {
        public static void sendQQMail() throws AddressException, MessagingException {
                Properties properties = new Properties();
                //Connection agreement
                properties.put("mail.transport.protocol", "smtp");
                // host name
                properties.put("mail.smtp.host", "smtp.qq.com");
                // port number
                properties.put("mail.smtp.port", 465);
                properties.put("mail.smtp.auth", "true");
                //Set whether to use SSL secure connection -- generally
                properties.put("mail.smtp.ssl.enable", "true");
                //Set whether to display debug information true will display relevant information in the console
                properties.put("mail.debug", "true");
                //Get back to
                Session session = Session.getInstance(properties);
                //Get mail object
                Message message = new MimeMessage(session);
                //Set sender email address
                message.setFrom(new InternetAddress("[email protected]"));
                //Set the email address of the recipient. There can be multiple recipients
                message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("[email protected]")});
                //Set message title
                Message. Setsubject ("Ping result");
                //Set message content
                Message. Settext ("your IP is unsealed, successfully Ping through Google! "";
                //Get postman object
                Transport transport = session.getTransport();
                //Connect your own email account, password is the client authorization code after the STMP service opened for QQ email!!!
                transport.connect("[email protected]", "**********");
                //Send mail
                transport.sendMessage(message, message.getAllRecipients());
                transport.close();
        }
}

Scheduled tasks

This class is based on the schedulingconfigurer interface. After Ping, the value of cron expression will be modified to prevent repeated mail sending. (once a minute before, once a day now)

import com.example.demojpa.utils.MailUtils;
import com.example.demojpa.utils.PingUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @author: zp
* @Date: 2019-10-08 16:22:20
* @Description:
*/
@Configuration
@EnableScheduling
public class TaskBasedInterface implements SchedulingConfigurer {

        /**
        *Once an hour
        */
        private static String cron = "0 */1 * * * ?";

        @Override
        public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
                taskRegistrar.addCronTask(()-> {
                        //Test connection
                        boolean connected = PingUtils.ping02("www.google.com");
                        if(connected){
                                try {
                                        //Modify cron expression and email me every morning
                                        cron = "0 0 0 * * ? *";
                                        MailUtils.sendQQMail();
                                        Log ("Pingtong succeeded! "";
                                        return;
                                } catch (Exception e) {
                                        e.printStackTrace();
                                }
                        }
                        Log ("Ping failed");
                },cron);
        }

        public static void log(String message) {
                DateFormat df = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
                String date = df.format(new Date(System.currentTimeMillis()));
                System.out.println(date + " "+message);
        }
}

Result

Send QQ email notification after Ping to Google

Send QQ email notification after Ping to Google

Posterior language

You can really improve your interest in technology by doing some interesting things with your existing knowledge. Anyway, I have realized it.
<center>

Send QQ email notification after Ping to Google

<font size = 5>If you think it’s useful, pay attention to me</font>

</center>

Recommended Today

Taghelper under. Net core MVC

. net web developers must have stepped into the pit during the development process. They clearly modified JS files, but when they deployed them to the production environment, the customer feedback said, “it’s still wrong.”.. Then I went to the server to read the file, which has been updated. Experienced coder may have thought that […]