在開發項目或軟件產品功能的過程中,經常遇到需要將數據、提醒、公告等通過郵件的方式發送給客戶或管理人員,也就是通過郵件的發送來執行業務的規則。
java mail用來建立郵件和消息應用程序。它可以方便的執行一些常用的郵件傳輸,支持PO3,IMAP,SMTP,既可以作爲JavaSE平臺的可選包,也可以在JavaEE平臺中使用。
目錄
④使用java mail實現郵件的發送--設計一個簡單的郵件發送系統
1.java mail環境的構建
直接使用java mail實現郵件的發送比較複雜,這裏採用Commons-Email實現,Commons-Email是Apache提供的一個開源的API,是對java mail的封裝,使用它時用到的Jar包包括:mail.jar,additionnal.jar,activation.jar和commons-emails-1.2.jar,主要包括四個類。
SimpleEmail | 發送簡單的email,不能添加附件 |
MultiPartEmail | 發送文本郵件,可以添加多個附件 |
HtmlEmail | 發送HTML格式的郵件,同時具有MultiPartEmail類的所有功能 |
EmailAttchment | 附件類,可以添加本地資源,也可以指定網絡資源,在發送時將網絡資源下載發送 |
2.發送郵件
①發送簡單的郵件
SimpleEmail email=new SimpleEmail();
email.setHostName("smtp.163.com"); //設置郵箱服務器
email.setAuthentication("用戶名","密碼")
email.addTo("***@**.com"); //收件人
email.setFrom("***@**.com");//發件人
email.setSubject("Subject");//郵件主題
email.setMsg("你好");//郵件內容
email.send(); //發送郵件
②發送帶附件的郵件
EmailAttachment attachment=NEW EmailAttachment();
attachment.setPath("附件路徑");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
MultiPartEmail email=new MultiPartEmail();
email.setHostName("smtp.163.com"); //設置郵箱服務器
email.setAuthentication("用戶名","密碼")
email.addTo("***@**.com"); //收件人
email.setFrom("***@**.com");//發件人
email.setSubject("Subject");//郵件主題
email.setMsg("你好");//郵件內容
email.attach(attachment);
email.send(); //發送郵件
③發送HTML形式的郵件
HtmlEmail email=new HtmlEmail();
email.setHostName("smtp.163.com"); //設置郵箱服務器
email.setAuthentication("用戶名","密碼")
email.addTo("***@**.com"); //收件人
email.setFrom("***@**.com");//發件人
email.setSubject("Subject");//郵件主題
email.setMsg("<font color='red'>123</font>");//郵件內容
email.send(); //發送郵件
④使用java mail實現郵件的發送--設計一個簡單的郵件發送系統
mail.jsp--jsp文件, 完成信息的獲取與傳遞
<%@ 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>
<h1 align="center">發送文本格式的郵件</h1>
<form action="sendmail" method="post" name="form1">
<table width="48%" border="1" align="center" cellspacing="1">
<tr><td width="20%" height="30">收件人地址:</td>
<td width="80%" height="30"><input name="to" type="text" size="40"></td></tr>
<tr><td height="30">標題:</td>
<td height="30"><input name="title" type="text" size="40"></td></tr>
<tr><td height="30">郵件內容:</td>
<td height="30"><textarea name="content" cols="60" rows="40" id="content"></textarea></td></tr>
<tr align="center"><td colspan="2" height="40">
<input type="submit" value="發送">
<input type="reset" value="重輸">
</td></tr>
</table>
</form>
</body>
</html>
sendmail.java--servlet類,完成工程的操作
package bean;
import java.io.IOException;
import java.io.PrintWriter;
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 org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
/**
* Servlet implementation class sendmail
*/
@WebServlet("/sendmail")
public class sendmail extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public sendmail() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
request.setCharacterEncoding("UTF-8");
SimpleEmail email=new SimpleEmail();
email.setHostName("smtp.163.com");
email.setAuthentication("用戶名", "密碼");//需要自己填寫
try {
email.addTo(request.getParameter("to"));
email.setFrom("用戶郵箱");//需要自己填寫
email.setSubject(request.getParameter("title"));
email.setMsg(request.getParameter("content"));
email.send();
out.println("郵件發送成功");
} catch (EmailException e) {
// TODO Auto-generated catch block
e.printStackTrace();
out.println("郵件發送失敗");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}