1 邮件配置部分
qq邮箱授权码获取
java 邮件属性配置
(1)写在代码中(任选其一,建议2)
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setHost("smtp.qq.com");
javaMailSender.setPort(25);
javaMailSender.setUsername("1464245418@qq.com");
javaMailSender.setPassword("yourAuthorizationCode");
Properties properties = new Properties();
properties.setProperty("mail.host", "smtp.qq.com");
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.smtp.port", "25");
properties.setProperty("mail.smtp.socketFactory.port", "25");
javaMailSender.setJavaMailProperties(properties);
(2)写在.properties文件中
spring.mail.host=smtp.qq.com
spring.mail.port=25
spring.mail.username=1464245418@qq.com
spring.mail.password=yourAuthorizationCode
2 邮件简单发送
主要使用JavaMail包,邮件内容使用thymeleaf
<dependencies>
<!--SpringBoot 起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--JavaMail-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!--thymeleaf模板-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
发送邮件
@Resource
private TemplateEngine templateEngine;
@Resource
private JavaMailSenderImpl javaMailSender;
/**
* @description 仅发送简单邮件.
* @author liuhongchen
*/
public void sendSimpleMail() {
// 创建MIMEMessage对象
MimeMessage message = javaMailSender.createMimeMessage();
try {
//true表示需要创建一个multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
String nick;
try {
nick = MimeUtility.encodeText("测试邮件");
} catch (UnsupportedEncodingException e) {
MailService.log.error("邮件nickName有问题:" + e.getMessage());
throw new ServiceException(MessageCode.SYSTEM_ERROR);
}
// 邮件发送信息(from,to,cc)
helper.setFrom(new InternetAddress(nick + "<1464245418@qq.com>"));
helper.setTo(new String[]{"1464245418@qq.com"});
helper.setCc(new String[]{"1464245418@qq.com"});
helper.setSubject("这是邮件的主题");
// 注入邮件内容模板
helper.setText("<h1>This is actual message</h1>", "text/html", true);
try {
javaMailSender.setPassword("记得填密码");
javaMailSender.setUsername("1464245418@qq.com");
javaMailSender.send(message);
MailService.log.info("html邮件发送成功");
} catch (Exception e) {
MailService.log.error("邮件发送有问题:" + e.getMessage());
}
} catch (Exception e) {
MailService.log.error("发送html邮件时发生异常!", e);
}
}
3 邮件带附件、内嵌图片(使用thymeleaf)
发送邮件代码
/**
* 发送带附件和内嵌图片的邮件.
*
* @param templateName thymeleaf模板名
* @param mailAccount 邮箱账号密码
* @param contextVars 邮件内容变量
* @param inlineImgMap 内嵌图片Map
* @param attachmentList 附件List
* @return void
* @author liuhongchen
* @date 2020/8/21 10:36 上午
*/
@Async
public void sendSimpleMail(String templateName, Map<String, Object> contextVars,
Map<String, String> inlineImgMap, Map<String, DataSource> attachmentList) {
// 解决:放置附件名过长被截断,从而产生乱码问题
System.getProperties().setProperty("mail.mime.splitlongparameters", "false");
MimeMessage message = javaMailSender.createMimeMessage();
try {
//true表示需要创建一个multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
String nick;
try {
nick = MimeUtility.encodeText("烛龙监控平台");
} catch (UnsupportedEncodingException e) {
MailService.log.error("邮件nickName有问题:" + e.getMessage());
throw new ServiceException(MessageCode.SYSTEM_ERROR);
}
// 邮件发送信息(from,to,cc)
helper.setFrom(new InternetAddress(nick + "<1464245418@qq.com>"));
helper.setTo(new String[]{"1464245418@qq.com"});
helper.setCc(new String[]{"1464245418@qq.com"});
helper.setSubject("这是邮件的主题");
// 注入邮件模板(注入变量和邮件模板内容)
Context context = new Context();
context.setVariables(contextVars);
String emailContext = templateEngine.process(templateName, context);
helper.setText(emailContext, true);
// 附件
if (attachmentList != null) {
for (Entry<String, DataSource> fileEntry : attachmentList.entrySet()) {
helper.addAttachment(fileEntry.getKey(), fileEntry.getValue());
}
}
// 插入内嵌资源
if (inlineImgMap != null) {
for (Entry<String, String> imgEntry : inlineImgMap.entrySet()) {
helper.addInline(imgEntry.getKey(), new File(imgEntry.getValue()));
}
}
try {
javaMailSender.setPassword("记得填密码");
javaMailSender.setUsername("1464245418@qq.com");
javaMailSender.send(message);
MailService.log.info("html邮件发送成功");
} catch (Exception e) {
MailService.log.error("邮件发送有问题:" + e.getMessage());
}
} catch (Exception e) {
MailService.log.error("发送html邮件时发生异常!", e);
}
}
thymeleaf模板
<!DOCTYPE html>
<html lang="zh-Hans" xmlns:th="http://www.thymeleaf.org" xmlns="http://www.w3.org/1999/html">
<head>
<meta charset="UTF-8"/>
</head>
<body>
<table th:if="${sendType eq 'preview'}">
<tr>
<td align="center"><img th:src="${previewAndroidTimeTop}" width="900" /></td>
</tr>
<tr >
<td align="center"><img th:src="${previewIosTimeTop}" width="900" /></td>
</tr>
</table>
</body>
</html>
- 其中的变量sendType在contextVar中设置
Map<String, Object> contextVars = new HashMap<>(16);
contextVars.put("sendType", "preview");
- 图片可以直接放置图片链接,但是会出现一个问题,邮件会出于保护隐私不显示图片,此时需要手动点击”下载图片“,才会显示。
使用cid的好处就是可以避免这一操作,但需要使用图片的绝对位置,后续需要删除该图片
Map<String, String> inlineImgMap = new HashMap<>(16);
inlineImgMap.put("androidTimeTop", "/Users/liuhongchen/code/androidTimeTop.png"));
inlineImgMap.put("iosTimeTop", "/Users/liuhongchen/code/iosTimeTop.png"));