Spirngboot多邮件源
Springboot多邮件地址发送邮件
1. 自定义配置文件
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @author devcxl
*/
@Component
@ConfigurationProperties(prefix = "spring.mail")
public class MailConfig {
private List<MailProperties> configs;
public List<MailProperties> getConfigs() {
return configs;
}
public void setConfigs(List<MailProperties> configs) {
this.configs = configs;
}
}
2. 邮件发送配置 MailSenderConfig
import com.devcxl.mail.MailAllSender;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
* @author devcxl
*/
@Configuration
@ConditionalOnProperty(
prefix = "spring.mail",
name = {"configs"}
)
public class MailSenderConfig {
/**
* AllMailSender 没有被配置时自动配置
*
* @return
*/
@Bean
@ConditionalOnMissingBean({MailAllSender.class})
MailAllSender mailAllSender(MailConfig config) {
MailAllSender allSender = new MailAllSender();
List<JavaMailSenderImpl> javaMailSenders = new ArrayList<>();
List<MailProperties> configs = config.getConfigs();
for (MailProperties mailProperties : configs) {
JavaMailSenderImpl sender = new JavaMailSenderImpl();
this.applyProperties(mailProperties, sender);
javaMailSenders.add(sender);
}
allSender.setJavaMailSenders(javaMailSenders);
return allSender;
}
private void applyProperties(MailProperties properties, JavaMailSenderImpl sender) {
sender.setHost(properties.getHost());
if (properties.getPort() != null) {
sender.setPort(properties.getPort());
}
sender.setUsername(properties.getUsername());
sender.setPassword(properties.getPassword());
sender.setProtocol(properties.getProtocol());
if (properties.getDefaultEncoding() != null) {
sender.setDefaultEncoding(properties.getDefaultEncoding().name());
}
if (!properties.getProperties().isEmpty()) {
sender.setJavaMailProperties(this.asProperties(properties.getProperties()));
}
}
private Properties asProperties(Map<String, String> source) {
Properties properties = new Properties();
properties.putAll(source);
return properties;
}
}
3. 邮件发送组件 MailAllSender
import com.devcxl.mail.constant.Constant;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;
/**
* @author devcxl
*/
@Slf4j
public class MailAllSender {
@Resource
private RedisTemplate<String, Object> redisTemplate;
private List<JavaMailSenderImpl> javaMailSenders;
public List<JavaMailSenderImpl> getJavaMailSenders() {
return javaMailSenders;
}
public void setJavaMailSenders(List<JavaMailSenderImpl> javaMailSenders) {
this.javaMailSenders = javaMailSenders;
}
/**
* 轮询使用MailSender
*
* @return
*/
public JavaMailSenderImpl getSender() {
Long increment = redisTemplate.opsForValue().increment(Constant.MAIL_SEND_STEP_NUM);
long countNumber = increment != null ? increment : 0L;
int size = this.javaMailSenders.size();
int selectSender = (int) (countNumber % size);
return this.javaMailSenders.get(selectSender);
}
/**
* 轮询使用MailSender
*
* @return
*/
public void send(String to, String senderName, String title, String content, boolean isHtml) {
JavaMailSenderImpl sender = this.getSender();
MimeMessage mimeMessage = sender.createMimeMessage();
try {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, StandardCharsets.UTF_8.toString());
mimeMessageHelper.setTo(to);
mimeMessageHelper.setFrom(senderName);
mimeMessageHelper.setText(content, isHtml);
mimeMessageHelper.setSubject(title);
for (Integer attachmentFileId : attachmentFileIds) {
File file = null;
mimeMessageHelper.addAttachment(file.getName(), file);
}
} catch (MessagingException e) {
log.error("邮件发送错误", e);
}
}
}
- 配置示例
spring:
mail:
configs:
- host: email-smtp.ap-southeast-1.amazonaws.com
port: 465
username: youName
password: youPassword
properties:
"mail.smtp.auth": true
"mail.smtp.ssl.enable": true
"mail.smtp.starttls.enable": true
"mail.smtp.starttls.required": true
"mail.imap.ssl.socketFactory.fallback": false
protocol: smtps
default-encoding: UTF-8
- host: smtp.qq.com
port: 465
username: testmail@qq.com
password: youPassword
properties:
"mail.smtp.auth": true
"mail.smtp.ssl.enable": true
"mail.smtp.starttls.enable": true
"mail.smtp.starttls.required": true
"mail.imap.ssl.socketFactory.fallback": false
protocol: smtps
default-encoding: UTF-8
- host: smtp.exmail.qq.com
port: 465
username: testmail@exmail.com
password: youPassword
properties:
"mail.smtp.auth": true
"mail.smtp.ssl.enable": true
"mail.smtp.starttls.enable": true
"mail.smtp.starttls.required": true
"mail.imap.ssl.socketFactory.fallback": false
protocol: smtps
default-encoding: UTF-8