最近看到一篇用js代码实现表白的文章,深有感触。
然后创造自己也可以用java代码实现,然后就开始写代码了,创造还挺故意思的,话不多说开搞
实现思路:

利用HttpClient远程获取彩虹屁天生器网站中的内容 网站:https://chp.shadiao.app/
java Mail 实现发送邮件
SpringBoot 整合Scheduled 实现定时发送邮件
二、搭建项目
项目环境在SpringBoot框架根本上,加入邮件发送mail、RPC远程调用httpclient、Scheduled 的一个Maven项目,依赖如下:
org.springframework.boot
spring-boot-starter-parent
2.3.2.RELEASE
<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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
三、编写配置
在编写配置前须要,在浏览器登录自己的邮箱在账号安全中设置开启POP3/SMT做事
开始开启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("s p r i n g . m a i l . u s e r n a m e " ) p r i v a t e S t r i n g f r o m ; @ V a l u e ( " {spring.mail.username}") private String from; @Value("spring.mail.username")privateStringfrom;@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("网站获取句子失落败");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
}
六、编写定时任务
@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);
}
1
2
3
4
5
6
}
七、打包运行
有条件的可以吧jar包放在运做事器上,没有条件的可以在本地win10系统上添加定时任务,每天定时实行jar包。
jar包放在做事器上须要放行端口:587 ,防火墙放行587端口
除了放行,还有放行 http 端口 和 https端口
然后在linux上后台启动jar包nohup java -jar jar包 >test.log &win10 定时运jar 包 在任务操持程序中创建任务
然后可以瞥见,创建好的任务
八、总结
代码还有很大的提升,也有很多不敷之处。由于韶光缘故原由,可优化的地方还很多,比如:发送纯挚的笔墨内容的邮件,不雅观观,可以实现html办法发送邮件,使发送邮件内容更加都雅。
小编还有很多不敷之处,欢迎大家在评论区留言一起谈论