用Java实现每天给对象发情话
黑客与编程
共 5205字,需浏览 11分钟
·
2021-03-19 13:15
【重要通知!!!】
黑客与编程公众号转移阵地啦!点击上方关注,希望大家多多关注,以后主要精力就放在新公众号上面啦。快来关注吧!!!
来自:https://blog.csdn.net/qq_33758782
一、引言
使用HttpClient远程获取彩虹屁生成器网站中的内容 网站:https://chp.shadiao.app/
java Mail 实现发送邮件
SpringBoot 整合Scheduled 实现定时发送邮件
二、搭建项目
<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>
三、编写配置
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启动类
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();
}
- END -
评论