首先,感谢大佬的帮助~附上大佬的博客以示尊敬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,所以需要将其设置为打开