经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
ping通谷歌后发送QQ邮件通知
来源:cnblogs  作者:糟糕的艺术家  时间:2019/10/9 11:54:04  对本文有异议

前言

国庆期间,据说是为了防止有人在重大节日发表不正当言论,很多可以kxsw的ip都被封了,可是什么时候才会解封呢,不能没事就去ping一下吧,所以我写了个定时任务,定时ping谷歌服务器,如果ping通则发邮件通知,来看看是怎么做的吧!

Ping工具类

首先保证你的电脑之前是可以访问谷歌的(shadowsocks),这个类是专门用来ping谷歌的,相当于手动输入ping www.google.com

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. /**
  5. * @author: zp
  6. * @Date: 2019-10-08 11:31
  7. * @Description:
  8. */
  9. public class PingUtils {
  10. public static boolean ping02(String ipAddress){
  11. // 读取的行信息
  12. String line =line = null;
  13. // 相当于cmd服务
  14. Process exec = null;
  15. // ping 的结果
  16. boolean res = true;
  17. try {
  18. exec = Runtime.getRuntime().exec("ping " + ipAddress);
  19. BufferedReader br = new BufferedReader(new InputStreamReader(exec.getInputStream()));
  20. // 最多执行三秒
  21. long endTime = System.currentTimeMillis()+3000;
  22. // 测试输出行中是否有ttl字符串,有就说明ping通了
  23. while ((res=true)==true&&(line = br.readLine()).indexOf("ttl")<0){
  24. System.out.println("line = " + line);
  25. res = false;
  26. // 三秒还是ping不通则放弃尝试
  27. if(System.currentTimeMillis()>endTime){
  28. break;
  29. }
  30. }
  31. System.out.println("line = " + line);
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. } finally {
  35. exec.destroy();
  36. return res;
  37. }
  38. }
  39. }

发送邮件的工具类

发送邮件首先需要引入两个jar包:

  • maven

    1. <dependency>
    2. <groupId>javax.mail</groupId>
    3. <artifactId>javax.mail-api</artifactId>
    4. <version>1.6.2</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>com.sun.mail</groupId>
    8. <artifactId>javax.mail</artifactId>
    9. <version>1.6.2</version>
    10. </dependency>
  • gradle

    1. compile group: 'javax.mail', name: 'javax.mail-api', version: '1.6.2'
    2. compile group: 'com.sun.mail', name: 'javax.mail', version: '1.6.2'

    值得注意的是:邮箱密码是 QQ邮箱开通的stmp服务后得到的客户端授权码!!!类似于sjhabsjhdabasf这种字串。

    1. import javax.mail.Message;
    2. import javax.mail.MessagingException;
    3. import javax.mail.Session;
    4. import javax.mail.Transport;
    5. import javax.mail.internet.AddressException;
    6. import javax.mail.internet.InternetAddress;
    7. import javax.mail.internet.MimeMessage;
    8. import java.util.Properties;
    9. /**
    10. * @author: zp
    11. * @Date: 2019-10-08 14:13
    12. * @Description:
    13. */
    14. public class MailUtils {
    15. public static void sendQQMail() throws AddressException, MessagingException {
    16. Properties properties = new Properties();
    17. // 连接协议
    18. properties.put("mail.transport.protocol", "smtp");
    19. // 主机名
    20. properties.put("mail.smtp.host", "smtp.qq.com");
    21. // 端口号
    22. properties.put("mail.smtp.port", 465);
    23. properties.put("mail.smtp.auth", "true");
    24. // 设置是否使用ssl安全连接 ---一般都使用
    25. properties.put("mail.smtp.ssl.enable", "true");
    26. // 设置是否显示debug信息 true 会在控制台显示相关信息
    27. properties.put("mail.debug", "true");
    28. // 得到回话对象
    29. Session session = Session.getInstance(properties);
    30. // 获取邮件对象
    31. Message message = new MimeMessage(session);
    32. // 设置发件人邮箱地址
    33. message.setFrom(new InternetAddress("1226364229@qq.com"));
    34. // 设置收件人邮箱地址,可以有多个收件人
    35. message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("313694179@qq.com")});
    36. // 设置邮件标题
    37. message.setSubject("ping结果");
    38. // 设置邮件内容
    39. message.setText("您的IP解封了,成功ping通谷歌!");
    40. // 得到邮差对象
    41. Transport transport = session.getTransport();
    42. // 连接自己的邮箱账户,密码为QQ邮箱开通的stmp服务后得到的客户端授权码!!!
    43. transport.connect("1226364229@qq.com", "**********");
    44. // 发送邮件
    45. transport.sendMessage(message, message.getAllRecipients());
    46. transport.close();
    47. }
    48. }

定时任务类

  1. 这个类采用的是基于SchedulingConfigurer接口的,在ping通后,会修改cron表达式的值,防止重复发送邮件。(之前一分钟测一次,现在一天测一次)
  2. ```java
  3. import com.example.demojpa.utils.MailUtils;
  4. import com.example.demojpa.utils.PingUtils;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.scheduling.annotation.EnableScheduling;
  7. import org.springframework.scheduling.annotation.SchedulingConfigurer;
  8. import org.springframework.scheduling.config.ScheduledTaskRegistrar;
  9. import java.text.DateFormat;
  10. import java.text.SimpleDateFormat;
  11. import java.util.Date;
  12. /**
  13. * @author: zp
  14. * @Date: 2019-10-08 16:22:20
  15. * @Description:
  16. */
  17. @Configuration
  18. @EnableScheduling
  19. public class TaskBasedInterface implements SchedulingConfigurer {
  20. /**
  21. * 每小时执行一次
  22. */
  23. private static String cron = "0 */1 * * * ?";
  24. @Override
  25. public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
  26. taskRegistrar.addCronTask(()-> {
  27. // 测试连接
  28. boolean connected = PingUtils.ping02("www.google.com");
  29. if(connected){
  30. try {
  31. // 修改cron表达式,每天凌晨执行给我发邮件
  32. cron = "0 0 0 * * ? *";
  33. MailUtils.sendQQMail();
  34. log("已成功ping通!");
  35. return;
  36. } catch (Exception e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. log("ping不通");
  41. },cron);
  42. }
  43. public static void log(String message) {
  44. DateFormat df = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
  45. String date = df.format(new Date(System.currentTimeMillis()));
  46. System.out.println(date + " "+message);
  47. }
  48. }
  49. ```

结果

file

file

后语

能用已有的知识来做一些有趣的事真的能提高你对技术的兴趣呀,反正我是体会到了。

原文链接:http://www.cnblogs.com/menmenz/p/11639944.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号