依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
springboot配置:
spring:
mail:
host: smtp.exmail.qq.com
username: xxxxxx@xxxxxx.com
password: xxxxxxx
protocol: smtp
发送邮件
获取MimeMessage
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
设置发件人
发件人地址与配置文件中的username一致
messageHelper.setFrom(xxxxxx@xxxxxx.com);
设置发件人昵称
messageHelper.setFrom(new InternetAddress(xxxxxx@xxxxxx.com, nickName));
设置收件人
单个收件人
messageHelper.setTo(xxxx@xxx.com);
多个收件人
messageHelper.setTo(new String[]{xxxx@xxx.com,xxxx@xxx.com});
设置抄送人
messageHelper.setCc(xxxx@cccc.com);
多个抄送人,参数与收件人一致
设置私密抄送人
messageHelper.setBcc(xxxx@cccc.com);
多个私密抄送人,参数与收件人一致
设置邮件主题
messageHelper.setSubject(subject);
设置邮件内容
// isHtmlText邮件内容是否为html格式,支持仅部分内容为html格式
messageHelper.setText(contentText, ishtmlText);
添加附件
public void addAttachment(String attachmentFilename, DataSource dataSource) throws MessagingException;
public void addAttachment(String attachmentFilename, File file) throws MessagingException ;
public void addAttachment(String attachmentFilename, InputStreamSource inputStreamSource) throws MessagingException;
public void addAttachment(String attachmentFilename, InputStreamSource inputStreamSource, String contentType) throws MessagingException;
例:
messageHelper.addAttachment(附件名称, new ByteArrayResource(文件字节数组));
添加内嵌
如一张图片,展示在邮件正文中,而非在附件中
public void addInline(String contentId, DataSource dataSource) throws MessagingException;
public void addInline(String contentId, File file) throws MessagingException;
public void addInline(String contentId, Resource resource) throws MessagingException;
public void addInline(String contentId, InputStreamSource inputStreamSource, String contentType)
例:
messageHelper.addInline("p02",new FileSystemResource(new File("/Users/gamedev/Desktop/瑞文.jpg")));
发送邮件
javaMailSender.send(mimeMessage);
不使用AutoConfiguration配置JavaMailSender
邮件配置位于数据库,启动时加载到缓存中
@Component
public class JavaMailConfig {
private static final String MAIL_HOST = "MAIL_HOST";
private static final String MAIL_PORT = "MAIL_PORT";
public static final String MAIL_USERNAME = "MAIL_USERNAME";
private static final String MAIL_PASSWORD = "MAIL_PASSWORD";
private static final String MAIL_PROTOCOL = "MAIL_PROTOCOL";
private static final String MAIL_ENCODING = "MAIL_ENCODING";
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
@Autowired
private CacheService cacheService;
@Bean
@ConditionalOnMissingBean
public JavaMailSenderImpl mailSender() {
JavaMailSenderImpl sender = new JavaMailSenderImpl();
applyProperties(sender);
return sender;
}
public void refreshJavaMailConfig() {
JavaMailSenderImpl javaMailSender = BeanFactory.getBean(JavaMailSenderImpl.class);
if (javaMailSender == null) {
javaMailSender = new JavaMailSenderImpl();
}
applyProperties(javaMailSender);
}
private void applyProperties(JavaMailSenderImpl sender) {
sender.setHost(cacheService.getParamValueCache(MAIL_HOST));
String mailPort = cacheService.getParamValueCache(MAIL_PORT);
if (StringUtils.isNotBlank(mailPort)) {
sender.setPort(Integer.parseInt(mailPort));
}
sender.setUsername(cacheService.getParamValueCache(MAIL_USERNAME));
sender.setPassword(cacheService.getParamValueCache(MAIL_PASSWORD));
sender.setProtocol(cacheService.getParamValueCache(MAIL_PROTOCOL));
String mailEncoding = cacheService.getParamValueCache(MAIL_ENCODING);
if (StringUtils.isNotBlank(mailEncoding)) {
sender.setDefaultEncoding(mailEncoding);
} else {
sender.setDefaultEncoding(DEFAULT_CHARSET.name());
}
}
}