赞
踩
这次想做一个以电子邮件的方式来发送验证码给管理员,以便修改管理员密码。由于自己将本地环境部署的电子邮件案例测试通过后,便天真地以为能够上线,正常跑通了,但是由于是第一次接触这个电子邮箱,对其也是一知半解的,果然,踩坑了。
在项目上线后,由于阿里云和腾讯云默认关闭25端口(要开启听说要申请),所以没办法,只能修改电子邮件的端口为SSL方式的另外两个端口(465或994),最终在一系列配置之后,总算是成功发送电子邮件了
<!-- 电子邮件发送工具--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
- --- #短信配置
- spring:
- mail:
- # 配置 SMTP 服务器地址
- host: smtp.163.com
- # 发送者邮箱
- username: ***(自己的邮箱)***
- # 配置密码注意是刚刚申请到的授权码
- password: ***(自己的授权码)***
- # 端口号 25或465/994(使用ssl时)
- port: 465
- # 默认的邮件编码为UTF-8
- default-encoding: UTF-8
- # 邮件协议
- protocol: smtp
- # 配置SSL 加密工厂
- properties:
- mail:
- smtp:
- auth: true
- required:
- enable: true
- # 设为true时 端口号设为 465 设为false时 端口号设为25
- ssl:
- enable: true
- socketFactoryClass: javax.net.ssl.SSLSocketFactory
- #表示开启 DEBUG 模式邮件发送过程的日志会在控制台打印出来
- debug: true
- @Autowired
- private MailService mailService;
-
- @PostMapping("/sendMail/{email}")
- public Result<String> sendMail(@PathVariable("email") String email) {
- // 校验邮箱格式是否正确
- boolean isEmail = Validator.isEmail(email);
- if (!isEmail) {
- throw new BusinessNewException(ErrorConstant.EMAIL_PATTERN_NOT_MATCH);
- }
- try {
- mailService.sendMail(email);
- return Result.ok("邮件发送成功");
- } catch (MessagingException e) {
- e.printStackTrace();
- return Result.failed("邮件发送失败");
- }
- }
@Autowired(required = false) private JavaMailSender mailSender; @Resource private StringRedisTemplate stringRedisTemplate; /** * 发送信息邮件 * @param to 收件人 * @throws MessagingException 异常 */ public void sendMail(String to) throws MessagingException { // 使用hutool生成验证码 // 创建一个LineCaptcha实例,设置长度(默认为4位) String captchaCode = RandomUtil.randomNumbers(6); if (Boolean.TRUE.equals(stringRedisTemplate.hasKey(RedisConstant.ADMIN_FORGOT_PASSWORD_KEY + to))) { throw new BusinessNewException("验证码已发送,请勿在一分钟内重复发送"); }else{ stringRedisTemplate.opsForValue().set(RedisConstant.ADMIN_FORGOT_PASSWORD_KEY + to, captchaCode, 60, TimeUnit.SECONDS); } MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); //邮箱发送者 helper.setFrom(userName); //收件人,可以为多个收件人,收件人之间用逗号隔开 helper.setTo(to); // 邮箱标题 helper.setSubject(EmailConstant.EMAIL_TITLE_FORGOT_PASSWORD); // 邮箱内容 String emailContent = EmailConstant.EMAIL_PREFIX_CONTENT_FORGOT_PASSWORD + "您本次的验证码为:" + captchaCode + ",请在60秒内填写,否则验证码失效\n" + EmailConstant.EMAIL_SUFFIX_FORGOT_PASSWORD; helper.setText(emailContent, true); mailSender.send(message); }这里使用了RedisStringTemplate限制用户1分钟后才能重复发送。
EmailConstant:
public interface EmailConstant { String EMAIL_TITLE_FORGOT_PASSWORD = "这里是电子邮件标题"; String EMAIL_PREFIX_CONTENT_FORGOT_PASSWORD ="(电子邮件内容前缀)尊敬的管理员,\n" + "您收到这封邮件是因为您在***网站要通过邮件的方式找回密码,\n"; String EMAIL_SUFFIX_FORGOT_PASSWORD = "(电子邮件内容后缀)如果您的邮箱没有收到邮件,请联系管理员,\n" ; }RedisConstant.ADMIN_FORGOT_PASSWORD_KEY
String ADMIN_FORGOT_PASSWORD_KEY = "wx:admin:forget:";
在这过程中,我卡得最久的竟然是yum的配置,将“ssl: ” 那里的"enable" 写成了“enabled” (主要是阿里的通义灵码给我的提示,我直接tab了),导致一直没成功。看来还得多多学习,才能少出BUG
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。