用Java實(shí)現(xiàn)每天給對象發(fā)情話
閱讀本文大概需要 3.5 分鐘。
來自:https://blog.csdn.net/qq_33758782
一、引言
使用HttpClient遠(yuǎn)程獲取彩虹屁生成器網(wǎng)站中的內(nèi)容 網(wǎng)站:https://chp.shadiao.app/
java Mail 實(shí)現(xiàn)發(fā)送郵件
SpringBoot 整合Scheduled 實(shí)現(xiàn)定時發(fā)送郵件
二、搭建項(xiàng)目
<parent><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-parentartifactId><version>2.3.2.RELEASEversion>parent><dependencies><dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-mailartifactId>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-context-supportartifactId>dependency><dependency><groupId>org.apache.httpcomponentsgroupId><artifactId>httpclientartifactId><version>4.5.12version>dependency>dependencies><build><plugins><plugin><groupId>org.springframework.bootgroupId><artifactId>spring-boot-maven-pluginartifactId><configuration><fork>truefork>configuration>plugin>plugins>????build>
三、編寫配置




spring:mail:username: [email protected] # 自己郵箱地址password: xxxxxxx # SMTP|POP3|IMAP協(xié)議授權(quán)碼host: smtp.qq.com # 服務(wù)器地址。參考郵箱服務(wù)運(yùn)營商提供的信息。properties:mail:smtp:auth: true # 開啟smtp協(xié)議驗(yàn)證port: 587# 發(fā)給誰的郵箱she::[email protected]
四、編寫SpringBoot啟動類
public class BiaoBaiApp {public static void main(String[] args) {SpringApplication.run(BiaoBaiApp.class,args);}
五、自動生成發(fā)送內(nèi)容
public class SendMessage {private JavaMailSender mailSender;("${spring.mail.username}")private String from;("${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);//發(fā)送者郵件郵箱helper.setTo(sheMail);//收郵件者郵箱helper.setSubject(subject);//發(fā)件主題helper.setText(message);//發(fā)件內(nèi)容mailSender.send(helper.getMimeMessage());//發(fā)送郵件} catch (MessagingException e) {e.printStackTrace();}}/**遠(yuǎn)程獲取要發(fā)送的信息*/public static String getOneS(){try {//創(chuàng)建客戶端對象HttpClient client = HttpClients.createDefault();/*創(chuàng)建地址 https://du.shadiao.app/api.php*/HttpGet get = new HttpGet("https://chp.shadiao.app/api.php");//發(fā)起請求,接收響應(yīng)對象HttpResponse response = client.execute(get);//獲取響應(yīng)體,響應(yīng)數(shù)據(jù)是一種基于HTTP協(xié)議標(biāo)準(zhǔn)字符串的對象//響應(yīng)體和響應(yīng)頭,都是封裝HTTP協(xié)議數(shù)據(jù)。直接使用可能出現(xiàn)亂碼或解析錯誤HttpEntity entity = response.getEntity();//通過HTTP實(shí)體工具類,轉(zhuǎn)換響應(yīng)體數(shù)據(jù)String responseString = EntityUtils.toString(entity, "utf-8");return responseString;} catch (IOException e) {throw new RuntimeException("網(wǎng)站獲取句子失敗");}}}
六、編寫定時任務(wù)
public class MyScheduled {private SendMessage sendMessage;/*定時執(zhí)行任務(wù)方法 每天5點(diǎn)20執(zhí)行該任務(wù)*/(cron ="0 20 17 * * *")public void dsrw(){String message = sendMessage.getOneS();sendMessage.sendMessage("來自清茶淡粥的消息!?",message);}}
七、打包運(yùn)行

nohup?java?-jar?jar包?>test.log?&




八、總結(jié)
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 方式發(fā)送mailSender.send(helper.getMimeMessage());} catch (MessagingException e) {e.printStackTrace();????????}
百度云鏈接:? https://pan.baidu.com/s/17z1Pjs1iP9CDd7IYuGohYA? 提取碼: 4rxp
推薦閱讀:
華為正式宣布養(yǎng)豬,網(wǎng)友沸騰:支持華為自救!
微信掃描二維碼,關(guān)注我的公眾號
朕已閱?
評論
圖片
表情

