1.配置
导入依赖以及在yml中写好邮箱的配置信息
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
mail:
host: smtp.exmail.qq.com //QQ 邮件服务器
username: 用户名
password: 密码
properties.mail.smtp.port: 465
from: 发件人邮箱
properties.mail.smtp.starttls.enable: true
properties.mail.smtp.starttls.required: true
properties.mail.smtp.ssl.enable: true
default-encoding: utf-8
2.获取自己的附件
这一步根据自己的业务代码获取需要发送邮件的附件文件,(流的格式)
ByteArrayOutputStream byteArrayOutputStream = fileUtilService.getFile(fileName);
//将获取的文件流转换为邮件service方法的参数ByteArrayInputStream格式
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
3.文件名参数拼接