JSP與JavaMail之3(試著寫第一個程式)

來源:互聯網
上載者:User
js|程式 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個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.