JavaMail - 注册登陆邮箱验证激活

前言:

1、邮件发送协议 - SMTP,默认端口号 25。

用于把用户邮件从一个服务器转到下一个服务器

2、邮件接收协议 - POP3,默认端口号 110。

用于支持使用客户端远程管理在服务器上的电子邮件

3、搭建邮件服务器(易邮邮件服务器 + Foxmail):Email - 搭建自定义邮件服务器

JavaMail - 注册登陆邮箱验证激活_第1张图片

 

核心Jar包

JavaMail - 注册登陆邮箱验证激活_第2张图片

核心代码

package com.jy.utils;

import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.search.RecipientStringTerm;
import sun.nio.cs.MS1250;

/**
 * 邮箱工具
 */
public class MailUtil {
	
	public static void sendMail(String to,String code) throws Exception{

		// 1.创建连接对象
		Session session = Session.getDefaultInstance(new Properties(), new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication("[email protected]", "123456");
			}
		});

		// 2.创建邮件对象
		Message message = new MimeMessage(session);
		message.setFrom(new InternetAddress("[email protected]"));
		message.setRecipient(RecipientType.TO, new InternetAddress(to));

		//主题
		message.setSubject("来自dt的激活邮件");

		//正文
		message.setContent("

来自dt的激活邮件,点击链接激活账号:

http://localhost:8081/javamail01/active?code="+code+"

", "text/html;charset=utf-8"); // 3.发送激活邮件 Transport.send(message); } }
package com.jy.web.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jy.domain.User;
import com.jy.service.UserService;
import com.jy.service.impl.UserServiceImpl;
import com.jy.utils.UUIDUtil;

@WebServlet(urlPatterns="/regist")
public class RegistServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		try{
			req.setCharacterEncoding("utf-8");
			res.setCharacterEncoding("utf-8");

			String username = req.getParameter("username");
			String password = req.getParameter("password");
			String nickname = req.getParameter("nickname");
			String email = req.getParameter("email");

			User user = new User();
			user.setNickname(nickname);
			user.setUsername(username);
			user.setCode(UUIDUtil.code());
			user.setStatus(0);  // 状态码,0未激活,1激活
			user.setEmail(email);
			user.setPassword(password);

			UserService userService = new UserServiceImpl();
			userService.save(user);

			req.setAttribute("msg", "注册成功,请邮箱激活");
			req.getRequestDispatcher("/msg.jsp").forward(req, res);

		}catch(Exception e){
			
		}
	}
}
package com.jy.web.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jy.domain.User;
import com.jy.service.UserService;
import com.jy.service.impl.UserServiceImpl;

/**
 * 激活注册邮箱
 */
@WebServlet(urlPatterns="/active")
public class ActiveServlet extends HttpServlet{
	
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		try{
			req.setCharacterEncoding("utf-8");
			res.setCharacterEncoding("utf-8");

			String code = req.getParameter("code");
			UserService userService = new UserServiceImpl();
			User user = userService.findByCode(code);
			if(user!=null){
				// 已经激活
				user.setStatus(1);
				user.setCode(null);
				userService.update(user);
				req.setAttribute("msg", "激活成功,请登录");
			}else{
				// 激活失败
				req.setAttribute("msg", "激活失败,重新激活");
			}

			req.getRequestDispatcher("/msg.jsp").forward(req, res);

		}catch(Exception e){
			
		}
	}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


	

用户注册的页面

用户名:
密码:
昵称:
邮箱:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




信息反馈页面


	${msg}

 

源码下载:https://download.csdn.net/download/dream_weave/10869560

你可能感兴趣的:(#,JavaWeb,#,JavaMail)