简体   繁体   English

如何在springboot中配置两个不同的电子邮件?

[英]how to configure two different emails in springboot?

I know I can use the following properties to automatically create a JavaMailSender bean:我知道我可以使用以下属性自动创建JavaMailSender bean:

spring.mail.host=hostname
spring.mail.port=587
spring.mail.username=username
spring.mail.password=password

However, how can I define these properties to create two JavaMailSender beans so I can send emails from different SMTP servers?但是,如何定义这些属性来创建两个JavaMailSender bean,以便我可以从不同的 SMTP 服务器发送电子邮件?

I tried defining the following properties:我尝试定义以下属性:

# Properties for sender 1
spring.mail.host=hostname
spring.mail.port=587
spring.mail.username=username
spring.mail.password=password

# Properties for sender 2
spring.mail.host1=hostname2
spring.mail.port1=587
spring.mail.username1=username2
spring.mail.password1=password2

However, this does not work as I expected, so how can I create two JavaMailSender beans using Spring boot?但是,这并不像我预期的那样工作,那么如何使用 Spring 引导创建两个JavaMailSender bean?

Creating the beans创建 bean<\/h2>

Spring boot will only initialize one JavaMailSender<\/code> as soon as it finds the spring.mail.*<\/code> properties. Spring boot 只会在找到spring.mail.*<\/code>属性后立即初始化一个JavaMailSender<\/code> 。 If you need multiple ones, you have to define these beans by yourself.如果需要多个,则必须自己定义这些 bean。 If you only need the properties host, port, username and password, you could use this simple configuration:如果你只需要属性主机、端口、用户名和密码,你可以使用这个简单的配置:

 @Configuration public class MailConfiguration { @Bean @ConfigurationProperties(prefix = "spring.mail.primary") public JavaMailSender primarySender() { return new JavaMailSenderImpl(); } @Bean @ConfigurationProperties(prefix = "spring.mail.secondary") public JavaMailSender secondarySender() { return new JavaMailSenderImpl(); } }<\/code><\/pre>

However, this will not work<\/strong> if you also want to configure spring.mail.properties.*<\/code> as well.但是,如果您还想配置spring.mail.properties.*<\/code> ,这将不起作用<\/strong>。 In order to do that, your configuration will be a bit more complex, since you'll have to do the following:为此,您的配置会稍微复杂一些,因为您必须执行以下操作:

If you are using the mail properties, at least in my case, the mail properties were not read with the @ConfigurationProperties.如果您使用的是邮件属性,至少在我的情况下,邮件属性不是使用@ConfigurationProperties 读取的。 So I change the solution a bit:所以我稍微改变一下解决方案:

@Bean
@ConfigurationProperties(prefix = "spring.mail.primarySender")
public JavaMailSender primarySender() {
    JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
    return javaMailSenderWithProperties(javaMailSender);
}

@Bean
@ConfigurationProperties(prefix = "spring.mail.secondarySender")
public JavaMailSender secondarySender() {
    JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
    return javaMailSenderWithProperties(javaMailSender);
}

private JavaMailSender javaMailSenderWithProperties(JavaMailSenderImpl javaMailSender) {
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    javaMailSender.setJavaMailProperties(props);
    return javaMailSender;
}

Configure Two different SMTP emails in springboot在springboot中配置两个不同的SMTP邮件

The correct configuration is:正确的配置是:

For a correct injection of the javaMailProperties it is necessary to read the keys manually by means of the relative prefix.为了正确注入 javaMailProperties,必须通过相对前缀手动读取密钥。

Bean


@Configuration
public class EmailConfig {
    public static final String MAIL_SENDER_PRIMARY_KEY = "spring.mail.primary";
    public static final String MAIL_SENDER_PRIMARY_PROPERTIES_KEY = "spring.mail.primary.properties";
    public static final String MAIL_SENDER_SECONDARY_KEY = "spring.mail.secondary";
    public static final String MAIL_SENDER_SECONDARY_PROPERTIES_KEY = "spring.mail.secondary.properties";
    
    public static final String KEY_SEPARATOR = ".";
    public static final String EMPTY_STRING = "";

    @Autowired
    Environment env;

    @Bean
    @ConfigurationProperties(prefix = MAIL_SENDER_PRIMARY_KEY)
    public JavaMailSender primarySender() {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        return javaMailSenderWithProperties(javaMailSender,MAIL_SENDER_PRIMARY_PROPERTIES_KEY);
    }

    @Bean
    @ConfigurationProperties(prefix = MAIL_SENDER_SECONDARY_KEY)
    public JavaMailSender secondarySender() {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

        return javaMailSenderWithProperties(javaMailSender,MAIL_SENDER_SECONDARY_PROPERTIES_KEY);
    }

    private JavaMailSender javaMailSenderWithProperties(JavaMailSenderImpl javaMailSender, String prefix) {
        Properties props = new Properties();
        if (env instanceof ConfigurableEnvironment) {
            for (PropertySource<?> propertySource : ((ConfigurableEnvironment) env).getPropertySources()) {
                if (propertySource instanceof EnumerablePropertySource) {
                    for (String key : ((EnumerablePropertySource) propertySource).getPropertyNames()) {
                        if (key.startsWith(prefix)) {
                            props.setProperty(key.replaceAll(prefix + KEY_SEPARATOR, EMPTY_STRING), propertySource.getProperty(key).toString());
                        }
                    }
                }
            }
        }

        javaMailSender.setJavaMailProperties(props);
        return javaMailSender;
    }
}

Configuration配置

Properties of PrimarySender PrimarySender 的属性

spring.mail.primary.username=
spring.mail.primary.password=
spring.mail.primary.host=
spring.mail.primary.port=
spring.mail.primary.protocol=smtp
spring.mail.primary.properties.mail.smtp.auth=true
spring.mail.primary.properties.mail.smtp.connectiontimeout=5000
spring.mail.primary.properties.mail.smtp.timeout=5000
spring.mail.primary.properties.mail.smtp.writetimeout=5000
spring.mail.primary.properties.mail.smtp.socketFactory.class=
spring.mail.primary.properties.mail.smtp.socketFactory.port=
#spring.mail.primary.properties.mail.smtp.auth=true
spring.mail.primary.properties.mail.smtp.starttls.enable=true
spring.mail.primary.properties.mail.smtp.starttls.required=true

Properties of SecondarySender SecondarySender 的属性

spring.mail.secondary.host=
spring.mail.secondary.port=
spring.mail.secondary.username=
spring.mail.secondary.password=
spring.mail.secondary.protocol=
spring.mail.secondary.properties.mail.smtp.auth=true
spring.mail.secondary.properties.mail.smtp.connectiontimeout=5000
spring.mail.secondary.properties.mail.smtp.timeout=5000
spring.mail.secondary.properties.mail.smtp.writetimeout=5000
spring.mail.secondary.properties.mail.smtp.socketFactory.class=
spring.mail.secondary.properties.mail.smtp.socketFactory.port=
#spring.mail.secondary.properties.mail.smtp.auth=true
spring.mail.secondary.properties.mail.smtp.starttls.enable=true
spring.mail.secondary.properties.mail.smtp.starttls.required=true

Usage用法


@Service
public class EmailService {

    private JavaMailSender primarySender;
    private JavaMailSender secondarySender;

    public EmailService (
        @Qualifier("primarySender") JavaMailSender primarySender,
        @Qualifier("secondarySender") JavaMailSender secondarySender) {
        this.primarySender = primarySender;
        this.secondarySender = secondarySender;
    }

    public void sendPrimaryEmail(String from, String to, String subject, String text){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        
        primarySender.send(message);

    }

 public void sendSecondaryEmail(String from, String to, String subject, String text) 
       {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        
        secondarySender.send(message);

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 SpringBoot 中为不同的数据源配置 JPA 存储库? - How can I configure JPA repositories for different DataSources in SpringBoot? 如何在 SpringBoot 中配置额外的类路径? - How to configure additional classpath in SpringBoot? 如何为 RestController 配置 SpringBoot 认证 - How to configure SpringBoot authentication for RestController 如何为两个不同的端口配置Jboss? - How to configure Jboss for two different ports? 如何在 SpringBoot 中将管理员电子邮件作为列表返回 - How to return admin emails in SpringBoot as a List 如何使用Springboot将JSP文件保存在两个或更多不同的文件夹中? - How to keep JSP files in two or more different folders with Springboot? 如何在springboot的requestbody中使用两个不同节点的map单个实体 - How to map single entity with two different node in requestbody in springboot 如何在 Java 中向两个不同的电子邮件地址发送 2 封电子邮件? - How do you send 2 emails to two different email addresses in Java? 如何为两个不同的会话配置两个hibernate transactionManager - How to configure two hibernate transactionManager for two different sessions 如何使用camunda企业版配置Springboot? - How to configure Springboot with camunda enterprise edition?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM