'how to configure two different emails in springboot?

I know I can use the following properties to automatically create a 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?

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?



Solution 1:[1]

Creating the beans

Spring boot will only initialize one JavaMailSender as soon as it finds the spring.mail.* properties. If you need multiple ones, you have to define these beans by yourself. 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();
    }
}

However, this will not work if you also want to configure spring.mail.properties.* as well. In order to do that, your configuration will be a bit more complex, since you'll have to do the following:

  • Create two beans of MailProperties using the same @ConfigurationProperties as you can see above.
  • Use the MailProperties in a similar way as Spring boot does within MailSenderPropertiesConfiguration.

Configuration

After that, you can use the spring.mail.primary.* properties and spring.mail.secondary.* properties as you're used to. For example:

spring.mail.primary.host=host1
spring.mail.primary.port=port1
spring.mail.primary.username=username1
spring.mail.primary.password=password1
spring.mail.secondary.host=host2
spring.mail.secondary.port=port2
spring.mail.secondary.username=username2
spring.mail.secondary.password=password2

Usage

After that, you can autowire both primarySender and secondarySender. Make sure to use the @Qualifier annotation to tell Spring which is which:

@Service
public class MailService {
    private JavaMailSender primarySender;
    private JavaMailSender secondarySender;

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

Solution 2:[2]

If you are using the mail properties, at least in my case, the mail properties were not read with the @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;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 g00glen00b
Solution 2 Dany Alfaro