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

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: [email protected]
     #qq 授权码
     password: thsyichkpaxabegh 
     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("[email protected]", "测试标题", "测试内容");
	}
}

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("[email protected]", "测试标题", "你好");
		System.out.println("=======测试==========");
		System.in.read();
	}

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

猜你喜欢

转载自blog.csdn.net/qq_41129811/article/details/84993752