springboot发送邮件发送方多个
时间: 2025-03-17 09:09:20 浏览: 38
### Spring Boot 配置多个邮件发送方的解决方案
为了支持在 Spring Boot 中配置并切换多个电子邮件发送者,可以通过自定义 `JavaMailSender` 实现动态设置发件人地址。以下是详细的实现方法:
#### 1. 添加依赖项
确保项目中的 `pom.xml` 文件包含以下 Maven 依赖项以启用邮件功能[^2]:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
```
#### 2. 定义多邮箱配置属性
创建一个类用于加载不同的邮件服务器配置。可以将这些配置存储在 `application.properties` 或 `application.yml` 文件中。
##### application.yml 示例
```yaml
mail:
sender1:
host: smtp.sender1.com
port: 587
username: [email protected]
password: pass1
protocol: smtp
properties:
mail.smtp.auth: true
mail.smtp.starttls.enable: true
sender2:
host: smtp.sender2.com
port: 465
username: [email protected]
password: pass2
protocol: smtp
properties:
mail.smtp.ssl.enable: true
```
#### 3. 创建配置类读取属性
编写一个 Java 类来映射上述 YAML 属性到对象结构中[^3]。
```java
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "mail")
public class MailConfigurations {
private Sender sender1;
private Sender sender2;
public static class Sender {
private String host;
private int port;
private String username;
private String password;
private String protocol;
private Properties properties;
// Getters and Setters
public String getHost() { return host; }
public void setHost(String host) { this.host = host; }
public int getPort() { return port; }
public void setPort(int port) { this.port = port; }
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
public String getProtocol() { return protocol; }
public void setProtocol(String protocol) { this.protocol = protocol; }
public java.util.Properties getProperties() { return properties; }
public void setProperties(java.util.Properties properties) { this.properties = properties; }
}
// Getters and Setters for sender1, sender2
}
```
#### 4. 动态配置 `JavaMailSender`
通过工厂模式或条件注入的方式,在运行时选择合适的 `JavaMailSender` 实例。
##### 工厂类示例
```java
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
@Service
public class EmailSenderFactory {
@Autowired
private MailConfigurations mailConfigurations;
public JavaMailSenderImpl createEmailSender(String senderKey) {
MailConfigurations.Sender sender = null;
switch (senderKey.toLowerCase()) {
case "sender1":
sender = mailConfigurations.getSender1();
break;
case "sender2":
sender = mailConfigurations.getSender2();
break;
default:
throw new IllegalArgumentException("Invalid sender key");
}
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(sender.getHost());
mailSender.setPort(sender.getPort());
mailSender.setUsername(sender.getUsername());
mailSender.setPassword(sender.getPassword());
mailSender.setDefaultEncoding("UTF-8");
java.util.Properties props = mailSender.getJavaMailProperties();
props.putAll(sender.getProperties());
return mailSender;
}
}
```
#### 5. 使用服务层调用不同发送器
在业务逻辑中根据需求动态选择发送方。
```java
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
@Service
public class EmailService {
private final EmailSenderFactory emailSenderFactory;
public EmailService(EmailSenderFactory emailSenderFactory) {
this.emailSenderFactory = emailSenderFactory;
}
public void sendEmail(String senderKey, String recipient, String subject, String content) {
JavaMailSender mailSender = emailSenderFactory.createEmailSender(senderKey);
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(recipient);
message.setSubject(subject);
message.setText(content);
mailSender.send(message);
}
}
```
#### 6. 测试代码
测试以上实现是否能够正常工作。
```java
@SpringBootTest
class EmailTest {
@Autowired
private EmailService emailService;
@Test
void testSendWithSender1() {
emailService.sendEmail("sender1", "[email protected]", "Test Subject", "This is a test.");
}
@Test
void testSendWithSender2() {
emailService.sendEmail("sender2", "[email protected]", "Another Test", "Testing with second sender.");
}
}
```
---
### 总结
此方案允许应用程序灵活地管理多个邮件发送者的配置,并能够在运行时动态切换它们。通过封装 `JavaMailSender` 的实例化过程,使代码更加模块化和可维护。
阅读全文
相关推荐




















