SpringBoot集成Java Mail邮箱发送邮件以及异步发送邮件

本文详细介绍了如何在Spring Boot项目中配置和使用邮件服务,包括Maven依赖添加、application.yml配置、工具类抽离及异步发送邮件的实现。通过具体代码示例,展示了如何设置邮件服务器、账号及安全协议,以及如何使用JavaMailSender发送简单邮件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Maven jar包依赖

<!-- Inherit defaults from Spring Boot -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.10.RELEASE</version>
	</parent>

	<!-- Add typical dependencies for a web application -->
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- 热部署 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
		</dependency>
		<!--测试 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>   
		<!-- 邮件服务 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-mail</artifactId>
		</dependency>
	<dependencies>

2.application.yml配置文件



spring:
#发送邮箱配置  
   mail:
     #QQ 服务器
     host: smtp.qq.com
     #qq 发件邮箱
     username: ……@qq.com
     #qq 授权码
     password:
     properties:
       mail:
         smtp:
           auth: true  # 开启邮箱验证
           timeout: 25000 # 超时
           starttls:
             enable: true  # 安全协议  
             required: true       

3、工具类抽离

@Component
public class SendMailUtil{
	@Autowired
	JavaMailSender mailSender;// spring 提供的邮件发送类
	@Value("${spring.mail.username}")
	private String username;
		public void sendSimpleMail(String recName,String topic,String content)  {
		SimpleMailMessage message = new SimpleMailMessage();
		message.setFrom(username);// 发送者
		message.setTo(recName);  // 接受者
		message.setSubject(topic);// 邮件主题
		//邮件内容
		message.setText(content);
		mailSender.send(message);
	}
}

4、测试发送邮件


@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = RunApp.class)
public class SendEmailTest {
	@Autowired
	private SendMailUtil mailUtils;
	//发送邮件
	@Test
	public void testSendMail() throws Exception {
		mailUtils.sendSimpleMail("……@qq.com", "测试标题", "测试内容");
	}
}

5、异步发送邮件

1)启动类上加上@EnableAsync   //开启异步注解功能
@SpringBootApplication
@EnableAsync   //开启异步注解功能
public class RunApp {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(RunApp.class, args);
    }
}
2)需要异步的方法上加上@Async  // 异步方法
@Component
public class SendMailUtil {
	@Autowired
	JavaMailSender mailSender;// spring 提供的邮件发送类
	@Value("${spring.mail.username}")
	private String username;
	@Async   //  异步方法
	public void sendSimpleMail(String recName, String topic, String content) {
		SimpleMailMessage message = new SimpleMailMessage();
		message.setFrom(username);// 发送者
		message.setTo(recName); // 接受者
		message.setSubject(topic);// 邮件主题
		// 邮件内容
		message.setText(content);
		mailSender.send(message);
		System.out.println("==========发送邮件==========");
	}
}
3)测试异步发送邮件
	//发送邮件
	@Test
	public void testSendMail() throws Exception {
		mailUtils.sendSimpleMail("……@qq.com", "测试标题", "你好");
		System.out.println("=======测试==========");
		System.in.read();
	}

如果帮助到你了的话请点赞!!!!!!!!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值