javaMailSender
基于spring-boot 2.x:
maven:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
interface:
public interface MailService {
void sendMail(String subject, String body, String from, String[] to) throws Exception;
void sendMail(String subject, String body, String from, String[] cc, String[] to) throws Exception;
void sendMail(String subject, String body, boolean html, String from, String[] cc, String[] to) throws Exception;
void sendMail(String subject, String body, boolean html, String from, String[] cc, String[] to, List<String> attachPathList) throws Exception;
}
impl:
@Service
@Slf4j
public class MailServiceImpl implements MailService {
@Autowired
private JavaMailSenderImpl javaMailSender;
/**
* 发送邮件
*
* @param subject 主题
* @param body 内容
* @param from 发件人
* @param to 收件人[多个]
*/
public void sendMail(String subject, String body, String from, String[] to) throws Exception {
sendMail(subject, body, from, null, to);
}
/**
* 发送邮件
*
* @param subject 主题
* @param body 内容
* @param from 发件人
* @param cc 抄送人[多个]
* @param to 收件人[多个]
*/
public void sendMail(String subject, String body, String from, String[] cc, String[] to) throws Exception {
sendMail(subject, body, false, from, cc, to);
}
/**
* 发送邮件
*
* @param subject 主题
* @param body 内容
* @param html 是否为html格式
* @param from 发件人
* @param cc 抄送人[多个]
* @param to 收件人[多个]
*/
public void sendMail(String subject, String body, boolean html, String from, String[] cc, String[] to) throws Exception {
sendMail(subject, body, html, from, cc, to, null);
}
/**
* 发送邮件
*
* @param subject 主题
* @param body 内容
* @param html 是否为html格式
* @param from 发件人
* @param cc 抄送人[多个]
* @param to 收件人[多个]
* @param attachPathList 附件路径
*/
public void sendMail(String subject, String body, boolean html, String from, String[] cc, String[] to, List<String> attachPathList) throws Exception {
try {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
helper.setSubject(subject);
helper.setFrom(new InternetAddress(from));
helper.setSentDate(new Date());
// 设置收件人地址
helper.setTo(to);
// 设置抄送人
if (!StringUtils.isEmpty(cc)) {
helper.setCc(cc);
}
// 设置正文
helper.setText(body, html);
// 设置附件
if (!CollectionUtils.isEmpty(attachPathList)) {
for (String attachPath : attachPathList) {
FileDataSource fileDataSource = new FileDataSource(attachPath);
helper.addAttachment(fileDataSource.getName(), fileDataSource);
}
}
// 正式发送邮件
javaMailSender.send(mimeMessage);
} catch (Exception e) {
e.printStackTrace();
log.error("发送邮件失败:{}", e.getMessage());
throw e;
}
}
config是基于application.xml/yml配置
example1:
指定465端口, 可以不填默认使用25端口
spring:
# 邮件发送配置(改为自己的账号和密码)
mail:
host: xxx
username: xxx
password: xxx
default-encoding: UTF-8
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
socketFactory:
port: 465
class: javax.net.ssl.SSLSocketFactory
fallback: false
example2:
简单配置
spring:
# 邮件发送配置(改为自己的账号和密码)
mail:
host: xxx
port: 25
username: xxx
password: xxx
default-encoding: UTF-8