spring boot 发邮件
时间: 2023-08-20 15:08:14 浏览: 113
您好!关于使用Spring Boot发送邮件,您可以按照以下步骤进行操作:
1. 首先,在您的Spring Boot项目中添加相关依赖。在您的pom.xml文件中添加以下依赖项:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
```
2. 在您的application.properties(或application.yml)文件中配置邮件服务器的相关信息,例如:
```properties
spring.mail.host=your_mail_host
spring.mail.port=your_mail_port
spring.mail.username=your_username
spring.mail.password=your_password
```
3. 创建一个邮件发送服务类,例如`EmailService`,并在其中注入`JavaMailSender`对象。例如:
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
@Service
public class EmailService {
@Autowired
private JavaMailSender javaMailSender;
public void sendEmail(String to, String subject, String text) {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(text);
javaMailSender.send(message);
}
}
```
4. 在需要发送邮件的地方,注入`EmailService`并调用`sendEmail`方法。例如:
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.
阅读全文
相关推荐













