用Java實(shí)現(xiàn)每天給對(duì)象發(fā)情話
【重要通知!!!】
黑客與編程公眾號(hào)轉(zhuǎn)移陣地啦!點(diǎn)擊上方關(guān)注,希望大家多多關(guān)注,以后主要精力就放在新公眾號(hào)上面啦。快來(lái)關(guān)注吧!!!

來(lái)自: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)定時(shí)發(fā)送郵件
二、搭建項(xiàng)目
<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 依賴(lài) --><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>
三、編寫(xiě)配置




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

評(píng)論
圖片
表情
