(一)郵箱登錄校驗
準備:
申請smtp授權碼(自行百度、google)
activation.jar、javax.mail-1.6.0.jar
項目結構
welcome.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'welcome.jsp' starting page</title>
</head>
<body style="background: url(res/email.jpg)">
<h1 align="center">郵箱登錄</h1>
<form action="Login" method="post">
<table align="center">
<tr><td>用戶名:<input type="text" name="user"/></td></tr>
<tr><td>授權碼:<input type="password" name="pswd"/></td></tr>
<tr><td><input type="submit" value="登錄"/>
<input type="reset" value="重置">
</td></tr>
</table>
</form>
</body>
</html>
Login.class (servlet)
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
String user = request.getParameter("user");
String pswd = request.getParameter("pswd");
String host = "pop3.163.com";
//登錄驗證
if(EmailUtils.getFolder(host, user, pswd)!=null){
request.getSession().setAttribute("user", user);
request.getSession().setAttribute("pswd", pswd);
response.sendRedirect("main.jsp");
}else{
response.sendRedirect("error.jsp");
}
}
EmailUtils.class
/**
* 登錄校驗、收取郵件獲取folder
*
* @param host
* @param username
* @param password
* @return
*/
public static Folder getFolder(String host, String user, String pswd) {
Properties prop = new Properties();
prop.setProperty("mail.store.protocol", "pop3");
prop.setProperty("mail.pop3.host", host);
// javax.mail.Session mailSession = Session.getDefaultInstance(prop,
// null);
Session mailSession = Session.getInstance(prop, null);
mailSession.setDebug(false);
try {
Store store = mailSession.getStore("pop3");
store.connect(host, user, pswd);
Folder folder = store.getFolder("inbox");
folder.open(Folder.READ_WRITE);
return folder;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
登錄校驗之後
main.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'main.jsp' starting page</title>
</head>
<body style="background: url(res/main.jpeg)">
<h3>歡迎${sessionScope.user}</h3>
<br>
<a href="newEmail.jsp">發郵件</a>
<br>
<a href="Recieve">收郵件</a>
</body>
</html>
error.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'error.jsp' starting page</title>
</head>
<body>
<h2>Error!</h2>
</body>
</html>
因爲某tx郵箱太難伺候,乾脆改成163郵箱了,想登錄其他郵箱自己改地址