当前位置: 首页 > news >正文

【愚人节专场】Java实现定时发送小情话

首先,感谢大佬的帮助~附上大佬的博客以示尊敬https://blog.csdn.net/qq_38591577/article/details/128164308?spm=1001.2014.3001.5502

功能实现:

在名为愚人节,实为告白/情人节的日子里,怎么样才能引起TA的关注呢?不妨试着定时发送(土味)小情话来增进感情呢~

我的老婆们收到之后都开心的表示,不要捣鼓这些无聊的东西,不如抓紧去赚钱。

这是来自老婆的反馈:

 

 

 

咳咳,虽然被针对了,但是女人说不要那就是要(/▽\)

框架设计:

2.1 创建springboot项目

此处注意尽量不要使用springboot3.0.0,我这里用的是2.7.10。

2.2 pom依赖

<!-- hutool 依赖-->
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>4.3.2</version>
    </dependency>
<!-- 邮件 依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
        <version>2.4.3</version>
    </dependency>

2.3 application.yml (配置文件)

spring:
  mail:
    host: smtp.qq.com                  #邮箱发送服务器
    username: 181*******@qq.com       #邮箱地址
    password: abdsjszkazkjsad       #获取邮箱第三方使用秘钥
    protocol: smtp
    properties.mail.smtp.port: 25       #端口
    default-encoding: utf-8
she:
  mail: 114*******@qq.com
  mail2: 184*******@qq.com
  mail3: 182*******@qq.com

2.4 DemoApplication启动类

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

2.5 SendMailService.java (接口)

package com.example.demo.service.impl;

import org.springframework.stereotype.Service;
@Service
public interface SendMailService {
    void sendMessage(String sub, String message);

    String getLovePrattle();

    String getWeather();

}

2.6 SendMail.java (接口实现类)

package com.example.demo.service.impl;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.util.ArrayList;


@Component
public class SendMail implements SendMailService{
    @Resource
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    @Value("${she.mail}")
    private String[] sheMail;
    @Value("${she.mail2}")
    private String[] sheMail2;
    @Value("${she.mail3}")
    private String[] sheMail3;


    public void sendMessage(String subject,String message) {
        ArrayList<String[]> objects = new ArrayList<>();
        objects.add(sheMail);
        objects.add(sheMail2);
        objects.add(sheMail3);

        for (String[] object : objects) {
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
            helper.setFrom(from);       //发送方邮件名
            helper.setTo(object);         //接收方邮件地址
            helper.setSubject(subject);     //邮件标题
            helper.setText(message,true);   //邮件内容,是否为html格式
            mailSender.send(helper.getMimeMessage());
        } catch (javax.mail.MessagingException e) {
            e.printStackTrace();
        }}
    }
    @Override
    public String getLovePrattle() {
        String result1= HttpUtil.get("https://api.lovelive.tools/api/SweetNothings");
        System.out.println(result1);
        return result1;
    }

    //  实时天气
    @Override
    public String getWeather() {
        StringBuffer sb = new StringBuffer();
        String s = HttpUtil.get("https://devapi.qweather.com/v7/weather/now?location=101190113&key=f5fe849feaf24a60b54e6d6cb7062a8e");
        JSONObject jsonObject = JSONUtil.parseObj(s);
        System.out.println("时间为:"+jsonObject.get("updateTime"));
        sb.append("<p>时间为:"+jsonObject.get("updateTime")+"<br>\n");
        System.out.println("详细天气请查看"+jsonObject.get("fxLink"));
        sb.append("详细天气请查看"+jsonObject.get("fxLink")+"<br>\n");
        Object now = jsonObject.get("now");
        JSONObject nowJson = JSONUtil.parseObj(now);
        System.out.println("当前温度为"+nowJson.get("temp")+"℃");
        sb.append("当前温度为"+nowJson.get("temp")+"℃"+"<br>\n");
        System.out.println("天气状况为"+nowJson.get("text"));
        sb.append("天气状况为"+nowJson.get("text")+"<br>\n");
        System.out.println("风力等级"+nowJson.get("windScale")+"级");
        sb.append("风力等级"+nowJson.get("windScale")+"级"+"<br>\n");
        System.out.println("能见度"+nowJson.get("vis")+"公里");
        sb.append("能见度"+nowJson.get("vis")+"公里</p >");
        String weater = sb.toString();
        return weater;
    }

}

2.7 SchedueTask.java (定时任务配置类)

package com.example.demo.config;

import com.example.demo.service.impl.SendMail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;

@Configuration      //主要用于标记配置类,兼备Component的效果。
@EnableScheduling   // 开启定时任务
public class ScheduleTask {
    @Resource
    SendMail sendMail;

    @Async
    @Scheduled(cron = "0 */1 * * * ?")//每分钟发一次(这里是用的是cron表达式,可以上网查阅)
    public void send(){
//        土味情话
        String one = sendMail.getLovePrattle();
//        String weather = sendMail.getWeather();

        sendMail.sendMessage("小点心",one);


    }
}

总结:

试着实现了两个方法,一个调用天气,一个土味小情话。

当然,如果鱼塘里有好好多的好多鱼的话,也可以在配置文件里编辑多个邮箱,实现的时候for循环一下就可以了~~

QQ邮箱里的password一栏需要在QQ邮箱里进行设置。

 

由于我们配置的是SMTP,所以需要将其设置为打开

 

 

相关文章:

  • 基于SpringBoot实现CSGO游戏赛事管理系统演示【附项目源码】分享
  • 一个年薪40w软件测试员的职业规划,写给还在迷茫中的朋友
  • 使用Xarray解码GFS气象源文件和pip国内源
  • Beats:在 Docker 中同时部署 Metricbeat 和 Elasticsearch
  • 【数据仓库-7】-- 使用维度建模的一些缘由
  • node-fs
  • 无人机动力测试台-自动化测试系统拉力、扭矩、电压、电流、转速和效率
  • 国产机器人抢滩进行时!优艾智合引领智能制造浪潮
  • Autosar标准与其他标准的关系
  • 语音通知短信 API:一种新型的信息传递方式
  • 网络现代化势在必行,VMware 发布软件定义网络 SD-WAN 全新方案
  • Redis缓存穿透、击穿、雪崩问题及解决方法
  • 音乐制作:Ableton Live 11 Suite Mac
  • C++ 学习笔记(十)(继承、抽象篇)
  • xss labs(11-14)
  • STM32学习(四)
  • JAVA开发(自研项目的开发与推广)
  • 计算机网络的基本组成
  • Linux- 系统随你玩之--网络上的黑客帝国
  • 图片的美白与美化
  • https://app.hackthebox.com/machines/Inject
  • Spring —— Spring简单的读取和存储对象 Ⅱ
  • 渗透测试之冰蝎实战
  • Mybatis、TKMybatis对比
  • Microsoft Office 2019(2022年10月批量许可版)图文教程
  • 《谷粒商城基础篇》分布式基础环境搭建
  • 哈希表题目:砖墙
  • Vue 3.0 选项 生命周期钩子
  • 【车载嵌入式开发】AutoSar架构入门介绍篇
  • 【计算机视觉 | 目标检测】DETR风格的目标检测框架解读