前言:
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("[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("<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>