【愚人节专场】Java实现定时发送小情话
生活随笔
收集整理的這篇文章主要介紹了
【愚人节专场】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: smtpproperties.mail.smtp.port: 25 #端口default-encoding: utf-8 she:mail: 114*******@qq.commail2: 184*******@qq.commail3: 182*******@qq.com2.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{@Resourceprivate 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();}}}@Overridepublic 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 {@ResourceSendMail sendMail;@Async@Scheduled(cron = "0 */1 * * * ?")//每分鐘發一次(這里是用的是cron表達式,可以上網查閱)public void send(){ // 土味情話String one = sendMail.getLovePrattle();sendMail.sendMessage("小點心",one);} }總結:
試著實現了兩個方法,一個調用天氣,一個土味小情話。
當然,如果魚塘里有好好多的好多魚的話,也可以在配置文件里編輯多個郵箱,實現的時候for循環一下就可以了~~
QQ郵箱里的password一欄需要在QQ郵箱里進行設置。
由于我們配置的是SMTP,所以需要將其設置為打開
總結
以上是生活随笔為你收集整理的【愚人节专场】Java实现定时发送小情话的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu22.04.1无法找到Ubu
- 下一篇: 敏锐嗅出商机,她瞄准花卉市场的空白,将花