springboot 飞书飞书SMTP
时间: 2023-08-17 20:11:20 浏览: 147
Spring Boot 提供了简单易用的邮件发送功能,可以通过集成飞书SMTP来发送邮件。下面是一个示例代码:
首先,在 pom.xml 文件中添加以下依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
```
接下来,在 application.properties 文件中配置飞书SMTP的相关信息:
```properties
# 邮件服务器主机名
spring.mail.host=smtp.exmail.qq.com
# 邮件服务器端口号
spring.mail.port=465
# 使用 SSL 连接
spring.mail.protocol=smtps
# 邮件服务器用户名
spring.mail.username=your_username
# 邮件服务器密码
spring.mail.password=your_password
# 发件人名称
spring.mail.properties.mail.smtp.from=your_email_address
```
然后,创建一个邮件发送服务类,例如 `EmailService`:
```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 mailSender;
public void sendEmail(String to, String subject, String text) {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(text);
mailSender.send(message);
}
}
```
最后,在需要发送邮件的地方注入 `EmailService` 并调用 `sendEmail` 方法即可发送邮件。
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class EmailController {
@Autowired
private EmailService emailService;
@GetMapping("/sendEmail")
public String sendEmail() {
emailService.sendEmail("recipient@example.com", "Test Email", "This is a test email.");
return "Email sent.";
}
}
```
以上代码示例了如何在 Spring Boot 中使用飞书SMTP发送邮件。你可以根据自己的需求修改配置和实现发送邮件的逻辑。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)