tags: [jmail]
categories: [邮件服务]
前言
最近项目需要做邮件相关功能,研究了下使用javamail来发送邮件的一般方式,其中有一些经验,记录下来,希望以后可以少走点弯路SpringBoot版本2.2.6
以QQ邮箱示例
- 登录QQ邮箱,设置 -> 账户 进入以下界面,将
IMAP/SMTP服务开启

- 记录该授权码,以后需要用到
邮件功能实现
从我目前了解的来看,可以发送四种邮件,分别是普通邮件,HTML格式的邮件,带附件的邮件,模板邮件,其中HTML格式的邮件和模板邮件的区别在于,模板邮件有点像JSP,可以支持语法的编写和属性注入
简单的归纳邮件发送方法如下
/*** 发送简单邮件** @param from 发送人* @param to 接收人* @param subject 主题* @param content 内容*/@SuppressWarnings("unused")private void sendSimpleMessage(String from, String to, String subject, String content){SimpleMailMessage message = new SimpleMailMessage();message.setFrom(from);message.setSubject(to);message.setTo(subject);message.setText(content);mailSender.send(message);}/*** 发送HTML邮件** @param from 发送人* @param to 接收人* @param subject 主题* @param content 内容*/@SuppressWarnings("unused")private void sendHtmlEmail(String from, String to, String subject, String content){MimeMessage message = mailSender.createMimeMessage();MimeMessageHelper helper;try {helper = new MimeMessageHelper(message, true);helper.setFrom(from);helper.setTo(to);helper.setSubject(subject);helper.setText(content, true);} catch (MessagingException e) {log.error("HTML邮件发送异常", e);throw new BusinessException(ExceptionEnum.EMAIL_SEND_ERROR);}mailSender.send(message);}/*** 发送模板邮件** @param from 发件人* @param to 收件人* @param subject 主题* @param templateValue 模板map*/private void sendTemplateEmail(String from, String to, String subject, Map<String, Object> templateValue){try {MimeMessage mimeMessage = mailSender.createMimeMessage();MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);mimeMessageHelper.setFrom(from);mimeMessageHelper.setTo(to);mimeMessageHelper.setSubject(subject);Context context = new Context();context.setVariables(templateValue);String text = templateEngine.process(captchaTemplate,context);mimeMessageHelper.setText(text, true);mailSender.send(mimeMessage);}catch (MessagingException e) {e.printStackTrace();}}
注意
- HTML邮件需要将整个HTML内容作为content来导入,所以实际生产中最好还是作为配置或者第三方文件导入,不宜写入代码中
- 模板文件应该导入
模板邮件发送示例
使用模板需要导入依赖,我们使用的是
thymeleaf模板需要导入依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
spring:# thymeleafthymeleaf:mode: LEGACYHTML5# 这里去除缓存,生产需要开启cache: false
配置文件配置,这自然是我们自定义的属性
email:sender: 'bwensun@foxmail.com'# 邮件thymeleaf模板路径template:captcha: /mail/captcha
配置模板路径和模板文件
- 在
resources目录下建立templates文件夹,该文件夹是SpringBoot几个默认的文件夹,templates下建立mail文件夹,mail文件夹下存放模板文件 编写模板文件如下
<!DOCTYPE html><html lang="en" xmlns:th="http://www.thymeleaf.org"><head><meta charset="UTF-8"><title>Title</title></head><body><tablewidth="100%"height="100%"cellpadding="0"cellspacing="0"bgcolor="#f5f6f7"><tbody><tr><td height="50"></td></tr><tr><td align="center" valign="top"><tablewidth="600"cellpadding="0"cellspacing="0"bgcolor="#ffffff"style="border: 1px solid #f1f2f5;"class="main-content"><tbody><tr><tdcolspan="3"height="54"style="text-align: center; background-color: #47bb9b;"><divstyle="font-family: Helvetica, Arial, sans-serif;font-weight: bold;font-size: 28px;line-height: 28px;color: #fff;">Zoombar</div></td></tr><tr><td width="20"></td><td align="left"><table cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td colspan="3" height="20"></td></tr><tr><td th:text="${name}"bgcolor="#ffffff"align="left"style="background-color: #ffffff;font-size: 17px;color: #7b7b7b;padding: 28px 0 0 0;line-height: 25px;"></td></tr><tr><tdalign="left"valign="top"style="font-size: 14px;color: #7b7b7b;line-height: 25px;font-family: Hiragino Sans GB;padding: 20px 0px 20px 0px;">你此次注册的验证码如下,请在 30分钟内输入验证码,或直接点击右侧的按钮,以进行下一步操作。如非你本人操作,请忽略此邮件。</td></tr><tr><tdstyle="border-bottom: 1px #f1f4f6 solid;padding: 0 0 40px 0;"align="center"><table border="0" cellspacing="0" cellpadding="0"><tbody><tr><td><span style="font-family: Hiragino Sans GB;"><div th:text="${captcha}"style="padding: 10px 18px 10px 18px;border-radius: 3px;text-align: center;text-decoration: none;background-color: #ecf4fb;color: #47bb9b;font-size: 20px;font-weight: 700;letter-spacing: 2px;margin: 0;white-space: nowrap;"></div></span></td><td rowspan="3" width="20px;"></td><td><ahref="http://baidu.com"style="display: inline-block;text-decoration: none;width: 150px;"><divstyle="padding: 10px 18px 10px 18px;border-radius: 3px;text-align: center;text-decoration: none;background-color: #47bb9b;color: #ecf4fb;font-size: 17px;font-weight: 400;letter-spacing: 2px;margin: 0;white-space: nowrap;">打开激活页面</div></a></td></tr></tbody></table></td></tr><tr><td colspan="3" height="20"></td></tr></tbody></table></td></tr><tr><td></td><td valign="top">此邮件来自<a href="www.zoombar.fun" style="color: #47bb9b;">Zoombar</a>网站,用于发送用户注册所需的验证码。</td><td height="50"></td></tr></tbody></table></td></tr></tbody></table></body></html>
- 在
调用发送邮件方法
- 我们以发送验证码邮件为例,传入用户名和验证码,就会以默认邮箱去发送验证码
/*** 发送验证码邮件** @param name 用户名* @param to 收件人* @param captcha 验证码*/public void sendCaptchaEmail(String name, String to, String captcha) {String subject = "ZoomBar账号注册";Map<String, Object> templateValue = new HashMap<>();String username = "你好, " + name;templateValue.put("name", username);templateValue.put("captcha", captcha);sendTemplateEmail(emailSender, to, subject, templateValue);}
- 我们以发送验证码邮件为例,传入用户名和验证码,就会以默认邮箱去发送验证码
-
后面遇到的问题
相关
- spring boot 阿里云发送邮件,使用 qq 邮箱的 SMTP 服务,端口由 25 换成 465
