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

- 记录该授权码,以后需要用到
邮件功能实现
从我目前了解的来看,可以发送四种邮件,分别是普通邮件,HTML 格式的邮件,带附件的邮件,模板邮件,其中 HTML 格式的邮件和模板邮件的区别在于,模板邮件有点像 JSP,可以支持语法的编写和属性注入
简单的归纳邮件发送方法如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
@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); }
@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); }
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
模板需要导入依赖
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
|
1 2 3 4 5 6
| spring: thymeleaf: mode: LEGACYHTML5 cache: false
|
- 配置文件配置,这自然是我们自定义的属性
1 2 3 4 5 6
| email: sender: "bwensun@foxmail.com"
template: captcha: /mail/captcha
|
- 配置模板路径和模板文件
- 在
resources
目录下建立templates
文件夹,该文件夹是 SpringBoot 几个默认的文件夹,templates
下建立mail
文件夹,mail 文件夹下存放模板文件
- 编写模板文件如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
| <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8" /> <title>Title</title> </head> <body> <table width="100%" height="100%" cellpadding="0" cellspacing="0" bgcolor="#f5f6f7" > <tbody> <tr> <td height="50"></td> </tr> <tr> <td align="center" valign="top"> <table width="600" cellpadding="0" cellspacing="0" bgcolor="#ffffff" style="border: 1px solid #f1f2f5;" class="main-content" > <tbody> <tr> <td colspan="3" height="54" style="text-align: center; background-color: #47bb9b;" > <div style=" 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> <td align="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> <td style=" 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> <a href="http://baidu.com" style=" display: inline-block; text-decoration: none; width: 150px; " > <div style=" 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>
|
- 调用发送邮件方法
- 我们以发送验证码邮件为例,传入用户名和验证码,就会以默认邮箱去发送验证码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
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); }
|
- 结果如下

后面遇到的问题
原以为部署上去会一样,然而。。
第一个问题是找不到模板路径,
相关
- SpringBoot 发送邮件
- spring boot 阿里云发送邮件,使用 qq 邮箱的 SMTP 服务,端口由 25 换成 465