springboot实现邮箱验证码功能

网友投稿 289 2022-12-14


springboot实现邮箱验证码功能

本文实例为大家分享了springboot实现邮箱验证码功能的具体代码,供大家参考,具体内容如下

我这边使用的QQ邮箱

1、首先创建maven项目,配置pom文件

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.example

springbootdemo

0.0.1-SNAPSHOT

jar

springbootdemo

Demo project for Spring Boot

org.springframework.boot

spring-boot-starter-parent

2.0.4.RELEASE

UTF-8

UTF-8

org.springframework.boot

spring-boot-starter-web

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.2

org.springframework.boot

spring-boot-starter-mail

commons-io

commons-io

2.4

mysql

mysql-connector-java

runtime

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-devtools

true

com.github.pagehelper

pagehelper

4.1.6

org.springframework.boot

spring-boot-maven-plugin

src/main/java

**/*.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.example

springbootdemo

0.0.1-SNAPSHOT

jar

springbootdemo

Demo project for Spring Boot

org.springframework.boot

spring-boot-starter-parent

2.0.4.RELEASE

UTF-8

UTF-8

org.springframework.boot

spring-boot-starter-web

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.2

org.springframework.boot

spring-boot-starter-mail

commons-io

commons-io

2.4

mysql

mysql-connector-java

runtime

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-devtools

true

com.github.pagehelper

pagehelper

4.1.6

org.springframework.boot

spring-boot-maven-plugin

src/main/java

**/*.xml

2、配置springboot,我这里使用的是properties方式

#配置Mybatis别名和扫描包

mybatis.type-aliases-package=com.demo.bean

mybatis.mapper-locations=classpath:mapper/*.xml

#数据库相关

spring.datasource.url=jdbc:mysql://localhost:3306/ssm?useSSL=false

spring.datasource.username=root

spring.datasource.password=123456

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#配置日志

logging.level.root=info

logging.level.com.demo.mapper=debug

#配置视图前缀和后缀

spring.mvc.view.prefix=/

spring.mvc.view.suffix=.html

#邮件发送配置

spring.mail.default-encoding=UTF-8

spring.mail.host=smtp.qq.com

spring.mail.username=你的邮箱

spring.mail.password=邮箱授权码

spring.mail.properties.mail.smtp.auth=true

spring.mail.properties.mail.smtp.starttls.enable=true

spring.mail.properties.mail.smtp.starttls.required=true

#thymeleaf配置

spring.thymeleaf.mode=HTML5

spring.thymeleaf.encoding=UTF-8

spring.thymeleaf.servlet.content-type=text/html

spring.thymeleaf.cache=false

邮箱授权码可以按以下方法获取

打开QQ邮箱网页→设置→账户→POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务→开启POP3/SMTP服务,然后就能看到授权码了

3、编写mailService

${spring.mail.username}是在properties中配置的属性,这里有一个方法,第一个是发送普通邮件,第二个是发送带有附件的邮件

@Service("mailService")

public class MailService {

@Value("${spring.mail.username}")

private String from;

@Autowired

private JavaMailSender mailSender;

Logger logger = LoggerFactory.getLogger(this.getClass());

public void sendSimpleMail(String to,String title,String content){

SimpleMailMessage message = new SimpleMailMessage();

message.setFrom(from);

message.setTo(to);

message.setSubject(title);

message.setText(content);

mailSender.send(message);

logger.info("邮件发送成功")http://;

}

public void sendAttachmentsMail(String to, String title, String cotent, List fileList){

MimeMessage message = mailSender.createMimeMessage();

try {

MimeMessageHelper helper = new MimeMessageHelper(message,true);

helper.setFrom(from);

helper.setTo(to);

helper.setSubject(title);

helper.setText(cotent);

String fileName = null;

for (File file:fileList) {

fileName = MimeUtility.encodeText(file.getName(), "GB2312", "B");

helper.addAttachment(fileName, file);

}

} catch (Exception e) {

e.printStackTrace();

}

mailSender.send(message);

logger.info("邮件发送成功");

}

}

4、编写controller

@Controller

public class MailController {

@Autowired

private MailService mailService;

@RequestMapping("getCheckCode")

@ResponseBody

public String getCheckCode(String email){

String checkCode = String.valueOf(new Random().nextInt(899999) + 100000);

String message = "您的注册验证码为:"+checkCode;

try {

mailService.sendSimpleMail(email, "注册验证码", message);

}catch (Exception e){

return "";

}

return checkCode;

}

}

5、编写页面

6、测试

邮件发送

发送成功

收到邮件

60s禁止重发


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Springboot实现邮件发送功能
下一篇:Java判断用户名和密码是否符合要求过程详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~