一、引言
最近看到一篇用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協議驗證
port: 587
# 發給誰的郵箱
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://download.csdn.net/download/qq_33758782/13762917
項目推薦:
Java微服務實戰296集大型視頻-谷粒商城【附代碼和課件】
Java開發微服務暢購商城實戰【全357集大項目】-附代碼和課件
最全最詳細數據結構與算法視頻-【附課件和源碼】