用Java实现每天给对象发情话
来源:https://blog.csdn.net/qq_33758782
一、引言
最近看到一篇用js代码实现表白的文章,深有感触。
然后发现自己也可以用java代码实现,然后就开始写代码了,发现还挺有意思的,话不多说开搞
实现思路:
使用HttpClient远程获取彩虹屁生成器网站中的内容 网站:https://chp.shadiao.app/
java Mail 实现发送邮件
SpringBoot 整合Scheduled 实现定时发送邮件
二、搭建项目
项目环境在SpringBoot框架基础上,加入邮件发送mail、RPC远程调用httpclient、Scheduled 的一个Maven项目,依赖如下:
<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>
三、编写配置
在编写配置前需要,在浏览器登录自己的邮箱在账号安全中设置开启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:: xxxxxxx@163.com
四、编写SpringBoot启动类
public class BiaoBaiApp {public static void main(String[] args) {SpringApplication.run(BiaoBaiApp.class,args);}
五、自动生成发送内容
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);//发送者邮件邮箱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("网站获取句子失败");}}}
六、编写定时任务
public class MyScheduled {private SendMessage sendMessage;/*定时执行任务方法 每天5点20执行该任务*/(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();}
最后附上我写的源码供大家参考:
2. 在下面公众号后台,回复关键字「123」:
评论
