springboot中怎么实现一个邮箱验证码功能
创始人
2024-12-25 05:42:59
0次

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

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 1.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.beanmybatis.mapper-locations=classpath:mapper/*.xml #数据库相关spring.datasource.url=jdbc:mysql://localhost:3306/ssm?useSSL=falsespring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.jdbc.Driver #配置日志logging.level.root=infologging.level.com.demo.mapper=debug #配置视图前缀和后缀spring.mvc.view.prefix=/spring.mvc.view.suffix=.html #邮件发送配置spring.mail.default-encoding=UTF-8spring.mail.host=smtp.qq.comspring.mail.username=你的邮箱spring.mail.password=邮箱授权码spring.mail.properties.mail.smtp.auth=truespring.mail.properties.mail.smtp.starttls.enable=truespring.mail.properties.mail.smtp.starttls.required=true #thymeleaf配置spring.thymeleaf.mode=HTML5spring.thymeleaf.encoding=UTF-8spring.thymeleaf.servlet.content-type=text/htmlspring.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("邮件发送成功"); }  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

@Controllerpublic 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、编写页面

注册

 

 

请输入注册信息

 
   

       

         

   

   

       

         

   

   

       

               

   

   

   

         

   

 
 

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...