谁说程序员不懂浪漫的?每日自动给女朋友发情话, 源码奉上(文末获取) !
一、引言
前几天听说朋友跟我吹嘘自己可以用js代码实现表白,我一想,你JS可以,难道我Java不行吗?说干就干,首先,我捋清实现的思路:
- 情话获取方式:利用HttpClient,将网站:彩虹屁生成器()中的内容存下来
- 发送邮件功能:java Mail
- 定时发送功能:利用SpringBoot对Scheduled进行整合
二、搭建项目
首先,以SpringBoot框架为基础,对项目环境进行搭建,同时,将 RPC 远程调用httpclient、Scheduled 的一个Maven项目、邮件发送mail加入进来:
<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服务(安全设置中)
这一步骤需要【验证码】
复制【授权码】
勾选第三项,【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 send Message { @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 (Messaging Exception e) { e.printStackTrace(); } } /**远程获取要发送的信息*/ public static String getOneS(){ try { //创建客户端对象 HttpClient client = HttpClients.createDefault(); /*创建地址 HttpGet get = new HttpGet("#34;); //发起请求,接收响应对象 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包,放在 云服务器 上,此方式防火墙放行587端口,同时,放行 https 端口 、http端口;其二,将定时任务添加在本地win10系统上,但是此方式需要每天定时执行jar包。
后台启动jar包(linux)
nohup java -jar jar包 >test.log &
方式二:win10 定时运jar 包
实现:创建任务(在任务计划程序中)
新建触发器
输入jar命令
创建ok
八、总结
到这一步,就可以实现每日自动给女朋友发送情话啦!但是由于时间仓促,代码还有很大的提升,也有很多不足之处。
例如:可以采用 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(); }
需要 源码 的小伙伴: