文章目錄
- 一、引言
- 二、搭建專案
- 二、撰寫配置
- 四、撰寫SpringBoot啟動類
- 五、自動生成發送內容
- 六、撰寫定時任務
- 七、打包運行
- 八、總結
一、引言
最近看到一篇用js代碼實作表白的文章,深有感觸,
然后發現自己也可以用java代碼實作,然后就開始寫代碼了,發現還挺有意思的,話不多說開搞
實作思路:
-
使用HttpClient遠程獲取彩虹屁生成器網站中的內容 網站:https://chp.shadiao.app/
-
java Mail 實作發送郵件
-
SpringBoot 整合Scheduled 實作定時發送郵件
二、搭建專案
專案環境在SpringBoot框架基礎上,加入郵件發送mail、RPC遠程呼叫httpclient、Scheduled 的一個Maven專案,依賴如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<!-- httpclient 依賴 -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
</dependencies>
<!--打包插件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
二、撰寫配置
在撰寫配置前需要,在瀏覽器登錄自己的郵箱在賬號安全中設定開啟POP3/SMTP服務

開始開啟POP3/SMTP服務需要輸入驗證碼

復制授權碼

勾選SMTP發信后保存到服務器,勾選這一項主要是可以看到自己發送了什么資訊,不勾選此項,郵件訊息發送成功后,郵箱內看不到自己已發送的資訊

根據授權碼撰寫配置
spring:
mail:
username: xxxxxx@qq.com # 自己郵箱地址
password: xxxxxxx # SMTP|POP3|IMAP協議授權碼
host: smtp.qq.com # 服務器地址,參考郵箱服務運營商提供的資訊,
properties:
mail:
smtp:
auth: true # 開啟smtp協議驗證
# 發給誰的郵箱
she:
mail: xxxxxxx@163.com
四、撰寫SpringBoot啟動類
@EnableScheduling
@SpringBootApplication
public class BiaoBaiApp {
public static void main(String[] args) {
SpringApplication.run(BiaoBaiApp.class,args);
}
五、自動生成發送內容
@Component
public class SendMessage {
@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.setSubject(subject);//發件主題
helper.setText(message);//發件內容
mailSender.send(helper.getMimeMessage());//發送郵件
} catch (MessagingException e) {
e.printStackTrace();
}
}
/**遠程獲取要發送的資訊*/
public static String getOneS(){
try {
//創建客戶端物件
HttpClient client = HttpClients.createDefault();
/*創建地址 https://du.shadiao.app/api.php*/
HttpGet get = new HttpGet("https://chp.shadiao.app/api.php");
//發起請求,接收回應物件
HttpResponse response = client.execute(get);
//獲取回應體,回應資料是一種基于HTTP協議標準字串的物件
//回應體和回應頭,都是封裝HTTP協議資料,直接使用可能出現亂碼或決議錯誤
HttpEntity entity = response.getEntity();
//通過HTTP物體工具類,轉換回應體資料
String responseString = EntityUtils.toString(entity, "utf-8");
return responseString;
} catch (IOException e) {
throw new RuntimeException("網站獲取句子失敗");
}
}
}
六、撰寫定時任務
@Component
public class MyScheduled {
@Autowired
private SendMessage sendMessage;
/*定時執行任務方法 每天5點20執行該任務*/
@Scheduled(cron ="0 20 17 * * *")
public void dsrw(){
String message = sendMessage.getOneS();
sendMessage.sendMessage("來自清茶淡粥的訊息!?",message);
}
}
七、打包運行
有條件的可以吧jar包放在運服務器上,沒有條件的可以在本地win10系統上添加定時任務,每天定時執行jar包,
jar包放在服務器上需要放行埠:587 ,防火墻放行587埠
除了放行,還有放行 http 埠 和 https埠

然后在linux上后臺啟動jar包
nohup java -jar jar包 >test.log &
win10 定時運jar 包 在任務計劃程式中創建任務

新建觸發器

新建操作,在程式或腳本輸入執行的jar命令,點擊確定

然后可以看見,創建好的任務

八、總結
代碼還有很大的提升,也有很多不足之處,
由于時間原因,可優化的地方還很多,比如:發送單純的文字內容的郵件,不美觀,可以實作html方式發送郵件,使發送郵件內容更加美觀,
public void sendHtmlMessage(String subject,String message){
try {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
helper.setFrom(from);
helper.setTo(sheMail);
helper.setSubject(subject);
helper.setText(message,true);//true 使用html 方式發送
mailSender.send(helper.getMimeMessage());
} catch (MessagingException e) {
e.printStackTrace();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/237592.html
標籤:其他
上一篇:String StringBuffer StringBuilder之間的區別
下一篇:基于區塊鏈的政務平臺設計探索
