您的位置:首页 > 编程语言 > Java开发

单身狗福利!利用java实现每天给对象发情话,脱单指日可待!

2020-12-20 22:22 2711 查看

引言

最近看到一篇用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
@SpringBootApplication
public class BiaoBaiApp {
public static void main(String[] args) {
SpringApplication.run(BiaoBaiApp.class,args);
}

自动生成发送内容

@Component
public 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("网站获取句子失败");
}
}
}

编写定时任务

@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);
}
}

打包运行

有条件的可以吧jar包放在运服务器上,没有条件的可以在本地win10系统上添加定时任务,每天定时执行jar包。

jar包放在服务器上需要放行端口:587 ,防火墙放行587端口

除了放行,还有放行 http 端口 和 https端口

然后在linux上后台启动jar包 1767

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();
}

最后

欢迎关注公众号:前程有光,领取一线大厂Java面试题总结+各知识点学习思维导+一份300页pdf文档的Java核心知识点总结!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐