依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
配置文件
username是发送者的邮箱账号
password 是发送邮件服务器的验证码
host 是发送邮件服务器的地址
spring:
mail:
username: 166@163.com
password: CRWUP
host: smtp.163.com
代码
@Component
@EnableScheduling
public class ScheduledTest {
// 邮件发送器
@Autowired
private JavaMailSender sender;
private String Subject = "主题";
private String Content = "正文";
private String To = "2039701916@qq.com";
private String from = "16638011271@163.com";
@Scheduled(cron = "0/5 * * * * *")
public void test(){
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setSubject(Subject);
simpleMailMessage.setText(Content);
simpleMailMessage.setTo(To);
simpleMailMessage.setFrom(from);
sender.send(simpleMailMessage);
System.out.println("发送邮件");
}
}