pom:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
配置:
# 普通QQ邮箱
# 默认端口25,使用465端口时,需要添加配置:
# spring.mail.port=465
# spring.mail.properties.mail.smtp.ssl.enable=true
spring:
mail:
host: smtp.cntaiping.com #
port: 25
username: yusy02@xx.com # 邮箱账号
# password: eyhtbgdrujetbgfh # 授权码
password: XXXXX # 密码
properties:
mail:
smtp:
ssl: false
auth: true
starttls:
enable: true
required: false
default-encoding: UTF-8
# 配置常量
mail:
fromMail:
addr: yusy02@xxx.com # 发送人
receptionMail:
addr: yusy02@xxx.com # 接收人
toCCMail:
addr: yusy02@xxx.com # 抄送
自定义...
代码:
Service:
package com.XXXX.XXX.XXX.email;
/**
* yusy
*/
public interface MailService {
/**
* 发送纯文本邮件多人
* @param toAddr 发送给谁
* @param ccAddr 抄送
* @param title 标题
* @param content 内容
*/
public void sendTextMail(String toAddr[],String ccAddr[], String title, String content);
/**
* 发送 html 多人
* @param toAddr
* @param ccAddr 抄送
* @param title
* @param content 内容(HTML)
*/
public void sendHtmlMail(String toAddr[],String ccAddr[], String title, String content);
/**
* 发送待附件的邮件-多人
* @param toAddr
* @param ccAddr 抄送
* @param title
* @param content
* @param filePaths 附件地址
*/
public void sendAttachmentsMail(String toAddr[],String ccAddr[], String title, String content, String filePaths[]);
/**
* 发送文本中有静态资源(图片)的邮件-多人
* @param toAddr
* @param ccAddr 抄送
* @param title
* @param content
* @param rscPath 资源路径
* @param rscId 资源id (可能有多个图片)
*/
public void sendInlineResourceMail(String toAddr[], String ccAddr[],String title, String content, String rscPath, String rscId);
}
impl:
package com.XXXX.email;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.Service;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
@Service
public class MailServiceImpl implements MailService {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Resource
private JavaMailSender mailSender;
// 注入常量
@Value("${mail.fromMail.addr}")
private String from;
/**
* 发送文本邮件
*
* @param toAddr
* @param title
* @param content
*/
public void sendTextMail(String toAddr[], String title, String content) {
sendTextMail(toAddr, null, title, content);
}
/**
* 发送文本邮件
*
* @param toAddr 发送给谁
* @param title 标题
* @param content 内容
*/
@Override
public void sendTextMail(String toAddr[], String ccAddr[], String title, String content) {
// 纯文本邮件对象
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(toAddr);
message.setSubject(title);
message.setText(content);
//抄送
if (ccAddr != null && ccAddr.length > 0) {
message.setCc(ccAddr);
}
try {
mailSender.send(message);
logger.info("Text邮件已经发送。");
} catch (Exception e) {
logger.error("发送Text邮件时发生异常!", e);
}
}
/**
* 发送html邮件
*
* @param toAddr
* @param title
* @param content
*/
public void sendHtmlMail(String toAddr[], String title, String content) {
sendHtmlMail(toAddr, null, title, content);
}
/**
* 发送html邮件
*
* @param toAddr
* @param title
* @param content
*/
@Override
public void sendHtmlMail(String toAddr[], String ccAddr[], String title, String content) {
// html 邮件对象
MimeMessage message = mailSender.createMimeMessage();
try {
//true表示需要创建一个 multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(toAddr);
helper.setSubject(title);
helper.setText(content, true);
//抄送
if (ccAddr != null && ccAddr.length > 0) {
helper.setCc(ccAddr);
}
mailSender.send(message);
logger.info("html邮件发送成功");
} catch (MessagingException e) {
logger.error("发送html邮件时发生异常!", e);
}
}
/**
* 发送带附件的邮件
*
* @param toAddr
* @param title
* @param content
* @param filePath
*/
public void sendAttachmentsMail(String toAddr[], String title, String content, String filePath[]) {
sendAttachmentsMail(toAddr, null, title, content, filePath);
}
/**
* 发送带附件的邮件
*
* @param toAddr
* @param title
* @param content
* @param filePaths
*/
@Override
public void sendAttachmentsMail(String toAddr[], String ccAddr[], String title, String content, String filePaths[]) {
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(toAddr);
helper.setSubject(title);
helper.setText(content, true);
//抄送
if (ccAddr != null && ccAddr.length > 0) {
helper.setCc(ccAddr);
}
//附件
if (filePaths != null) {
for (String filePath : filePaths) {
FileSystemResource file = new FileSystemResource(new File(filePath));
helper.addAttachment(file.getFilename(), file);
}
}
mailSender.send(message);
logger.info("带附件的邮件已经发送。");
} catch (MessagingException e) {
logger.error("发送带附件的邮件时发生异常!", e);
}
}
public void sendInlineResourceMail(String toAddr[], String title, String content, String rscPath, String rscId) {
sendInlineResourceMail(toAddr, null, title, content, rscPath, rscId);
}
/**
* 发送正文中有静态资源(图片)的邮件
*
* @param toAddr
* @param title
* @param content
* @param rscPath
* @param rscId
*/
@Override
public void sendInlineResourceMail(String toAddr[], String ccAddr[], String title, String content, String rscPath, String rscId) {
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(toAddr);
helper.setSubject(title);
helper.setText(content, true);
//抄送
if (ccAddr != null && ccAddr.length > 0) {
helper.setCc(ccAddr);
}
FileSystemResource res = new FileSystemResource(new File(rscPath));
helper.addInline(rscId, res);
mailSender.send(message);
logger.info("嵌入静态资源的邮件已经发送。");
} catch (MessagingException e) {
logger.error("发送嵌入静态资源的邮件时发生异常!", e);
}
}
}