pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
在测试代码中
@SpringBootTest
class SpringbootTestApplicationTests {
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {
//一个简单的邮件
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setSubject("nihao");
mailMessage.setText("xiexieni");
mailMessage.setTo("1426386618@qq.com");
mailMessage.setFrom("1426386618@qq.com");
mailSender.send(mailMessage);
}
}
application.properites
spring.mail.username=1426386618@qq.com
spring.mail.password=wnxlwhdpttwkfhhh
spring.mail.host=smtp.qq.com
#开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true
本文详细介绍了如何在Spring Boot项目中配置和使用JavaMailSender发送邮件。通过pom.xml引入spring-boot-starter-mail依赖,设置application.properties中的邮件服务器参数,并在测试类中使用@SpringBootTest进行邮件发送测试。
1140

被折叠的 条评论
为什么被折叠?



