Apache Commons-Email 使用簡介

jopen 12年前發布 | 1K 次閱讀

Java開發中,為了避免重復勞動,提高效率,我向來愿意采用組件包拼接式開發。今天為大家奉獻上的是使用多年的Apache Commons項目中組件Email,其它組件會在以后適當時候發布。
在Java中進行Email操作并不難,因為Sun發布了J2EE組件JavaMail,但是還可以更簡單。Apache基于JavaMainl進一步開發了Email組件。
官網:http://commons.apache.org/email/
所以在部署的時候除了下載Commons-Email外,還要有JavaMail。
下面是一段發送簡單文本信息郵件的例子:

<%@ page language="java" contentType="text/html; charset=gb2312"
        pageEncoding="UTF-8"%>
<%@page import="org.apache.commons.mail.SimpleEmail" %>
<%
String action=request.getParameter("action");
if("send".equals(action)){
  SimpleEmail email = new SimpleEmail();
  email.setHostName("mail.server.com");
  email.addTo("to@XX.com", "Jerry");
  email.setFrom("from@XX.com", "Jerry");
  email.setSubject("測試郵件");
  String body=new String("Java課上的\r\n測試程序");
  email.setCharset("gb2312");//設置文件內容編碼
  email.setMsg(body);
  email.send();
}
%>
<!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=gb2312">
<title>發送郵件</title>
</head>
<body>
<h1>發送郵件-純文本</h1>
<form method="post" action="sendtext.jsp?action=send">
<input type="submit" value="發送" />
</form>
</body>
</html>
</div> 運行很簡單。

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!