JSP與JavaMail之4(發送HTML格式郵件)

14 篇文章 0 订阅
    1.介紹: 

Java Mail API的開發是SUN為Java開發者提供公用API框架的持續努力的良好例證。提倡公用框架,反對受限於供應商的解決方案,充分預示著一個日益開放的開發環境的建立。 
  Java Mail API的結構本身證明了它的開發者的基本目標之一--軟件開發的工作量應該取決於應用程序本身的複雜程度以及開發者所要求的控制程度。換句話說,Java Mail API盡可能地保持簡單。乍看起來,JavaMail API所擁有的類總數以及類之間的關係可能讓人誤解為要花費漫長的學習時間。實際上,一旦正式開始使用,你就會發現該API不失為在應用程序中加入健壯的郵件/通訊支持的簡單工具。 

2.安裝: 

安裝前要確保你的機子上安裝得有標準版的JDK和Web服務器,並且已配置好,有關它們的安裝方法,請參考其它文章(網上到處都有). 

(1).安裝JavaMail API。現在最常用的 JavaMail API 版本是1.3. 
  要使用 JavaMail 1.3 API,請下載 JavaMail 1.3 實現,解開Javamail-1_3.zip 文件,並將  mail.jar 文件添加到 CLASSPATH 中。除了核心類,隨版本 1.3 實現一起提供的還有 SMTP、IMAP4 和 POP3 供應商。 
   
(2).JavaBeans Activation Framework(1.0.2版) 的安裝 
JavaMail API  的所有版本都需要 JavaBeans Activation Framework 來支持任意數據塊的輸入及相應處理。功能似乎不多,但目前許多瀏覽器和郵件工具中都能找到這種基本的 MIME 型支持。下載完框架後,解開 jaf1_0_2.zip 文件,並將 activation.jar 文件添加到 CLASSPATH 中。 
   

注: 如果您使用的JDK是J2EE,就沒有什麼特定的事非要用基本  JavaMail API來做不可;J2EE 的類就能處理了,因為它本身就包含有JavaMail API和JAF,您只需要確將  j2ee.jar 文件添加到您的CLASSPATH 中並已全部設置好。 4.試著編寫第一個發送程序

在前面我們已對JavaMail作了一些介紹,下面我們可試著寫自己的程序了.

首先,我們先寫一個撰寫郵件的html程序index.htm,如下:
-------------------------------------------------------------------------------------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>撰寫郵件</title>
</head>

<body>
<form name="form1" method="post" action="testmail.jsp">
<table width="75" border="0" align="center" cellspacing="1" bgcolor="#006600" class="black">
<tr bgcolor="#FFFFFF">
<td width="24%">收信人地址:</td>
<td width="76%">
<input name="to" type="text" id="to"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>主題:</td>
<td>
<input name="title" type="text" id="title"></td>
</tr>
<tr>
<td height="107" colspan="2" bgcolor="#FFFFFF">
<textarea name="content" cols="50" rows="5" id="content"></textarea></td>
</tr>
<tr align="center">
<td colspan="2" bgcolor="#FFFFFF">
<input type="submit" name="Submit" value="發送">
<input type="reset" name="Submit2" value="重置">
</td>
</tr>
</table>
</form>
</body>
</html>


接著,我們再寫一個處理程序testmail.jsp,如下:
-----------------------------------------------------------------------------------------
<%@ page contentType="text/html;charset=GB2312" %>
<%request.setCharacterEncoding("gb2312");%><!--中文處理代碼-->

<!--引入要用到的類庫-->
<%@ page import="java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>發送成功</title>
</head>

<body>
<%
try{

//從html表單中獲取郵件信息
String tto=request.getParameter("to");
String ttitle=request.getParameter("title");
String tcontent=request.getParameter("content");

Properties props=new Properties();//也可用Properties props = System.getProperties();
props.put("mail.smtp.host","smtp.163.net");//存儲發送郵件服務器的信息
props.put("mail.smtp.auth","true");//同時通過驗證
Session s=Session.getInstance(props);//根據屬性新建一個郵件會話
s.setDebug(true);

MimeMessage message=new MimeMessage(s);//由郵件會話新建一個消息對像

//設置郵件
InternetAddress from=new InternetAddress("boy@163.net");
message.setFrom(from);//設置發件人
InternetAddress to=new InternetAddress(tto);
message.setRecipient(Message.RecipientType.TO,to);//設置收件人,並設置其接收類型為TO
message.setSubject(ttitle);//設置主題
message.setText(tcontent);//設置信件內容
message.setSentDate(new Date());//設置發信時間

//發送郵件
message.saveChanges();//存儲郵件信息
Transport transport=s.getTransport("smtp");
transport.connect("smtp.163.net","boy","iloveyou");//以smtp方式登錄郵箱
transport.sendMessage(message,message.getAllRecipients());//發送郵件,其中第二個參數是所有
//已設好的收件人地址
transport.close();

%>
<div align="center">
<p><font color="#FF6600">發送成功!</font></p>
<p><a href="recmail.jsp">去看看我的信箱</a><br>
<br>
<a href="index.htm">再發一封</a> </p>
</div>
<%
}catch(MessagingException e){
out.println(e.toString());
}
%>
</body>
</html>

**********************************注意***************************************

有好多書上和網上的文章在關鍵部分都是這樣寫testmail.jsp的,如下:

String tto=request.getParameter("to");
String ttitle=request.getParameter("title");
String tcontent=request.getParameter("content");
Properties props=new Properties();
props.put("mail.smtp.host","smtp.163.net");
Session s=Session.getInstance(props);
MimeMessage message=new MimeMessage(s);

InternetAddress from=new InternetAddress("boy@163.net");
message.setFrom(from);
InternetAddress to=new InternetAddress(tto);
message.setRecipient(Message.RecipientType.TO,to);

message.setSubject(ttitle);
message.setText(tcontent);
message.setSentDate(new Date());

Store store=s.getStore("pop3");
store.connect("pop.163.net","boy","iloveyou");//以pop3的方式登錄郵箱
Transport transport=s.getTransport("smtp");
transport.send(message);
store.close();

事實上,這種方式並不可靠,因為很多電子郵局的smtp服務器要求我們通過驗證,所以用這種方式發郵件時,只能發給同類郵箱(即相同smtp的郵箱),甚至有時同類郵箱也發不出去.以上兩種方式我試過很多次,結果證明第一種方式是最可靠的.


好了,我相信你應該會寫最簡單的Email發送程序了.OK,下一次我們將說說怎樣寫發送HTML格式的郵件.4.試著編寫第一個發送程序

在前面我們已對JavaMail作了一些介紹,下面我們可試著寫自己的程序了.

首先,我們先寫一個撰寫郵件的html程序index.htm,如下:
-------------------------------------------------------------------------------------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>撰寫郵件</title>
</head>

<body>
<form name="form1" method="post" action="testmail.jsp">
<table width="75" border="0" align="center" cellspacing="1" bgcolor="#006600" class="black">
<tr bgcolor="#FFFFFF">
<td width="24%">收信人地址:</td>
<td width="76%">
<input name="to" type="text" id="to"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>主題:</td>
<td>
<input name="title" type="text" id="title"></td>
</tr>
<tr>
<td height="107" colspan="2" bgcolor="#FFFFFF">
<textarea name="content" cols="50" rows="5" id="content"></textarea></td>
</tr>
<tr align="center">
<td colspan="2" bgcolor="#FFFFFF">
<input type="submit" name="Submit" value="發送">
<input type="reset" name="Submit2" value="重置">
</td>
</tr>
</table>
</form>
</body>
</html>


接著,我們再寫一個處理程序testmail.jsp,如下:
-----------------------------------------------------------------------------------------
<%@ page contentType="text/html;charset=GB2312" %>
<%request.setCharacterEncoding("gb2312");%><!--中文處理代碼-->

<!--引入要用到的類庫-->
<%@ page import="java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>發送成功</title>
</head>

<body>
<%
try{

//從html表單中獲取郵件信息
String tto=request.getParameter("to");
String ttitle=request.getParameter("title");
String tcontent=request.getParameter("content");

Properties props=new Properties();//也可用Properties props = System.getProperties();
props.put("mail.smtp.host","smtp.163.net");//存儲發送郵件服務器的信息
props.put("mail.smtp.auth","true");//同時通過驗證
Session s=Session.getInstance(props);//根據屬性新建一個郵件會話
s.setDebug(true);

MimeMessage message=new MimeMessage(s);//由郵件會話新建一個消息對像

//設置郵件
InternetAddress from=new InternetAddress("boy@163.net");
message.setFrom(from);//設置發件人
InternetAddress to=new InternetAddress(tto);
message.setRecipient(Message.RecipientType.TO,to);//設置收件人,並設置其接收類型為TO
message.setSubject(ttitle);//設置主題
message.setText(tcontent);//設置信件內容
message.setSentDate(new Date());//設置發信時間

//發送郵件
message.saveChanges();//存儲郵件信息
Transport transport=s.getTransport("smtp");
transport.connect("smtp.163.net","boy","iloveyou");//以smtp方式登錄郵箱
transport.sendMessage(message,message.getAllRecipients());//發送郵件,其中第二個參數是所有
//已設好的收件人地址
transport.close();

%>
<div align="center">
<p><font color="#FF6600">發送成功!</font></p>
<p><a href="recmail.jsp">去看看我的信箱</a><br>
<br>
<a href="index.htm">再發一封</a> </p>
</div>
<%
}catch(MessagingException e){
out.println(e.toString());
}
%>
</body>
</html>

**********************************注意***************************************

有好多書上和網上的文章在關鍵部分都是這樣寫testmail.jsp的,如下:

String tto=request.getParameter("to");
String ttitle=request.getParameter("title");
String tcontent=request.getParameter("content");
Properties props=new Properties();
props.put("mail.smtp.host","smtp.163.net");
Session s=Session.getInstance(props);
MimeMessage message=new MimeMessage(s);

InternetAddress from=new InternetAddress("boy@163.net");
message.setFrom(from);
InternetAddress to=new InternetAddress(tto);
message.setRecipient(Message.RecipientType.TO,to);

message.setSubject(ttitle);
message.setText(tcontent);
message.setSentDate(new Date());

Store store=s.getStore("pop3");
store.connect("pop.163.net","boy","iloveyou");//以pop3的方式登錄郵箱
Transport transport=s.getTransport("smtp");
transport.send(message);
store.close();

事實上,這種方式並不可靠,因為很多電子郵局的smtp服務器要求我們通過驗證,所以用這種方式發郵件時,只能發給同類郵箱(即相同smtp的郵箱),甚至有時同類郵箱也發不出去.以上兩種方式我試過很多次,結果證明第一種方式是最可靠的.


好了,我相信你應該會寫最簡單的Email發送程序了.OK,下一次我們將說說怎樣寫發送HTML格式的郵件.5.發送HTML格式的郵件

所謂HTML格式,就是超文本格式.你的郵件可以用HTML代碼編寫,發給對方後,對方收到的將是信息將是超文本,超文本比純文本好看多了.下以面是在以前例子的基礎上修改的程序:

<%@ page contentType="text/html;charset=GB2312" %>
<%request.setCharacterEncoding("gb2312");%>
<%@ page import="java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>發送成功</title>
</head>

<body>
<%
try{
String tto=request.getParameter("to");
String ttitle=request.getParameter("title");
String tcontent=request.getParameter("content");
Properties props=new Properties();
props.put("mail.smtp.host","127.0.0.1");
props.put("mail.smtp.auth","true");
Session s=Session.getInstance(props);
s.setDebug(true);

MimeMessage message=new MimeMessage(s);

//給消息對像設置發件人/收件人/主題/發信時間
InternetAddress from=new InternetAddress("xxf@cafe.com");
message.setFrom(from);
InternetAddress to=new InternetAddress(tto);
message.setRecipient(Message.RecipientType.TO,to);
message.setSubject(ttitle);
message.setSentDate(new Date());


//給消息對像設置內容
BodyPart mdp=new MimeBodyPart();//新建一個存放信件內容的BodyPart對像
mdp.setContent(tcontent,"text/html;charset=gb2312");//給BodyPart對像設置內容和格式/編碼方式
Multipart mm=new MimeMultipart();//新建一個MimeMultipart對像用來存放BodyPart對
//象(事實上可以存放多個)
mm.addBodyPart(mdp);//將BodyPart加入到MimeMultipart對像中(可以加入多個BodyPart)
message.setContent(mm);//把mm作為消息對象的內容

message.saveChanges();
Transport transport=s.getTransport("smtp");
transport.connect("127.0.0.1","xxf","coffee");
transport.sendMessage(message,message.getAllRecipients());
transport.close();
%>
<div align="center">
<p><font color="#FF6600">發送成功!</font></p>
<p><a href="recmail.jsp">去看看我的信箱</a><br>
<br>
<a href="index.htm">再發一封</a> </p>
</div>
<%
}catch(MessagingException e){
out.println(e.toString());
}
%>
</body>
</html>

注:撰寫郵件的html文件仍然和前面(請參考jsp和Java Mail(三))那個一樣,不需要作任何修改.

怎麼樣,這個程序是不是很簡單呢?如果還有什麼不懂的話,請在下面留言.下一次我們將要講一講怎樣發送附件.
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值