springboot发送邮件
时间: 2023-03-24 20:02:28 浏览: 110
一文快速搞懂Springboot发送邮件操作
可以使用JavaMailSender类来发送邮件,Spring Boot已经为我们提供了自动配置。首先需要在pom.xml文件中添加依赖:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
```
然后在application.properties文件中配置邮件服务器信息:
```
spring.mail.host=smtp.example.com
spring.mail.port=587
spring.mail.username=yourusername
spring.mail.password=yourpassword
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
```
最后在代码中使用JavaMailSender发送邮件即可:
```
@Autowired
private JavaMailSender javaMailSender;
public void sendEmail() {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo("recipient@example.com");
message.setSubject("Test Email");
message.setText("This is a test email.");
javaMailSender.send(message);
}
```
阅读全文