邮箱配置
项目配置:
spring:
## 邮箱配置(qq)# 其他邮件服务器参考配置,自己试试
mail:
protocol: smtp
host: smtp.qq.com #发送邮件服务器
port: 465 # 端口号465
username: xxx # 发送邮件的邮箱地址
password: xxx # 客户端授权码,不是邮箱密码,这个在qq邮箱设置里面自动生成的
default-encoding: utf-8
properties.mail.smtp.port: 465 # 端口号465或587
properties.mail.smtp.starttls.enable: true
properties.mail.smtp.starttls.required: true
properties.mail.smtp.ssl.enable: true
properties.mail.smtp.auth.enable: true
配置好后即可使用IMailService进行邮件发送。
IMailService下的接口方法说明
均没有返回值。
统一参数说明:
tos 收件人列表
subject 主题
content 内容(文字内容或html内容)
filePaths 服务器中的文件路径列表(绝对路径)
files 文件列表(附件名称默认为文件名)(或可自定义附件名称文件的列表)
| 方法 | 说明 |
|---|---|
sendSimpleMail(List<String> tos, String subject, String content) | 发送文本邮件 |
sendHtmlMail(List<String> tos, String subject, String content) | 发送HTML邮件 |
sendAttachmentsMail(List<String> tos, String subject, String content, String... filePath) | 发送带附件的邮件 |
sendAttachmentsMail(List<String> tos, String subject, String content, File... files) | 发送带附件的邮件 |
sendAttachmentsMail(List<String> tos, String subject, String content, Map<String, File> files) | 带附件 的邮件(自定义文件名) |
栗子:
@Autowired
private IMailService iMailService;
public void send(){
// 1.文本邮件
iMailService.sendSimpleMail(Arrays.asList("123@qq.com"),"你好","hello word!");
// 2.附件邮件
Map<String,File> files = new HashMap<>();
files.put("截图.png",new File("E:\\tmp\\1.png"));
files.put("肺腑之言.txt",new File("E:\\tmp\\废话.txt"));
iMailService.sendAttachmentsMail(Collections.singletonList("230f@ycv51.xyz"),
LocalDateTime.now().format(DatePattern.CHINESE_DATE_TIME_FORMATTER),
"先生请查收",
files);
}