javaweb通过发送邮箱进行账号注册、激活

先设计数据库表

active代表是否激活,如果没有去邮箱点击链接,active为0,点了就更新active字段为1

需要的jar包依赖

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.5<ersion>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4<ersion>
</dependency>

前台代码

oup.οnclick=function(){
   var username=document.getElementById('username').value;
   var password=document.getElementById('password').value;
   var password1=document.getElementById('password1').value;
    var email=document.getElementById('email').value;
//先对输入的邮箱格式进行判断
          if(email == ''){
              alert("请输入您的邮箱");
              return;
          }else if(email != "") {
              var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
              isok= reg.test(email );
              if (!isok) {
                  alert("邮箱格式不正确,请重新输入!");
                  return false;
              }
          };
   $.ajax({
      url : "User/user.do",
      type : "post",
      data : {
         "username":username,
         "password":password,
         "password1":password1,
         "email":email
      },
      async : false,
      success : function(data) {
         if(data=='No'){
            alert("注册失败");
         }
         else {
            alert("注册成功");
            window.location.href="login(1).html";
         }
      }
   });

};

后台代码Usercontroller

@RequestMapping(value = "/user.do")  /
@ResponseBody
public String regist(HttpServletRequest request, User user, Model model, ModelAndView modelandview) {

    String password = request.getParameter("password");
    String password1 = request.getParameter("password1");
    String username = request.getParameter("username");
    String email = request.getParameter("email");
    String active = "0";

     User  user1 = userService.login(username, password);


        if (user1 != null || !password.equals(password1)) {//我写的账户是否存在的判断。 可以自行删除
            System.out.println("No");
            return "No";
        } else {
            try{
               

                sendEmail a=new sendEmail();
                a.sendHtmlEmail(email);
                userService.regist(username, password, email, active);//账号存入数据库,但是active字段 为0
            }catch (Exception e){

                System.out.println("用户注册:" + user.getUsername() + user.getPassword());

                return "Yes";
            }
        }

        //注册成功后跳转success.jsp页面
         return "";

}

sendEmail类(三种邮箱类型),我用的是qq邮箱来激活,也可以用163等邮箱,道理差不多

package controller;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;

import java.io.File;
import java.net.URL;

public class sendEmail {
    /*我们使用普通邮件来测试

    public static void main(String[] args) {
        sendHtmlEmail();//发送普通邮件
      //  sendHtmlEmail();//发送html5邮件
      //   sendAttachmentsEmail();//发送带附件邮件
    }
  */
    private static void sendSimpleEmail() {
        Email email=new SimpleEmail();
        email.setHostName("smtp.qq.com");
        email.setSmtpPort(465);
        email.setAuthenticator(new DefaultAuthenticator("XXXXX","XXXXX"));//发送人的邮箱和授权码
        email.setSSLOnConnect(true);

        try {
            email.setFrom("XXXXX");//发送人的邮箱
            email.setSubject("测试主题");//发送的主题
            email.setMsg("测试内容");//发送的内容
            email.addTo("XXXXXX");//接受人的邮箱
            email.send();
            System.out.println("发送simple邮件成功!!!");
        } catch (EmailException e) {
            e.printStackTrace();
        }
    }

    public static void sendHtmlEmail(String mail)  {
        HtmlEmail email = new HtmlEmail();
        email.setHostName("smtp.qq.com");
        email.setSmtpPort(465);
        email.setAuthenticator(new DefaultAuthenticator("xxxxxx@qq.com","xxxxxx"));//发送人的邮箱和授权码,
        email.setSSLOnConnect(true);
        try {
            email.setFrom("xxxxxx","hwj");
            email.setSubject("HTML5格式邮箱");
            String activeUrl="http://localhost:8080/xuenian/User/activemail.do?mail="+mail;
//发送一个激活链接来实现对数据库的active字段更新为1,mail就是你前台注册的mail。
            email.setHtmlMsg("点击链接激活账户"+activeUrl);
            email.setTextMsg("测试内容");
            email.addTo(mail, "hzk");
            email.send();
            System.out.println("发送HTML邮件成功!!");
        } catch (EmailException e) {
            e.printStackTrace();
        }
    }

    private static void sendAttachmentsEmail() {
        MultiPartEmail email = new MultiPartEmail();
        email.setHostName("smtp.qq.com");
        email.setSmtpPort(465);
        email.setAuthenticator(new DefaultAuthenticator("xxxx","xxxxx"));//发送人的邮箱和授权码
        email.setSSLOnConnect(true);
        try {
            EmailAttachment attachment = new EmailAttachment();
            attachment.setURL(new URL("https://www.xys1118.com/images/topbg.jpg"));
            attachment.setDisposition(EmailAttachment.ATTACHMENT);
            attachment.setDescription("xys.jpg");
            attachment.setName("xys.jpg");

            email.attach(attachment);
            email.attach(new File("C:/Users/admin/Desktop/测试/1/1.txt"));
            email.setFrom("xxxxx@qq.com","hqs");
            email.setSubject("HTML5带有附件邮箱");
            email.setMsg("测试附件邮件内容");
            email.addTo("xxxxx@qq.com", "hzk");
            email.send();
            System.out.println("发送HTML邮件成功!!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
} 

邮箱授权码获取(qq邮箱为例子)

在你的qq邮箱的设置的账户里。

开启前两个,会有一个授权码给你,就是你的授权码。通过你的邮箱来发出激活邮件给别人的邮箱。

activemail.do(激活链接的处理后台代码) 

//验证邮箱
@RequestMapping(value = "/activemail.do")
public ModelAndView activemail(User user,HttpServletRequest request, Model model) {

    String email = request.getParameter("mail");
    System.out.println(email);
    String active = "1";
    //1.根据激活码从数据库查出用户信息
    ModelAndView mv = new ModelAndView("redirect:/success.html");//自己写一个success页面代表激活成功
    ModelAndView mv1 = new ModelAndView("redirect:/fail.html");//自己写一个success页面代表激活失败

    user = userService.findemail(email);
    //2.如果能查出来那么我们就将这个用户的激活码状态变为激活(active为1)
    if (user != null) {
        userService.updataemail(active, email);
        return mv;

    } else {
        System.out.println("激活失败!");
        return mv1;
    }
}

代码正确的话会有邮件 ,点击即可激活

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Mors.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值