web项目 -页面注册 -邮箱链接注册

本文介绍了使用Java进行网页邮箱注册验证的步骤,以网易163邮箱为例,涉及邮件发送协议的开通与授权,以及activation.jar和mail.jar库的使用。通过创建发邮件的Java类并调用发送邮件工具,实现注册页面的邮箱验证功能。
摘要由CSDN通过智能技术生成

这是我其中一个项目的做法

 

我这里上张注册界面的图


 

 

 

这里我们主要讲的是邮箱验证, 以 网易163的邮箱为例子,当然要做邮箱发送需要开通协议并授权(不多说,自行bai度)

 



 

 

还要准备两个jar包放入lib里

1.     activation.jar(附件可以下载)

2.     mail.jar(附件可以下载)
 ------------------------------------------------------------------------------------------------------------------------------------

开始撸代码:

                                1.我们先创建一个发邮件的java类

/**
 * 发送邮件工具
 * @author Administrator
 *
 */
public class EmailTools {
	/***
	 * 邮件发送方法
	 * @param address 邮件接收人
	 * @param subject 邮件的标题
	 * @param content 邮件的内容
	 * @param true 为成功 false 为失败 
	 */
	public static boolean send(String address,String subject,String content){	
		//1 创建session
		Properties pro = new Properties();//能储存键值对操作,方便存储
		pro.setProperty("mail.transport.protocol", "smtp");	
		pro.put("mail.host", "smtp.163.com");
                //下面输入用户名
		pro.put("mail.from", "用户名写这@163.com");
		//相当于客户端与邮件服务器的连接对象
		Session session = Session.getDefaultInstance(pro);
		//开启调试模式
		session.setDebug(true);
		//2 获取邮件发送对象
		try {
			Transport transport = session.getTransport();
			//设置发送人邮件账号第一个是账号名,第二个是授权码
			transport.connect("888888","888888");
			//3 创建邮箱信息
			MimeMessage message = new MimeMessage(session);
			message.setSubject(subject);//设置邮件的标题
			//设置邮件的内容和页面编码
			message.setContent(content,"text/html;charset=UTF-8");
			//4 发送邮件
			transport.sendMessage(message, InternetAddress.parse(address));
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
}

 

 

                    2.调用上面的发送邮件工具(主要是register的方法)

 

package com.carshop.action.customer;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.carshop.common.EmailTools;
import com.carshop.dao.UserDao;
import com.carshop.dto.User;

@Controller
@RequestMapping("customer")
public class UserAction {
	UserDao dao = null;
	/**
	 * 用户注册
	 * @return
	 */
	@RequestMapping("/register")
	public String register(User user){
		//发送激活邮箱
		String activeCode = String.valueOf(System.currentTimeMillis());
		String activeURL = ("http://localhost:1314/carshop/customer/active?userId="+user.getUserId()+"&activeCode="+activeCode);
		StringBuilder content = new StringBuilder();
		content.append(user.getUserId()+"您好,请点击下面的连接进行激活账号:</br>");
		content.append("<a href='"+activeURL+"'>"+activeURL+"</a>");
		boolean flag = EmailTools.send(user.getEmail(), "账号注册(标题)", content.toString());
		if(flag){
		
			dao = new UserDao();
			//创建账号的时间
			user.setCreateDate(new Date());
			user.setActiveCode(activeCode);
			System.out.println("register--"+user);
			dao.save(user);
		}else{
			return "register";
		}
		return "index";
	}
	//激活状态
	@RequestMapping("/active")
	public String avctive(String userId,String activeCode,Model data){
		dao = new UserDao();
		Map<String,String> map = new HashMap<>();
		map.put("userId", userId);
		map.put("activeCode", activeCode);
		int count = dao.activeUser(map);
		String tip = count>0?"激活成功":"激活失败";
		data.addAttribute("tip",tip);
		return "login";
	}
	//注册检查是否重复
	@RequestMapping("/userIdCheck")
	@ResponseBody
	public Map<String,Object> userIdChack(String userId){
		dao = new UserDao();
		boolean flag = dao.getUserByUserId(userId);
		Map<String,Object> map = new HashMap<>();
		map.put("status", flag?1:0);
		return map;
	}
}

           

                        附上我的注册页面(带页面jq验证)

 

 

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>注册页面</title>

<body>
<div class="h_title">欢迎注册</div>
</div>

<div class="content">

<form class="fr-re" action="${ctx}/customer/register" method="post">
	<label>用户名</label>
	<input class="username" type="text" name="userId" id="userId" placeholder="6-20个大小写英文字母.符号或数字组成"/>
	<div class="clear"></div><strong  id="userId_tip" class="normal">* 用户名不能为空!</strong>
	
	<label>密码</label>
	<input type="password" name="passWord" id="pwd" placeholder="输入密码"/>
	<div class="clear"></div><strong id="pwd_tip" class="normal">* 设置密码不能为空!</strong>
	
	<label>确认密码</label>
	<input type="password" name="okPwd" id="okPwd" placeholder="再次输入密码"/>
	<div class="clear"></div><strong id="okPwd_tip" class="normal">* 确认密码不能为空!</strong>
	
	<label>Email</label>
	<input type="text" name="email" id="email" placeholder="请输入您的邮箱地址"/>
	<div class="clear"></div><strong id="email_tip" class="normal">* 邮箱地址不能为空!</strong>
	
	<label>姓名</label>
	<input type="text" id="name" name="name" placeholder="请输入您的姓名"/>
	<div class="clear"></div><strong id="name_tip" class="normal">* 姓名不能为空!</strong>
	
	<label>手机号</label>
	<input type="text" id="phone" name="phone" placeholder="请输入您的常用手机号"/>
	<div class="clear"></div><strong id="phone_tip" class="normal">* 手机号码不能为空!</strong>
	
	<label>验证码</label>
	<input class="code" type="text" value=""/>
	<input class="but" type="button" value="点击获取">
	<div class="clear"></div>
	
	<label class="protocol">《用户协议》</label>
	<input class="che" id="agreen" type="checkbox" checked="checked"/>
	<strong id="agreen_tip" class="normal"></strong>
	
	<a href="javascript:void(0)"><input class="butt" type="submit" name="registe" value="点击注册"/></a>
</form>

</div>
<@carshop_footer.footer/>
<script type="text/javascript">
		$(function(){
			
			//用户名是否重复检查
			$("#userId").blur(function(){
				var url = "${ctx}/customer/userIdCheck";
				$.post(url,$("#userId"),function(data){
						if(data.status == 1){
							$("#userId_tip").html("用户名已存在!").show();
							$("#userId").focus(); // 获取焦点
							var isSubmit = false;
						}else{
							$("#userId_tip").hide();
							var isSubmit = true;
						}
				},"json");
			});
			
			
			
			
			/** 隐藏所有的提示信息 */
			$("strong[id$='_tip']").hide(); 
			/** 为表单绑定提交事件onsubmit */
			$("form[class='fr-re']").submit(function(){
				/** 表单输入校验 */
				var userId = $("#userId");
				var pwd = $("#pwd");
				var okPwd = $("#okPwd");
				var email = $("#email");
				var provice = $("#provice");
				
				var phone = $("#phone");
				
				
				
				

				/** 隐藏所有的提示信息 */
				$("strong[id$='_tip']").hide(); 

				/** 定义是否提交表单的标识符 */
				var isSubmit = true;
				if ($.trim(userId.val()) == ""){
					isSubmit = false;
					$("#userId_tip").html("* 用户名不能空!").show();
					userId.focus(); // 获取焦点
				}else if (!/^\w{5,20}$/.test($.trim(userId.val()))){
					isSubmit = false;
					$("#userId_tip").html("* 用户名必须在5-20位之间!").show();
					userId.focus(); // 获取焦点
				}else if ($.trim(pwd.val()) == ""){
					isSubmit = false;
					$("#pwd_tip").html("* 设置密码不能空!").show();
					pwd.focus(); // 获取焦点
				}else if (!/^\w{6,20}$/.test($.trim(pwd.val()))){
					isSubmit = false;
					$("#pwd_tip").html("* 设置密码必须在6-20位之间!").show();
					pwd.focus(); // 获取焦点
				}else if ($.trim(okPwd.val()) == ""){
					isSubmit = false;
					$("#okPwd_tip").html("* 确认密码不能空!").show();
					okPwd.focus(); // 获取焦点
				}else if (!/^\w{6,20}$/.test($.trim(okPwd.val()))){
					isSubmit = false;
					$("#okPwd_tip").html("* 确认密码必须在6-20位之间!").show();
					okPwd.focus(); // 获取焦点
				}else if (pwd.val() != okPwd.val()){
					isSubmit = false;
					$("#okPwd_tip").html("* 两次密码输入不一致!").show();
				}else if ($.trim(email.val()) == ""){ // 邮箱
					isSubmit = false;
					$("#email_tip").html("* 邮箱地址不能为空!").show();
					email.focus();
				}else if ($.trim(name.val()) == ""){ // 姓名
					isSubmit = false;
					$("#name_tip").html("* 姓名不能为空!").show();
					name.focus();
				}else if ($.trim(phone.val()) == ""){ // 手机号码
					isSubmit = false;
					$("#phone_tip").html("* 手机号码不能为空!").show();
					phone.focus();
				}else if (!/^1[3|4|5|8]\d{9}$/.test($.trim(phone.val()))){ // 手机号码
					isSubmit = false;
					$("#phone_tip").html("* 手机号码格式不正确!").show();
					phone.focus();
				}else if (!$("#agreen").attr("checked")){ // 同意
					isSubmit = false;
					$("#agreen_tip").html("* 请勾选我已经阅读并同意!").show();
				}
				return isSubmit;
			});
		});
	</script>
</body>
</html>

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值