springboot 使用QQ邮箱发送邮件的操作方法
作者:Mer_de_sauce 时间:2022-03-03 14:36:22
一、QQ邮箱打开POP3/SMTP服务
上面的服务开启后,会得到一串授权密码在springboot配置中需要用到
二、springboot配置
IDE目录
1.在pom.xml添加spring-boot-starter-mail起步依赖
<!-- springboot开发mail项目的起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2.在application.properties中配置mail属性
#静态资源映射,localhost:8080/ == /resources
spring.resources.static-locations=classpath:META-INF/resources/,classpath:static/,classpath:templates/
#https://blog.csdn.net/jawhiow/article/details/82625842
#如果原先访问首页的地址是:http://localhost:8888/index.html 那么在你配置这个配置后,http://localhost:8888/default/index.html
spring.mvc.static-path-pattern=/*
# 设置邮箱主机
spring.mail.host=smtp.qq.com
# 设置用户名
spring.mail.username=xxxxxx@qq.com
# 设置密码,该处的密码是QQ邮箱开启SMTP的授权码而非QQ密码
spring.mail.password=xxxxxx
# 设置是否需要认证,如果为true,那么用户名和密码就必须的,
# 如果设置false,可以不设置用户名和密码,当然也得看你的对接的平台是否支持无密码进行访问的。
spring.mail.properties.mail.smtp.auth=true
# STARTTLS[1] 是对纯文本通信协议的扩展。它提供一种方式将纯文本连接升级为加密连接(TLS或SSL),而不是另外使用一个端口作加密通信。
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.port=465
3.编写controller文件
@Controller
public class EmailController {
@Autowired
private JavaMailSender javaMailSender;
@RequestMapping("/")
public void index(HttpServletRequest request, HttpServletResponse response) throws IOException {
System.out.println("index.html");
response.sendRedirect("/index.html");
}
@ResponseBody
@RequestMapping("/emailSend")
public String emailSend(@RequestParam(value = "email") String eamil){
System.out.println(eamil);
String content = "1234";
try{
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("xxxxxx@qq.com");
message.setTo(eamil);
message.setSubject("主题:主题内容");
message.setText(content);
//发送邮件
javaMailSender.send(message);
System.out.println(eamil+"发送成功");
}catch (Exception e){
return "fail";
}
return "success";
}
}
4.编写网页页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>邮件</title>
</head>
<body>
<form action="/emailSend">
邮箱:<input type="text" name="email" value="xxxxxx@qq.com">
<input type="submit">
</form>
</body>
</html>
5.发送成功
来源:https://blog.csdn.net/qq_40833874/article/details/120676848
标签:springboot,QQ邮箱,发送邮件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java web中图片验证码功能的简单实现方法
2023-06-07 13:30:53
java递归实现拼装多个api的结果操作方法
2023-11-24 23:44:35
Java非侵入式API接口文档工具apigcc用法详解
2023-11-24 10:01:00
![](https://img.aspxhome.com/file/2023/1/59331_0s.png)
java前后端加密解密crypto-js的实现
2023-11-29 12:09:31
JPA多数据源分布式事务处理方案
2023-08-09 03:50:06
详解SpringBoot+SpringSecurity+jwt整合及初体验
2023-11-28 23:54:48
![](https://img.aspxhome.com/file/2023/3/60373_0s.png)
springcloud-gateway整合jwt+jcasbin实现权限控制的详细过程
2023-11-20 12:57:09
![](https://img.aspxhome.com/file/2023/8/59078_0s.png)
Java中反射动态 代理接口的详解及实例
2023-11-28 05:29:36
![](https://img.aspxhome.com/file/2023/6/60266_0s.png)
Java序列化和反序列化示例介绍
2023-11-25 04:24:26
Android之使用Bundle进行IPC详解
2023-09-27 22:44:56
Flutter 如何正确显示SnackBar
2023-06-23 13:00:40
老生常谈Java字符串进阶(必看篇)
2023-11-12 21:19:35
Springmvc Controller接口代码示例
2023-11-28 10:13:25
TransmittableThreadLocal解决线程间上下文传递烦恼
2023-11-09 17:09:35
Java实现接口的枚举类示例
2023-06-18 01:22:04
Java构造代码块,静态代码块原理与用法实例分析
2023-11-03 09:03:45
![](https://img.aspxhome.com/file/2023/7/58707_0s.png)
Java多线程之Park和Unpark原理
2023-03-29 15:46:11
![](https://img.aspxhome.com/file/2023/4/61564_0s.png)
jenkins+Maven从SVN上构建项目的方法
2022-07-09 04:42:37
![](https://img.aspxhome.com/file/2023/2/61582_0s.png)
Java实例讲解动态代理
2023-03-21 22:56:45
基于Java文件输入输出流实现文件上传下载功能
2023-08-10 11:50:32
![](https://img.aspxhome.com/file/2023/0/58170_0s.png)