本文最后更新于:March 18, 2022 pm
SpringBoot框架中有两个非常重要的策略:开箱即用和约定优于配置。其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。
目录
导入依赖
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
|
准备配置
这里以QQ邮箱为例。
先网页登录QQ邮箱→设置→开启相应服务(POP3/SMTP)并生成授权码。再配置:
| spring: mail: username: 1111111@qq.com password: xxxxx host: smtp.qq.com properties.mail.smtp.ssl.enable: true
|
邮件模板
📢注意:必须放到 resource/templates
目录下。
codeEmail.html
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
| <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org/"> <head> <meta charset="UTF-8"> <title>墨水记忆-邮件注册</title> </head> <body> <div style="opacity: 1;"> <div style="background: white; width: 100%; max-width: 800px; margin: auto auto; border-radius: 5px; border:#1bc3fb 1px solid; overflow: hidden; -webkit-box-shadow: 0px 0px 20px 0px rgba(0, 0, 0, 0.12); box-shadow: 0px 0px 20px 0px rgba(0, 0, 0, 0.18);"> <header style="overflow: hidden;"> <center> <img style="width:50%;z-index: 666;" src="https://img-blog.csdnimg.cn/a458bf114f6f4912953f8b73eab00580.png"> </center> </header> <div style="padding: 5px 20px; "> <p style=" position: relative; color: white; float: left; z-index: 999; background: #1bc3fb; padding: 5px 30px; margin: -25px auto 0 ; box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.30) "> 注册邮件 </p> <br> <center> <h3> 来自 <span style=" text-decoration: none;color: #FF779A;">墨水记忆</span> 的邮件提醒 </h3> <p style="text-indent:2em;"> 您现在正在<a style=" text-decoration: none;color: #1bc3fb " target="_blank" href="http://tothefor.com/" rel="noopener"> 墨水记忆 </a>上进行账号注册。 <br>假如这不是您本人所申请, 则无需理会这封电子邮件。 </p> <div style="background: #fafafa repeating-linear-gradient(-45deg,#fff,#fff 1.125rem,transparent 1.125rem,transparent 2.25rem);box-shadow: 0 2px 5px rgba(0, 0, 0, 0.15);margin:20px 0px;padding:15px;border-radius:5px;font-size:14px;color:#555555;text-align: center; "> 请使用以下验证码完成后续注册流程:<br>
<span style=" color: #c847e3;font-weight: bolder;font-size: 25px;" th:text="${code}"></span><br> 注意:请您在收到邮件5分钟内使用,否则该验证码将会失效。 </div>
<br> <div style=" text-align: center; ">
<a style="text-transform: uppercase; text-decoration: none; font-size: 14px; background: #cb2edd; color: #FFFFFF; padding: 10px; display: inline-block; border-radius: 5px; margin: 10px auto 0;" target="_blank" href="http://tothefor.com/" rel="noopener">墨水记忆|传送门</a> </div> <p style="font-size: 12px;text-align: center;color: #999; "> 最好的记忆不如最淡的墨水。<br> © 2021 <a style="text-decoration:none; color:#1bc3fb " href="http://tothefor.com/" rel="noopener" target="_blank">墨水记忆</a> </p> <p></p> </center> </div> </div> </body> </html>
|
邮件发送工具类
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
| package com.tothefor.utils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Component; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context;
import javax.mail.internet.MimeMessage;
@Component public class emailSend {
@Autowired private JavaMailSenderImpl javaMailSender;
@Autowired private TemplateEngine templateEngine;
public void sendEmail(String code,String inMan) throws Exception{ MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
mimeMessageHelper.setSubject("墨水记忆-账号注册"); mimeMessageHelper.setTo(inMan); mimeMessageHelper.setFrom("2629828909@qq.com");
Context context = new Context(); context.setVariable("code",code);
String process = templateEngine.process("codeEmail", context); mimeMessageHelper.setText(process,true);
javaMailSender.send(mimeMessage);
} }
|
不能用 static
修饰方法!!!
调用执行
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
| package com.tothefor.controller;
import com.tothefor.utils.emailSend; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;
@RestController public class emailController {
@Autowired private emailSend es;
@GetMapping("/emailSendTo") public void sends() throws Exception{ es.sendEmail("250","2629828909@qq.com"); System.out.println("okok"); } }
|
然后再访问 /emailSendTo
即可发送邮件。