配置文件:
spring:
mail:
#主题前缀
mailHead: 【xxx管理平台】
#配置smtp服务器地址
host: smtp.xxxxx.cn
#协议 默认就是smtp
protocol: smtp
#发送者邮箱
username: [email protected]
#配置密码或者授权码
password: xxxxxx
#smtp的指定端口(465或587)
port: 465
#默认的邮件编码为UTF-8
default-encoding: UTF-8
#配置SSL 加密工厂
properties:
mail:
smtp:
auth: true
ssl:
enable: true
trust: ${spring.mail.host}
工具类
package org.isbg.common.util;
import cn.hutool.core.convert.Convert;
import lombok.extern.slf4j.Slf4j;
import org.isbg.common.constant.SymbolConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;
/**
* 发邮件
*
*/
@Slf4j
@Component
public class EmailUtil {
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.mailHead:}")
private String mailHead;
@Value("${spring.mail.username}")
private String fromUser;
/**
* 简单文本邮件
*
* @param toUsers 收件人(多人用英文逗号分割:"[email protected],[email protected],[email protected]")
* @param subject 主题
* @param content 内容
*/
public void sendSimpleMail(String toUsers, String subject, String content) {
sendSimpleMail(toUsers, null, subject, content);
}
/**
* 简单文本邮件
*
* @param toUsers 收件人(多人用英文逗号分割:"[email protected],[email protected],[email protected]")
* @param ccUsers 抄送人(多人用英文逗号分割:"[email protected],[email protected],[email protected]")
* @param subject 主题
* @param content 内容
*/
public void sendSimpleMail(String toUsers, String ccUsers, String subject, String content) {
//创建SimpleMailMessage对象
SimpleMailMessage message = new SimpleMailMessage();
//邮件发送人
message.setFrom(fromUser);
//邮件接收人
message.setTo(toUsers.split(SymbolConstant.COMMA));
//邮件抄送人
if (ObjectUtils.isNotEmpty(ccUsers)) {
message.setCc(ccUsers.split(SymbolConstant.COMMA));
}
//邮件主题
message.setSubject(mailHead + subject);
//邮件内容
message.setText(content);
//发送邮件
mailSender.send(message);
}
/**
* html邮件
*
* @param toUsers 收件人(多人用英文逗号分割:"[email protected],[email protected],[email protected]")
* @param subject 主题
* @param content 内容
*/
public void sendHtmlMail(String toUsers, String subject, String content) throws MessagingException {
sendHtmlMail(toUsers, null, subject, content);
}
/**
* html邮件
*
* @param toUsers 收件人(多人用英文逗号分割:"[email protected],[email protected],[email protected]")
* @param ccUsers 抄送人(多人用英文逗号分割:"[email protected],[email protected],[email protected]")
* @param subject 主题
* @param content 内容
* @throws MessagingException
*/
public void sendHtmlMail(String toUsers, String ccUsers, String subject, String content) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper messageHelper;
messageHelper = new MimeMessageHelper(message, true);
//邮件发送人
messageHelper.setFrom(fromUser);
//邮件接收人
messageHelper.setTo(toUsers.split(SymbolConstant.COMMA));
//邮件抄送人
if (ObjectUtils.isNotEmpty(ccUsers)) {
messageHelper.setCc(ccUsers.split(SymbolConstant.COMMA));
}
//邮件主题
message.setSubject(mailHead + subject);
//邮件内容,html格式
messageHelper.setText(content, true);
//发送
mailSender.send(message);
}
/**
* 带附件的邮件
*
* @param toUsers 收件人(多人用英文逗号分割:"[email protected],[email protected],[email protected]")
* @param subject 主题
* @param content 内容
* @param filePath 附件地址
* @throws MessagingException
*/
public void sendAttachFileMailByPath(String toUsers, String subject, String content, String filePath) throws MessagingException {
sendAttachFileMailByPath(toUsers, null, subject, content, filePath);
}
/**
* 带附件的邮件
*
* @param toUsers 收件人(多人用英文逗号分割:"[email protected],[email protected],[email protected]")
* @param subject 主题
* @param content 内容
* @param filePathList 附件地址列表
* @throws MessagingException
*/
public void sendAttachFileMailByPath(String toUsers, String subject, String content, List filePathList) throws MessagingException {
sendAttachFileMailByPath(toUsers, null, subject, content, filePathList);
}
/**
* 带附件的邮件
*
* @param toUsers 收件人(多人用英文逗号分割:"[email protected],[email protected],[email protected]")
* @param ccUsers 抄送人(多人用英文逗号分割:"[email protected],[email protected],[email protected]")
* @param subject 主题
* @param content 内容
* @param filePath 附件地址
* @throws MessagingException
*/
public void sendAttachFileMailByPath(String toUsers, String ccUsers, String subject, String content, String filePath) throws MessagingException {
sendAttachFileMailByPath(toUsers, ccUsers, subject, content, Convert.toList(String.class, filePath));
}
/**
* 带附件的邮件
*
* @param toUsers 收件人(多人用英文逗号分割:"[email protected],[email protected],[email protected]")
* @param ccUsers 抄送人(多人用英文逗号分割:"[email protected],[email protected],[email protected]")
* @param subject 主题
* @param content 内容
* @param filePathList 附件地址列表
* @throws MessagingException
*/
public void sendAttachFileMailByPath(String toUsers, String ccUsers, String subject, String content, List filePathList) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(fromUser);
//邮件接收人
helper.setTo(toUsers.split(SymbolConstant.COMMA));
//邮件抄送人
if (ObjectUtils.isNotEmpty(ccUsers)) {
helper.setCc(ccUsers.split(SymbolConstant.COMMA));
}
helper.setSubject(mailHead + subject);
helper.setText(content, true);
for (String filePath : filePathList) {
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = FileUtil.getName(filePath);
helper.addAttachment(fileName, file);
}
mailSender.send(message);
}
/**
* 带附件的邮件
*
* @param toUsers 收件人(多人用英文逗号分割:"[email protected],[email protected],[email protected]")
* @param subject 主题
* @param content 内容
* @param file 附件
* @throws MessagingException
*/
public void sendAttachFileMail(String toUsers, String subject, String content, File file) throws MessagingException {
sendAttachFileMail(toUsers, null, subject, content, file);
}
/**
* 带附件的邮件
*
* @param toUsers 收件人(多人用英文逗号分割:"[email protected],[email protected],[email protected]")
* @param subject 主题
* @param content 内容
* @param fileList 附件列表
* @throws MessagingException
*/
public void sendAttachFileMail(String toUsers, String subject, String content, List fileList) throws MessagingException {
sendAttachFileMail(toUsers, null, subject, content, fileList);
}
/**
* 带附件的邮件
*
* @param toUsers 收件人(多人用英文逗号分割:"[email protected],[email protected],[email protected]")
* @param ccUsers 抄送人(多人用英文逗号分割:"[email protected],[email protected],[email protected]")
* @param subject 主题
* @param content 内容
* @param file 附件
* @throws MessagingException
*/
public void sendAttachFileMail(String toUsers, String ccUsers, String subject, String content, File file) throws MessagingException {
sendAttachFileMail(toUsers, ccUsers, subject, content, Convert.toList(File.class, file));
}
/**
* 带附件的邮件
*
* @param toUsers 收件人(多人用英文逗号分割:"[email protected],[email protected],[email protected]")
* @param ccUsers 抄送人(多人用英文逗号分割:"[email protected],[email protected],[email protected]")
* @param subject 主题
* @param content 内容
* @param fileList 附件列表
* @throws MessagingException
*/
public void sendAttachFileMail(String toUsers, String ccUsers, String subject, String content, List fileList) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(fromUser);
//邮件接收人
helper.setTo(toUsers.split(SymbolConstant.COMMA));
//邮件抄送人
if (ObjectUtils.isNotEmpty(ccUsers)) {
helper.setCc(ccUsers.split(SymbolConstant.COMMA));
}
helper.setSubject(mailHead + subject);
helper.setText(content, true);
for (File file : fileList) {
helper.addAttachment(file.getName(), file);
}
mailSender.send(message);
}
}