使用Apache Commons Email組件發送電子郵件
下載Apache Commons Email組件
下載地址:http://commons.apache.org/mail/
下載:commons-email-1.2-bin.zip 得到:commons-email-1.2.jar
新建web project項目:
Build Path: commons-email-1.2.jar javaMail1.2.jar
代碼如下:
sendMail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<html>
<head><title>Send E-Mail Form</title></head>
<style>* {font-family:"宋體" ; font-size: 14px }</style>
<body>
<p><font color="red">發送郵件的程序</font></p>
<form action="<%=request.getContextPath()%>/SendMailServlet.java" method="post">
<table cellspacing="2" cellpadding="2" border="0">
<tr><td>收件人</td>
<td><input type="text" name="to" size="30"></td>
</tr>
<tr><td>發件人</td>
<td><input type="text" name="from" size="30"></td>
</tr>
<tr><td>主題</td>
<td><input type="text" name="subject" size="30"></td>
</tr>
<tr><td valign="top">正文</td>
<td><textarea cols="50" rows="15" name="body"></textarea></td>
</tr>
<tr><td></td><td>
<input type="submit" value="提交"><input type="reset" value="重置"></td>
</tr>
</table>
</form>
</body>
</html>
package webbook.chapter16;
import java.io.IOException;
import javax.servlet.http.*;
import javax.servlet.ServletException;
import org.apache.commons.mail.*;
public class SendMailServlet extends HttpServlet {
private static final long serialVersionUID = -3302031686208755627L;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
SimpleEmail email = new SimpleEmail();
email.setHostName("smtp.163.com"); //郵件服務器
email.setAuthentication("[email protected]", "xxxxxxxx"); //用戶名,密碼
try {
email.setCharset("UTF-8");
email.addTo(request.getParameter("to"));
email.setFrom(request.getParameter("from"));
email.setSubject(request.getParameter("subject"));
email.setMsg(request.getParameter("body"));
email.send();
request.setAttribute("sendmail.message", "郵件發送成功!");
} catch (EmailException e) {
e.printStackTrace();
request.setAttribute("sendmail.message", "郵件發送失敗!");
}
request.getRequestDispatcher("/sendResult.jsp").forward(request, response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
}
sendResult.jsp
<html>
<head><title>Send E-Mail result page</title></head>
<style>
* { font-family:"宋體" ; font-size: 14px }
</style>
<body>
<p><font color="red">${requestScope['sendmail.message']}</font></p>
<p><a href="<%=request.getContextPath()%>/sendMail.jsp">返回</a></p>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>SendMailServlet</servlet-name>
<servlet-class>SendMailServlet.SendMailServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SendMailServlet</servlet-name>
<url-pattern>*.java</url-pattern>
</servlet-mapping>
</web-app>
最後訪問:http://localhost/email/sendMail.jsp