欢迎关注我的博客StarTower小站
一、邮件协议
SMTP
SMTP是一种提供可靠且有效的电子邮件传输的协议。SMTP是建立在FTP文件传输服务上的一种邮件服务,主要用于系统之间的邮件信息传递,并提供有关来信的通知。SMTP独立于特定的传输子系统,且只需要可靠有序的数据流信道支持,SMTP的重要特性之一是其能跨越网络传输邮件,即“SMTP邮件中继”。使用SMTP,可实现相同网络处理进程之间的邮件传输,也可通过中继器或网关实现某处理进程与其他网络之间的邮件传输。
pop3
POP3(Post Office Protocol version3),即“邮局协议版本3”。是TCP/IP协议族中的一员,由RFC1939 定义。本协议主要用于支持使用客户端远程管理在服务器上的电子邮件
二、准备过程
我们以QQ邮箱为发送邮件用户为例,登录自己的QQ邮箱进入设置>账户>POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务
按照提示开启POP3/STMP服务,开启之后会给一个授权码,授权码可以申请多个,申请到的授权码我们暂时保存等一下会用到
三、开发过程
- 创建新的SpringBoot工程,在pom文件中导入Maven依赖
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
其中version版本必须和自己的SpringBoot版本一致,我们可以直接删除这条依赖的版本,前提是parent内必须有springBoot的版本如下,这样就会自己匹配相应的版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
- 修改配置文件application.properties
# 设置邮箱主机
spring.mail.host=smtp.qq.com
# 设置用户名,自己的QQ邮箱,也就是发送邮件的发送者
spring.mail.username=自己的@qq.com
# 设置密码,该处的密码是QQ邮箱开启SMTP的授权码而非QQ密码
spring.mail.password=准备过程中获取到的授权码
- 编写发送邮件的Service层SendMailService.java
package com.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
@Service
public class SendMailService {
@Value("${spring.mail.username}")
private String sendUsername;
@Autowired
private JavaMailSender javaMailSender; //发送邮件封装类
/**
* 无附件邮件发送
* @param to 收信人
* @param subject 邮件主题
* @param content 邮件内容
* @return
*/
public void sendMail(String to,String subject,String content){
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);//收信人
message.setSubject(subject);//主题
message.setText(content);//内容
message.setFrom(sendUsername);//发信人
javaMailSender.send(message);
}
}
- 紧接着编写发送邮件的Action类
package com.action;
import com.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SendMailAction {
@Autowired
private SendMailService sendMailService;
@RequestMapping("/api/sendMail")
public String sendMail(String toMail){
System.out.println(toMail);
sendMailService.sendMail(toMail,"邮件注册验证码","123456");
return "success";
}
}
- 启动类中我们要加入包的扫描,否则启动后无法找到action包与service包里的文件
package com.springboot.springboot5;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan({"com.action","com.service"})
public class Springboot5Application {
public static void main(String[] args) {
SpringApplication.run(Springboot5Application.class, args);
}
}
- 功能类完成后我们需要自己写一个静态页面进行邮件发送的功能测试
这里的今天页面会用到jquery包
目录如下:
sendMail.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>邮件发送</title>
<script src="js/jquery-3.3.1.min.js"></script>
</head>
<body>
邮件:<input type="text" id="mail"> <button onclick="sendMail()">发送</button> <br>
验证码:<input type="text" id="code"> <br>
<button>验证</button>
</body>
</html>
<script>
function sendMail() {
var m = $("#mail").val();
$.get('/api/sendMail',{toMail:m},function (res) {
alert('发送成功');
});
}
</script>
- 打开浏览器输入页面网址进行测试
可以看到浏览器显示发送成功
同时邮箱也会收到验证码的邮件
发送邮件成功