1.在yml配置文件添加相关配置(mail是在spring配置下的)
host:是你选择发送邮箱服务器的地址
例子:发送qq邮件服务器:smtp.qq.com
port:发送邮箱服务器的端口
例子:发送qq邮件服务器端口号:465或587
username:邮箱账号
password:邮箱密码
2.EmailUtil
上代码!!!!!!!!!
//注入MailSender private static JavaMailSenderImpl mailSender; /** * 初始化发送服务器 */ private void initSmtpMail(SendTextMailDto sendMailDto) { mailSender = new JavaMailSenderImpl(); mailSender.setHost(sendMailDto.getHost()); mailSender.setPort(sendMailDto.getPort()); mailSender.setUsername(sendMailDto.getUsername()); mailSender.setPassword(sendMailDto.getPassword()); mailSender.setProtocol(sendMailDto.getProtocol()); mailSender.setDefaultEncoding(sendMailDto.getDefaultEncoding()); } /** * 发送文本邮件 * * @param sendMailDto 邮件对象(收件人,邮件主题,邮件内容) */ public void sendSimpleTextMailActual(SendTextMailDto sendMailDto) { //初始化发送服务器 initSmtpMail(sendMailDto); //检验参数:邮件主题、收件人、邮件内容必须不为空才能够保证基本的逻辑执行 if (sendMailDto == null) { throw new BizException("发送邮件不可以为空"); } if (sendMailDto.getSubject() == null || sendMailDto.getMailTo() == null || sendMailDto.getContent() == null) { log.error("邮件-> {} 无法继续执行,因为缺少基本的参数:邮件主题、收件人、邮件内容", sendMailDto.getSubject()); throw new BizException("邮件无法继续发送,因为缺少必要的参数!"); } String[] toWho = sendMailDto.getMailTo().split(","); if (toWho.length == 0) { throw new BizException("邮件无法继续发送,因为缺少必要的参数!"); } //创建一个简单邮件信息对象 SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); //设置邮件的基本信息 handleBasicInfo(simpleMailMessage, sendMailDto.getSubject(), sendMailDto.getContent(), toWho,sendMailDto); //发送邮件 mailSender.send(simpleMailMessage); log.info("发送邮件成功: 主题->{}", sendMailDto.getSubject(), toWho); } /** * 用于填充简单文本邮件的基本信息 * * @param simpleMailMessage:文本邮件信息对象 * @param subject:邮件主题 * @param content:邮件内容 * @param toWho:收件人 */ public void handleBasicInfo(SimpleMailMessage simpleMailMessage, String subject, String content, String[] toWho,SendTextMailDto sendMailDto) { //设置发件人 simpleMailMessage.setFrom(sendMailDto.getUsername()); //设置邮件的主题 simpleMailMessage.setSubject(subject); //设置邮件的内容 simpleMailMessage.setText(content); //设置邮件的收件人 simpleMailMessage.setTo(toWho); }
3.Controller
@Value("${spring.mail.host}") private String HOST; @Value("${spring.mail.port}") private Integer PORT ; @Value("${spring.mail.username}") private String USERNAME ; @Value("${spring.mail.password}") private String PASSWORD ; @Value("${spring.mail.protocol}") private String protocol; @Value("${spring.mail.default-encoding}") private String defaultEncoding; @ApiOperation("获取邮箱注册验证码") @RequestMapping(value = "/a",method = RequestMethod.GET) @ResponseBody public CommonResult a(@RequestParam String email) { SendTextMailDto sendTextMailDto = new SendTextMailDto(); sendTextMailDto.setHost(HOST); sendTextMailDto.setPassword(PASSWORD); sendTextMailDto.setPort(PORT); sendTextMailDto.setProtocol(protocol); sendTextMailDto.setUsername(USERNAME); sendTextMailDto.setDefaultEncoding(defaultEncoding); sendTextMailDto.setMailTo(email); return memberService.emailCode(sendTextMailDto); }
4.在yml配置文件里面添加redis配置
5.service
/** * 获取邮箱注册验证码 * @param sendTextMailDto 发送邮箱需要的参数 * @return */ CommonResult emailCode(SendTextMailDto sendTextMailDto);
6.serviceImpl
@Value("${redis.key.prefix.authCode}") private String REDIS_KEY_PREFIX_AUTH_CODE; @Value("${redis.key.expire.authCode}") private Long AUTH_CODE_EXPIRE_SECONDS; public CommonResult emailRegister(EmailRegisterDto emailRegisterDto){ //获取存储到redis的验证码 String str = stringRedisTemplate.opsForValue().get(REDIS_KEY_PREFIX_AUTH_CODE+emailRegisterDto.getEmail()); //对比验证码是否正确 if(StringUtils.isNotBlank(stringRedisTemplate.opsForValue().get(REDIS_KEY_PREFIX_AUTH_CODE+emailRegisterDto.getEmail()))&&emailRegisterDto.getCode().equals(str.substring(str.indexOf(":")+1))){ //没有该用户进行添加操作 UmsMember umsMember = new UmsMember(); BeanUtil.copyProperties(emailRegisterDto, umsMember); //获取默认会员等级并设置 UmsMemberLevelExample levelExample = new UmsMemberLevelExample(); levelExample.createCriteria().andDefaultStatusEqualTo(1); List<UmsMemberLevel> memberLevelList = memberLevelMapper.selectByExample(levelExample); if (!CollectionUtils.isEmpty(memberLevelList)) { umsMember.setMemberLevelId(memberLevelList.get(0).getId()); } //将密码进行加密操作 String encodePassword = passwordEncoder.encode(emailRegisterDto.getPassword()); umsMember.setPassword(encodePassword); memberMapper.insertSelective(umsMember); return CommonResult.success("注册成功"); }else { throw new BizException("验证码已失效"); } }
ending!!!!!!!!!!!ok!!!!!!!