-
开启POP3/SMTP服务
-
获取邮箱授权码
-
纯文本邮件发送
-
文本内容为html发送
-
邮件添加附件发送
开启POP3/SMTP服务
获取邮箱授权码
纯文本邮件发送
-
建立一个普通项目,导入依赖(使用了parent,所以版本没管)
<!-- 邮件依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency> -
配置邮件发送
spring:
mail:
#smtp服务主机 qq邮箱则为smtp.qq.com
host: smtp.qq.com
#服务协议
protocol: smtp
# 编码集
default-encoding: UTF-8
#发送邮件的qq邮箱
username: 999999@qq.com
#授权码
password: xxx(16位)
test-connection: true
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true -
配置发送信息:其中SimpleEmailEntity类是获取前端上传的参数,参数来源不重要,from表示发送人的账号,subject表示邮件标题,text表示邮件内容,tos表示发给谁,里面可以是多个人的邮箱如["123@qq.com","456@qq.com"]
@Service
public class EmailServiceImpl extends LogConf implements EmailService {
@Value("${spring.mail.username}")
private String from;
@Autowired
private JavaMailSender mailSender;
@Override
public void sendSimpleMail(SimpleEmailEntity simpleEmailEntity) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setSubject(simpleEmailEntity.getSubject());
message.setText(simpleEmailEntity.getContent());
message.setTo(simpleEmailEntity.getTos());
logger.info("开始发送信息给:{}",simpleEmailEntity.getTos());
try {
mailSender.send(message);
}catch (Exception e){
logger.info("发送信息失败,原因:{}",e.getMessage());
}
}
}相关实体和内容
@Data
public class SimpleEmailEntity {
/**
* 主题
*/
private String subject;
/**
* 主题内容
*/
private String content;
/**
* 接收人邮箱列表
*/
private String[] tos;
}{
"tos": ["1806731196@qq.com"],
"subject": "Hello 小..",
"content": "亲爱的小..,万分感谢你能在忙碌之中抽出时间来看这封信,虽然这封信啥都没说,哈哈哈"
} -
发送html类邮件(可以实现发送图片),关键点:定义了MimeMessage、MimeMessageHelper,图片添加时有一个key,这个key是html中图片的cid值,这点不对应不能发送图片
@Override
public void sendImage(SimpleEmailEntity simpleEmailEntity) {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper mineHelper = null;
try {
mineHelper= new MimeMessageHelper(message, true);
mineHelper.setFrom(from);
mineHelper.setSubject(simpleEmailEntity.getSubject());
mineHelper.setText("<html><body><h1>发送html测试</h1><img src='cid:test1'></img></body></html>", true);
mineHelper.setTo(simpleEmailEntity.getTos());
FileSystemResource file = new FileSystemResource(new File("E:\\DESK\\img.jpg"));
logger.info("file:{}",file);
mineHelper.addInline("test1", file);
} catch (MessagingException e) {
logger.info("定义MimeMessageHelper失败:{}",e.getMessage());
}
logger.info("开始发送信息给:{}",simpleEmailEntity.getTos());
try {
mailSender.send(message);
}catch (Exception e){
logger.info("发送信息失败,原因:{}",e.getMessage());
}
} -
发送带附件的邮件
@Override
public void sendem(SimpleEmailEntity simpleEmailEntity) {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper mineHelper = null;
try {
mineHelper= new MimeMessageHelper(message, true);
mineHelper.setFrom(from);
mineHelper.setSubject(simpleEmailEntity.getSubject());
mineHelper.setText("甜蜜蜜啊甜蜜蜜,冰雪甜蜜蜜<html><body><h1>发送html测试</h1><img src='cid:test1'></img></body></html>", true);
mineHelper.setTo(simpleEmailEntity.getTos());
FileSystemResource file = new FileSystemResource(new File("E:\\DESK\\img.jpg"));
logger.info("file:{}",file);
mineHelper.addInline("test1", file);
mineHelper.addAttachment("会计科目.xls",new File("E:\\DESK\\会计科目.xls"));
} catch (MessagingException e) {
logger.info("定义MimeMessageHelper失败:{}",e.getMessage());
}
logger.info("开始发送信息给:{}",simpleEmailEntity.getTos());
try {
mailSender.send(message);
}catch (Exception e){
logger.info("发送信息失败,原因:{}",e.getMessage());
}
}
可以把发送邮件的功能提取成一个工具类,要使用的时候调用就行了