[英]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?
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:
为此,您的配置会稍微复杂一些,因为您必须执行以下操作:
Create two beans of
MailProperties<\/code> using the same
@ConfigurationProperties<\/code> as you can see above.
如上所示,使用相同的
@ConfigurationProperties<\/code>创建两个
MailProperties<\/code> bean。
<\/li>
Use the
MailProperties<\/code> in a similar way as Spring boot does within
MailSenderPropertiesConfiguration<\/code><\/a> .
以与 Spring boot 在
MailSenderPropertiesConfiguration<\/code><\/a>中类似的方式使用
MailProperties<\/code> 。
<\/li><\/ul>
Configuration配置<\/h2>
After that, you can use the
spring.mail.primary.*<\/code> properties and
spring.mail.secondary.*<\/code> properties as you're used to.
之后,您可以照常使用
spring.mail.primary.*<\/code>属性和
spring.mail.secondary.*<\/code>属性。
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<\/code><\/pre>
Usage用法<\/h2>
After that, you can autowire both
primarySender<\/code> and
secondarySender<\/code> .
之后,您可以自动装配
primarySender<\/code>和
secondarySender<\/code> 。
Make sure to use the
@Qualifier<\/code> annotation to tell Spring which is which:
确保使用
@Qualifier<\/code>注释告诉 Spring 哪个是哪个:
@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; } }<\/code><\/pre>"
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;
}
For a correct injection of the javaMailProperties it is necessary to read the keys manually by means of the relative prefix.为了正确注入 javaMailProperties,必须通过相对前缀手动读取密钥。
@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;
}
}
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
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
@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.