一、引言
最近看到一篇用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@SpringBootApplicationpublic class BiaoBaiApp { public static void main(String[] args) { SpringApplication.run(BiaoBaiApp.class,args);}`
五、主动生成发送内容
`@Componentpublic 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("网站获取句子失败"); } }}`
六、编写定时工作
`@Componentpublic 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/dow...
我的项目举荐:
2000多G的计算机各行业电子资源分享(继续更新)
2020年微信小程序全栈我的项目之喵喵交友【附课件和源码】
Spring Boot开发小而美的集体博客【附课件和源码】
Java微服务实战296集大型视频-谷粒商城【附代码和课件】
Java开发微服务畅购商城实战【全357集大我的项目】-附代码和课件
最全最具体数据结构与算法视频-【附课件和源码】