首先,感谢大佬的帮助~附上大佬的博客以示尊敬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();
}
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;
}
}
}
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();
sendMail.sendMessage("小点心",one);
}
}
总结:
试着实现了两个方法,一个调用天气,一个土味小情话。
当然,如果鱼塘里有好好多的好多鱼的话,也可以在配置文件里编辑多个邮箱,实现的时候for循环一下就可以了~~
QQ邮箱里的password一栏需要在QQ邮箱里进行设置。
由于我们配置的是SMTP,所以需要将其设置为打开文章来源:https://www.toymoban.com/news/detail-402911.html
文章来源地址https://www.toymoban.com/news/detail-402911.html
到了这里,关于【愚人节专场】Java实现定时发送小情话的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!