前言:
1、邮件发送协议 - SMTP,默认端口号 25。
用于把用户邮件从一个服务器转到下一个服务器
2、邮件接收协议 - POP3,默认端口号 110。
用于支持使用客户端远程管理在服务器上的电子邮件
3、搭建邮件服务器(易邮邮件服务器 + Foxmail):Email - 搭建自定义邮件服务器
核心Jar包
核心代码
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("service@dt.com", "123456"); } }); // 2.创建邮件对象 Message message = new MimeMessage(session); message.setFrom(new InternetAddress("service@dt.com")); message.setRecipient(RecipientType.TO, new InternetAddress(to)); //主题 message.setSubject("来自dt的激活邮件"); //正文 message.setContent("<h1>来自dt的激活邮件,点击链接激活账号:</h1><h3><a href='http://localhost:8081/javamail01/active?code="+code+"'>http://localhost:8081/javamail01/active?code="+code+"</a></h3>", "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"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>用户注册的页面</h1> <form action="regist" method="post"> <table width="600" border="1"> <tr> <td>用户名:</td> <td><input type="text" name="username"/></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="password"/></td> </tr> <tr> <td>昵称:</td> <td><input type="text" name="nickname"/></td> </tr> <tr> <td>邮箱:</td> <td><input type="text" name="email"/></td> </tr> <tr> <td colspan="2"><input type="submit" value="提交"></td> </tr> </table> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>信息反馈页面</title> </head> <body> ${msg} </body> </html>