用Java实现每天给对象发情话
程序员的成长之路
共 4504字,需浏览 10分钟
·
2021-02-26 07:19
阅读本文大概需要 3.5 分钟。
来自:https://blog.csdn.net/qq_33758782
一、引言
使用HttpClient远程获取彩虹屁生成器网站中的内容 网站:https://chp.shadiao.app/
java Mail 实现发送邮件
SpringBoot 整合Scheduled 实现定时发送邮件
二、搭建项目
<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>
三、编写配置
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执行该任务*/
"0 20 17 * * *") (cron =
public void dsrw(){
String message = sendMessage.getOneS();
sendMessage.sendMessage("来自清茶淡粥的消息!❤",message);
}
}
七、打包运行
nohup java -jar jar包 >test.log &
八、总结
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://pan.baidu.com/s/17z1Pjs1iP9CDd7IYuGohYA 提取码: 4rxp
推荐阅读:
微信扫描二维码,关注我的公众号
朕已阅
评论