1、引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.1.16.RELEASE</version>
</dependency>
2、配置邮箱信息(授权码的获取可以查看QQ邮箱->设置,里面可以生成授权码)
# 名称不要改变,依赖包会自动寻找spring.mail开头的配置信息
# 用户名
spring.mail.username=
#授权码
spring.mail.password=
# 协议 一般是smtp.exmail.qq.com
spring.mail.host=
# 端口
spring.mail.port=
# smtp配置
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
3、因为配置信息的前缀,使用Spring默认的邮箱配置前缀,所以不需要自己去创建实体类去配置
4、工具类
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
/**
* 邮件工具类
*
* @author chenkailiang
* @date 2021/1/8
*/
@Service
@Slf4j
public class SendMailUtil {
/**
* 发件人
*/
@Value("${xxx}")
private String mailAddress;
@Autowired
private JavaMailSender mailSender;
/**
* 发送普通文本邮件
* @param from 发件人,如果未指定则使用默认配置的 service28@bulkea.oristand.com
* @param to 收件人
* @param cc 抄送
* @param subject 邮件主题
* @param content 邮件内容
*/
public void sendSimpleMail(String from,String[] to , String[] cc,String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
//发件人
String realFrom = from;
if(StringUtils.isBlank(from)){
realFrom = mailAddress;
}
message.setFrom(realFrom);
//收信人
message.setTo(to);
//抄送
message.setCc(cc);
//主题
message.setSubject(subject);
//文本
message.setText(content);
mailSender.send(message);
log.info("发送邮件成功:发件人:{},收件人:{},抄送:{},主题:{}",realFrom, to,cc, subject);
}
/**
* 发送html邮件
* @param from 发件人
* @param to 收件人
* @param cc 抄送
* @param subject 主题
* @param content 内容
*/
public void sendHtmlMail(String from ,String[] to,String[] cc, String subject, String content) {
//使用MimeMessage,MIME协议
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper;
String realFrom = from;
try {
helper = new MimeMessageHelper(message, true);
if(StringUtils.isBlank(from)){
realFrom = mailAddress;
}
helper.setFrom(realFrom);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
mailSender.send(message);
log.info("发送HTML邮件成功:发件人:{},收件人:{},抄送:{},主题:{}",realFrom, to,cc, subject);
} catch (MessagingException e) {
e.printStackTrace();
log.error("发送HTML邮件失败:发件人:{},收件人:{},抄送:{},主题:{}",realFrom, to,cc, subject);
}
}
/**
* 发送带附件邮件
* @param from 发件邮箱
* @param to 收件人
* @param cc 抄送
* @param subject 主题
* @param content 内容
* @param filePath 附件地址
*/
public void sendAttachmentMail(String from,String[] to, String[] cc,String subject, String content, String filePath) {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper;
String realFrom = from;
try {
//true代表支持多组件,如附件,图片等
helper = new MimeMessageHelper(message, true);
if(StringUtils.isBlank(from)){
realFrom = mailAddress;
}
helper.setFrom(realFrom);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = file.getFilename();
//添加附件,可多次调用该方法添加多个附件
helper.addAttachment(fileName, file);
mailSender.send(message);
log.info("发送带附件邮件成功:发件人:{},收件人:{},抄送:{},主题:{}",realFrom, to,cc, subject);
} catch (MessagingException e) {
e.printStackTrace();
log.error("发送带附件邮件成功:发件人:{},收件人:{},抄送:{},主题:{}",realFrom, to,cc, subject);
}
}
/**
* 发送带图片邮件
* @param from 发件人
* @param to 收件人
* @param cc 抄送
* @param subject 主题
* @param content 内容
* @param rscPath 图片路径
* @param rscId 图片id
*/
public void sendInlineResourceMail(String from,String[] to, String[] cc,String subject, String content, String rscPath, String rscId) {
MimeMessage message = mailSender.createMimeMessage();
String realFrom = from;
MimeMessageHelper helper;
try {
helper = new MimeMessageHelper(message, true);
if(StringUtils.isBlank(from)){
realFrom = mailAddress;
}
helper.setFrom(realFrom);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource res = new FileSystemResource(new File(rscPath));
//重复使用添加多个图片
helper.addInline(rscId, res);
mailSender.send(message);
log.info("发送带图片邮件成功:发件人:{},收件人:{},抄送:{},主题:{}",realFrom, to,cc, subject);
} catch (MessagingException e) {
e.printStackTrace();
log.error("发送带图片邮件成功:发件人:{},收件人:{},抄送:{},主题:{}",realFrom, to,cc, subject);
}
}
}