爱旅行邮箱注册功能

1.开启邮箱POP3三方登录协议

登录QQ邮箱官网(https://mail.qq.com/)
在这里插入图片描述
或者 网易邮箱(https://mail.163.com/)
在这里插入图片描述
![在这里插入图片描述](https://img-blog.csdnimg.cn/e18b0e842441407485491fd1f745c96b.jpeg

2.引入依赖

pom.xml

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
</dependency>

3.编写邮箱配置文件

applicationContext-mail.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd ">
   <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
      <property name="host" value="smtp.邮箱方式.com"></property>
      <property name="port" value="465"></property>
      <property name="username" value="邮箱地址"></property>
      <property name="password" value="第一步生成的授权码"></property>
      <!--<property name="protocol" value="smtp"></property>-->
      <property name="defaultEncoding" value="UTF-8"></property>
      <property name="javaMailProperties">
         <props>
            <!-- 设置SMTP服务器需要用户验证 -->
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
            <prop key="mail.smtp.socketFactory.port">465</prop>
         </props>
      </property>
   </bean>

   <bean id="activationMailMessage"  class="org.springframework.mail.SimpleMailMessage" scope="prototype">
      <property name="from" value="邮箱地址"></property>
      <property name="subject" value="【i旅行】请激活您的账户"></property>
      <!--<property name="text" value=""></property>-->
   </bean>
</beans>

4.编写邮箱接口实现类

public interface MailService {
    /**
     * 发送邮件接口
     * @param mailTo
     * @param activationCode
     */
    public void sendActivationMail(String mailTo,String activationCode);
}
import org.apache.tools.ant.taskdefs.Java;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class MailServiceImpl implements MailService {

    @Resource
    private MailSender mailSender;
    @Resource
    private SimpleMailMessage mailMessage;

    public void sendActivationMail(String mailTo, String activationCode) {
        mailMessage.setTo(mailTo);
        mailMessage.setText("您的注册邮箱:" + mailTo + "激活码:" + activationCode);
        mailSender.send(mailMessage);
    }
}

5.根据业务需求,实现邮箱注册

service 类

/**
 * 使用邮箱注册用户
 * @param user
 * @throws Exception     *
 * */
void doregister(ItripUser user) throws Exception;
/**
 * 根据账号,用户id查询用户信息
 * @param user
 * @param code
 * @return
 */
Boolean activate(String user, String code) throws Exception;

service实现类

 /**
 * 使用邮箱注册用户
 * @param user
 * @throws Exception     *
 * */
@Override
public void doregister(ItripUser user) throws Exception {
    //生成激活码
    String activationCode = MD5.getMd5(new Date().toLocaleString(),32);
    //发送邮件
    mailService.sendActivationMail(user.getUserCode(),activationCode);
    //保存激活码到Redis
    redisAPI.set("active:"+user.getUserCode(),10*60,activationCode);
    //保存用户信息
    itripUserMapper.insertItripUser(user);
}
 /**
 * 根据账号,用户id查询用户信息
 * @param user
 * @param code
 * @return
 */
 @Override
    public Boolean activate(String user, String code) throws Exception {
//比对激活码
        if(redisAPI.exist("active:"+user)){
            if(redisAPI.get("active:"+user).equals(code)){
                ItripUser itripUser = this.selectUserByUserCode(user);
                if(EmptyUtils.isNotEmpty(user)){
                    //更新用户
                    itripUser.setActivated(1); //平台ID(根据不同登录用户,进行相应存入:自注册用户主键ID、微信ID、QQID、微博ID)
                    itripUser.setUserType(0);//用户类型(标识:0 自注册用户 1 微信登录 2 QQ登录 3 微博登录)
                    itripUserMapper.updateItripUser(itripUser);
                    return true;
                }
            }
        }
        return false;
    }

controller类

/**
 * 使用邮箱注册 
 * @param userVO
 * @return
 */
@RequestMapping(value="/doregister",method=RequestMethod.POST,produces = "application/json")
public @ResponseBody Dto doRegister(@RequestBody ItripUserVO userVO) {
   if(!validEmail(userVO.getUserCode()))
      return  DtoUtil.returnFail("请使用正确的邮箱地址注册",ErrorCode.AUTH_ILLEGAL_USERCODE);
   try {
      ItripUser user=new ItripUser();
      user.setUserCode(userVO.getUserCode());
      user.setUserPassword(userVO.getUserPassword());
      user.setUserType(0);
      user.setUserName(userVO.getUserName());
      if (null == userService.selectUserByUserCode(user.getUserCode())) {
         user.setUserPassword(MD5.getMd5(user.getUserPassword(), 32));
         userService.doregister(user);
         return DtoUtil.returnSuccess("注册成功");
      }else
      {
         return DtoUtil.returnFail("用户已存在,注册失败", ErrorCode.AUTH_USER_ALREADY_EXISTS);
      }
   } catch (Exception e) {
      e.printStackTrace();
      return DtoUtil.returnFail(e.getMessage(), ErrorCode.AUTH_UNKNOWN);
   }
}
/**
 * 根据账号,用户id查询用户信息 激活账号
 * @param user
 * @param code
 * @return
 */
@RequestMapping(value="/activate",method=RequestMethod.PUT,produces= "application/json")
public @ResponseBody Dto activate(@RequestParam String user,@RequestParam String code){          
   try {
      if(userService.activate(user, code))
      {
         return DtoUtil.returnSuccess("激活成功");
      }else{
         return DtoUtil.returnSuccess("激活失败");
      }
   } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();         
      return DtoUtil.returnFail("激活失败", ErrorCode.AUTH_ACTIVATE_FAILED);
   }     
} 

/**        *
 * 合法E-mail地址:     
 * 1. 必须包含一个并且只有一个符号“@”    
 * 2. 第一个字符不得是“@”或者“.”
 * 3. 不允许出现“@.”或者.@   
 * 4. 结尾不得是字符“@”或者“.”    
 * 5. 允许“@”前的字符中出现“+” 
 * 6. 不允许“+”在最前面,或者“+@” 
 */
private boolean validEmail(String email){
   
   String regex="^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$"  ;       
   return Pattern.compile(regex).matcher(email).find();         
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

欢乐的阿博

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值