基于SpringBoot实现定时发送邮件过程解析
更新时间:2020年06月10日 11:53:28 作者:娜一抹微笑
这篇文章主要介绍了基于SpringBoot实现定时发送邮件过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
前提:
1.Springboot项目
2.引入maven 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
以下代码中涉及到的maven依赖有日志依赖,但是springboot都有集成,不用重新引入依赖

Application(程序入口)
package com.springbootemaildemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* 引入了一个注解@EnableSwagger2来启动swagger注解。(启动该注解使得用在controller中的swagger注解生效, 覆盖的范围由@ComponentScan的配置来指定,
* 这里默认指定为根路径”com.springboot”下的所有controller)
* 也可以单独写衣swaggerConfigura
*/
@EnableScheduling //启动定时任务
@EnableSwagger2 //启动swagger注解
@SpringBootApplication
public class MailApplication {
public static void main(String[] args) {
SpringApplication.run(MailApplication.class, args);
}
}
MailJob(定时任务类)
package com.springbootemaildemo.job;
import com.springbootemaildemo.send.SendMail;
import com.springbootemaildemo.send.TenSenvenMail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
@EnableScheduling
public class MailJob {
private static final Logger logger = LoggerFactory.getLogger(MailJob.class);
@Resource
SendMail sendMail;
@Resource
TenSenvenMail tenSenvenMail;
//@Scheduled(cron = "0/5 * * * * ?")
//或直接指定时间间隔,例如:100秒
// @Scheduled(fixedRate=100000)
//早晨7点
@Scheduled(cron = "0 0 7 * * ?")
public void sendJob() {
String bodyTen = "早安哇,太阳出来啦,记得开心哟";
String bodyWen = "记得开心哟";
logger.info("定时任务开始..........................");
sendMail.sendWen(bodyWen);
tenSenvenMail.sendTen(bodyTen);
logger.info("定时任务结束..........................");
}
}
@EnableScheduling 这个注解是 开启定时任务。
发送邮件代码:
发送普通的邮件(发送邮件类):
package com.springbootemaildemo.send;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;
@Component
public class SendMail {
private static final Logger logger = LoggerFactory.getLogger(SendMail.class);
public void sendWen(String body) {
logger.info("开始发送..................");
String from = "212212@qq.com";
String to = "5456456@qq.com";
String subject = "HAPPY";
String smtpHost = "smtp.qq.com";
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp"); // 使用的协议(JavaMail规范要求)
props.setProperty("mail.smtp.host", smtpHost); // 发件人的邮箱的 SMTP服务器地址
props.setProperty("mail.smtp.auth", "true"); // 请求认证,参数名称与具体实现有关
// 创建Session实例对象
Session session = Session.getDefaultInstance(props);
// 创建MimeMessage实例对象
MimeMessage message = new MimeMessage(session);
// 设置发件人
try {
message.setFrom(new InternetAddress(from));
// 设置收件人
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
// 设置发送日期
message.setSentDate(new Date());
// 设置邮件主题
message.setSubject(subject);
// 设置纯文本内容的邮件正文
message.setText(body);
// 保存并生成最终的邮件内容
message.saveChanges();
// 设置为debug模式, 可以查看详细的发送 log
session.setDebug(true);
// 获取Transport对象
Transport transport = session.getTransport("smtp");
// 第2个参数需要填写的是QQ邮箱的SMTP的授权码,什么是授权码,它又是如何设置?
transport.connect(from, "ipeiquufachheefg");
// 发送,message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
transport.sendMessage(message, message.getAllRecipients());
logger.info("发送完成");
transport.close();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
使用注解@Validated和BindingResult对入参进行非空校验方式
这篇文章主要介绍了使用注解@Validated和BindingResult对入参进行非空校验方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-10-10
java中jdbcTemplate的queryForList(坑)
本文主要介绍了java中jdbcTemplate的queryForList,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2021-09-09
Java中的System类、BigInteger类和BigDecimal类详解
这篇文章主要介绍了Java中的System类、BigInteger类和BigDecimal类详解,arraycopy()方法,复制数组元素,比较适合底层调用,一般使用Arrays.copyOf()完成复制数组,需要的朋友可以参考下2023-09-09
SpringBoot+layuimini实现左侧菜单动态展示的示例代码
Layuimini是Layui的升级版,它是专业做后台页面的框架,而且是适合PC端和移动端,以下地址可以在PC端显示,也可以在手机上显示,只不过会做自适应,本文将给大家介绍了SpringBoot+layuimini实现左侧菜单动态展示的方法,需要的朋友可以参考下2024-04-04
5分钟快速搭建SpringBoot3 + MyBatis-Plus工程/项目的实现示例
本文主要介绍了使用IntelliJ IDEA创建Spring Boot工程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2025-01-01


最新评论