我是靠谱客的博主 无语大白,最近开发中收集的这篇文章主要介绍Java 定时发送邮件1.开启SMTP2.添加依赖3.配置文件4.启动类5.SendMessage6.MyScheduled,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
1.开启SMTP
登录要发送邮件的邮箱,进入设置开启SMTP服务,并且拿到标识码。
2.添加依赖
<!-- httpclient 依赖 -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
<!-- 邮件 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.4.3</version>
</dependency>
<!-- 打包插件 -->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
3.配置文件
#邮箱的配置
spring:
mail:
host: smtp.163.com #发送邮件服务器
username: 13283341120@163.com #QQ邮箱
password: OZOBQZLBLXMGNHLG #客户端授权码
这个码都有的哈
protocol: smtp #发送邮件协议
properties.mail.smtp.port: 25 #端口号465或587
default-encoding: utf-8
she:
mail: 1835350825@qq.com
4.启动类
package com.example.emaildemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class EmailDemoApplication {
public static void main(String[] args) {
SpringApplication.run(EmailDemoApplication.class, args);
}
}
5.SendMessage
package com.example.emaildemo.service;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.swing.plaf.PanelUI;
import java.io.IOException;
@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,true);
mailSender.send(helper.getMimeMessage());
} catch (MessagingException e) {
System.out.println(e);
}
}
public String getOnes() {
try {
HttpClient client = HttpClients.createDefault();
HttpGet get = new HttpGet("https://chp.shadiao.app/api.php");
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "utf-8");
return responseString;
} catch (IOException e) {
System.out.println(e);
}
return null;
}
}
6.MyScheduled
package com.example.emaildemo.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class MyScheduled {
@Autowired
private SendMessage sendMessage;
@Scheduled(cron ="0 20 17 * *
*")
public void dsrw(){
String message = sendMessage.getOnes();
sendMessage.sendMessage("来自哲的消息!❤",message);
}
}
最后
以上就是无语大白为你收集整理的Java 定时发送邮件1.开启SMTP2.添加依赖3.配置文件4.启动类5.SendMessage6.MyScheduled的全部内容,希望文章能够帮你解决Java 定时发送邮件1.开启SMTP2.添加依赖3.配置文件4.启动类5.SendMessage6.MyScheduled所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复