文章目录
一、功能介绍
1、使用springboot定时任务
2、调用第三方api获取土味情话 这里使用的api是 https://api.lovelive.tools/api/SweetNothings
3、采用邮件方式进行发送
源代码在最下方说明。
二、实现
2.1.1、创建spingboot项目
此处注意尽量不要使用springboot3.0.0 可能会报错 在springboot3.0.0中需要使用jdk17版本
2.1.2、项目结构
2.2、pom.xml(依赖)
<!-- 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: 33309******@qq.com #邮箱地址
password: ********* #获取邮箱第三方使用秘钥
protocol: smtp
properties.mail.smtp.port: 25 #端口
default-encoding: utf-8
she:
mail2: 242695****@qq.com
2.4、GlowingTermsApplication.java(启动类)
package xia.wan.glowingterms;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling //启动定时任务
public class GlowingTermsApplication {
public static void main(String[] args) {
SpringApplication.run(GlowingTermsApplication.class, args);
}
}
2.5、SendMailService.java (接口)
package xia.wan.glowingterms.service;
import org.springframework.stereotype.Service;
/**
* @PACKAGE_NAME: xia.wan.glowingterms.service
* @AUTHOR: 太白
* @DATE: 2022/12/01/19:12
**/
@Service
public interface SendMailservice {
void sendMessage(String sub,String message);
String getLovePrattle();
}
2.6、SendMail.java (接口实现类)
package xia.wan.glowingterms.service.impl;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
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 xia.wan.glowingterms.service.SendMailservice;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
/**
* @PACKAGE_NAME: xia.wan.glowingterms.service
* @AUTHOR: 太白
* @DATE: 2022/12/01/19:05
**/
@Component
public class SendMail implements SendMailservice {
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from;
@Value("${she.mail}")
private String[] sheMail;
public void sendMessage(String subject,String message) {
try {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
helper.setFrom(from); //发送方邮件名
helper.setTo(sheMail); //接收方邮件地址
/* helper.setTo(she1Mail);
helper.setTo(she2Mail);*/
helper.setSubject(subject); //邮件标题
helper.setText(message,true); //邮件内容,是否为html格式
mailSender.send(helper.getMimeMessage());
} catch (MessagingException e) {
System.out.println(e);
}
}
@Override
public String getLovePrattle() {
String result1= HttpUtil.get("https://api.lovelive.tools/api/SweetNothings");
System.out.println(result1);
return result1;
}
}
2.7、SchedueTask.java (定时任务配置类)
package xia.wan.glowingterms.config;
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 xia.wan.glowingterms.service.impl.SendMail;
/**
* @PACKAGE_NAME: xia.wan.glowingterms.config
* @AUTHOR: 太白
* @DATE: 2022/12/01/19:05
**/
@Configuration //主要用于标记配置类,兼备Component的效果。
@EnableScheduling // 开启定时任务
public class ScheduleTask {
@Autowired
SendMail sendMail;
@Async
@Scheduled(cron = "0 */1 * * * ?")//每分钟发一次(这里是用的是cron表达式,可以上网查阅)
public void send(){
// 土味情话
String one = sendMail.getLovePrattle();
sendMail.sendMessage("来自太白的❤",one);
}
}
三、源码
版权声明:本文为qq_38591577原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。