小伙伴们,你们好呀!我是老寇!(本文用于记录代码,如果能帮助你,不胜荣幸)
一、环境准备
- SpringBoot 2.0.0
- QQ邮箱授权码
二、仓库地址
gitee : KCloud: 老寇云,我们一起进阶(已上传)
三、开发环节
3.1.导入依赖(2021/11/13)
4.0.0 laokou-third-party-demo laokou-third-party-demo1.0-SNAPSHOT io.laokou laokou-third-party-email-demo0.0.1-SNAPSHOT laokou-third-party-email-demo 第三方-邮件 1.8 org.springframework.boot spring-boot-starter-mailorg.springframework.boot spring-boot-starter-web
3.2.配置文件(2021/11/13)
server: port: 12032 servlet: context-path: /laokou-demo spring: application: name: laokou-third-party-email-demo mail: default-encoding: UTF-8 host: smtp.qq.com username: 你的邮箱地址 password: 授权码 properties: mail: smtp: auth: true starttls: enable: true required: true
3.3.核心代码(2021/11/13)
package io.laokou.email.utils; 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.Component; import java.util.Date; @Component public class EmailUtil { @Autowired private JavaMailSender sender; @Value("${spring.mail.username}") private String fromUser; public Integer send() { try{ // 构建一个邮件对象 SimpleMailMessage message = new SimpleMailMessage(); //发送人 message.setFrom(fromUser); //接收人 message.setTo(你发给哪个人的邮箱); //邮件主题 message.setSubject("验证码"); //邮件时间 message.setSentDate(new Date()); //邮件内容 message.setText(String.format("验证码:%s,您正在登录,若非本人 *** 作,请勿泄露。", 233232)); sender.send(message); return 1; } catch (Exception e) { e.printStackTrace(); System.out.println("error:"+e.getMessage()); return 0; } } }
四、运行截图
初始效果截图(2021/11/13)
五、优化过程
5.1.优化(一) 2021/11/13
将写死的验证码变成随机的六位数
1).在pom.xml加入依赖
org.apache.commons commons-lang3
2)修改核心代码
message.setText(String.format("验证码:%s,您正在登录,若非本人 *** 作,请勿泄露。", RandomStringUtils.randomNumeric(6)));
5.2 优化(二)2021/11/14
加入freemarker + mysql 生成对应的模板内容
1).数据库设计
DROP TABLE IF EXISTS `boot_freemarker_demo_template`; CREATE TABLE `boot_freemarker_demo_template` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id', `template_code` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '模板编码 login_email_captcha登录邮件验证码', `template_content` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL COMMENT '模板内容', `template_sign` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '模板签名', `template_subject` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '模板主题', `template_id` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '模板编号', `template_type` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '模板类型 email邮件 sms短信', PRIMARY KEY (`id`, `template_id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
作者开发中...
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)