# SpringBoot发送邮件 ### 导入依赖 ```xml org.springframework.boot spring-boot-starter-mail ``` ### 发送普通邮件 编写`EmailController`,注入`JavaMailSender` ```java @RestController @RequestMapping("/email") public class EmailController { @Resource private JavaMailSender jms; @Value("${spring.mail.username}") private String from; @GetMapping("sendSimpleEmail") public String sendSimpleEmail() { try { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); // 收件方 message.setTo("xxx@qq.com"); // 标题 message.setSubject("一封简单的邮件"); // 内容 message.setText("使用Spring Boot发送简单邮件。"); jms.send(message); return "发送成功"; } catch (Exception e) { e.printStackTrace(); return e.getMessage(); } } } ``` 邮箱配置: 以163邮箱为例 ```yml spring: mail: host: smtp.163.com username: 邮箱名 password: 邮箱授权码 properties: mail: smtp: auth: true starttls: enable: true required: true ```
{{}}
{{}}
### 发送Html邮件 将`SimpleMailMessage`替换为`MimeMessage`: ```java @GetMapping("sendHtmlEmail") public String sendHtmlEmail() { MimeMessage message = null; try { message = jms.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); // 收件方 helper.setTo("xxx@qq.com"); // 标题 helper.setSubject("一封HTML格式的邮件"); // 带HTML格式的内容 StringBuffer sb = new StringBuffer("

使用Spring Boot发送HTML格式邮件。

"); helper.setText(sb.toString(), true); jms.send(message); return "发送成功"; } catch (Exception e) { e.printStackTrace(); return e.getMessage(); } } ```
{{}}
### 带附件的邮件 发送带附件的邮件和普通邮件相比,其实就只是多了个传入附件的过程。不过使用的仍是`MimeMessage`: ```java @GetMapping("sendAttachmentsMail") public String sendAttachmentsMail() { MimeMessage message = null; try { message = jms.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); // 收件方 helper.setTo("xxx@qq.com"); // 标题 helper.setSubject("一封带附件的邮件"); // 内容 helper.setText("详情参见附件内容!"); // 传入附件 FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/test.txt")); // 附件的名字 helper.addAttachment("test.txt", file); jms.send(message); return "发送成功"; } catch (Exception e) { e.printStackTrace(); return e.getMessage(); } } ```
{{}}
### 发送带静态资源的邮件 ```java @GetMapping("sendInlineMail") public String sendInlineMail() { MimeMessage message = null; try { message = jms.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); // 接收地址 helper.setTo("xxx@qq.com"); // 标题 helper.setSubject("一封带静态资源的邮件"); // 内容 helper.setText("

图片:

", true); // 传入附件 FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/1679230591682.jpg")); helper.addInline("myImg", file); jms.send(message); return "发送成功"; } catch (Exception e) { e.printStackTrace(); return e.getMessage(); } ```
{{}}
### 发送验证码 引入模板引擎thymeleaf的依赖 ```xml org.springframework.boot spring-boot-starter-thymeleaf ``` 在`template`目录下创建一个`emailTemplate.html`模板: ```html 验证码模板 您好,本次验证的验证码为: ``` 发送模板邮件,本质上还是发送HTML邮件,不过加上给模板引擎传值的步骤 ```java @Autowired private TemplateEngine templateEngine; @PostMapping("sendTemplateEmail") public String sendTemplateEmail() { MimeMessage message = null; try { message = jms.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); // 设置发件人 helper.setFrom(from); // 收件人 helper.setTo("xxx@qq.com"); // 标题 helper.setSubject("发送验证码"); // 处理邮件模板 Context context = new Context(); // 使用工具类生成验证码,将code传递给模板 context.setVariable("code", VerCodeGenerateUtil.generateVerCode()); // 对应上面的emailTemplate.html String template = templateEngine.process("emailTemplate", context); helper.setText(template, true); jms.send(message); return "发送成功"; } catch (Exception e) { e.printStackTrace(); return e.getMessage(); } } ``` 验证码生成工具类: ```java /** * 验证码生成 */ public class VerCodeGenerateUtil { //验证码包含的字段,可自己设置 private static final String SYMBOLS = "0123456789" + "abcdefghijkomnopqrstuvwxyz" + "ABCDEFGHIGKLMNOPQRSTUVWXYZ"; // 验证码位数 public static final Integer CODE_NUMBER = 4; private static final Random RANDOM = new SecureRandom(); public static String generateVerCode() { char[] numbers = new char[CODE_NUMBER]; for (int i = 0; i < numbers.length; i++) { numbers[i] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length())); } return new String(numbers); } } ```
{{}}
{{< admonition type="quote" title="参考文档">}} [使用Spring Boot发送邮件](https://mrbird.cc/Spring-Boot-Email.html) {{< /admonition>}}