【笔记】SpringBoot 发邮件

前言

SpringBoot项目实现发邮件,案例通过QQ邮箱实现

准备工作

  • 导入依赖

Spring Web
Java Mail Sender

源代码

  • 修改application.properties项目配置文件
1
2
3
4
5
6
7
8
9
10
11
server.port=80

spring.mail.host=smtp.qq.com
#改成自己的邮箱
spring.mail.username=[email protected]
#邮箱的密码(QQ邮箱需要使用授权码)
spring.mail.password=000000
spring.mail.default-encoding=utf-8
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
  • 创建Controller做测试
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
package com.cy.mail.pj;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.PrintWriter;
import java.io.StringWriter;

@RestController
public class MailController {

//加上autowired之后,框架创建一个JavaMailSender,赋值给javaMailSender
@Autowired
JavaMailSender javaMailSender;

public void sendMail(String subject, String text) {
//创建一封邮件
SimpleMailMessage mailMessage = new SimpleMailMessage();
//设置收件人
mailMessage.setTo("[email protected]");
//设置发件人
mailMessage.setFrom("[email protected]");
//设置标题
mailMessage.setSubject(subject);
//设置正文
mailMessage.setText(text);
//发送邮件
javaMailSender.send(mailMessage);
}

// 成功案例
@RequestMapping("/send1")
public String send1() {
sendMail("邮件标题", "邮件正文");
return "发送成功";
}

// 失败案例
@RequestMapping("/send2")
public String send2() {
try {
int n = 10 / 0;
} catch (Exception e) {
//异常信息打印到服务器中,程序员看不到
//e.printStackTrace();
//把异常信息转成字符串
StringWriter stingWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stingWriter);
e.printStackTrace(printWriter);
String execeptionInfo = stingWriter.toString();
//return execeptionInfo;
this.sendMail("异常信息", execeptionInfo);
return "发送失败";
}
return "发送成功";
}

}

完成

  • 访问http://localhost/send1测试发送成功效果
  • 访问http://localhost/send2测试发送失败效果