ASP.NET中發送Email完整實例
本文舉例說明在ASP.NET中發送Email的眾多可能性,內容覆蓋了諸如Email格式、優先權、附件及Email編碼等方面。
ASP.NET被賦予了一個發送Email的新對象,名為SmtpMail。使用SmtpMail對象從ASP.NET頁面中發送Email時,可以遵循以下簡單步驟:
▲包含與郵件有關類所需要的名稱空間; ▲例示一個信息對象,設置屬性; ▲使用SmtpMail對象實例的send方法發送郵件。
現在我們就來一步一步地研究從一個ASP.NET頁面發送Email的過程。我們使用了VB來說明這個例子,最后將包含VB和C#的完整代碼。
第一步:包含名稱空間
在ASP.NET 頁面中引入System.Web.Util 名稱空間,這個名稱空間中包括了發送一個email所必須的所有對象。這些對象是:
SmtpMail:代表郵件系統,用于發送email。 MailMessage:代表一個信息,其屬性包括發件人地址、收件人地址等。 MailFormat:代表信息的格式:HTML、文本等。 MailAttachment:代表一個email附件。 MailEncoding enum:代表Base64 或Uuencode的任何編碼。取值范圍:Base64、UUencode MailPriority enum:用來為信息設置優先權。值為:高、低、一般。 <% @Import Namespace = "System.Web.Util" %>
第二步:例示 MailMessage 對象
使用以下語句來例示MailMessage對象:
Dim mailObj AS new MailMessage
用MailMessage對象的屬性來準備郵件。MailMessage對象有下列屬性:
From:發件人的Email地址 To:收件人的Email地址 Subject:email的主題 Body:email的主體 CC:email抄送的收件人列表 BCC:email暗送的收件人列表 Priority:信息的優先權:高、低或一般 BodyEncoding:信息體的編碼,如果有的話,就是Base64或UUencode BodyFormat:信息的格式:Html 或text Attachments:附加到email 的MailAttachment對象列表,主要就是對這個對象集合的一個引用
下面這段代碼示范了使用MailMessage 對象屬性的方法,它們代表了將在本例中創建的一個信息,這個信息要用SmtpMail對象來發送。在例子中,mailObj引用了信息對象的例示:
mailObj.From = "abc@mydomain.com" mailObj.To = Request.Form ("to") mailObj.Subject = "subject of the mail" mailObj.Body = "Message of the mail"
第三步:發送Email
這時,我們就可以使用SmtpMail 對象的Send方法來發送郵件了:
SmtpMail.Send(mailObj)
完整實例
最后,我們把以上解釋的屬性結合在一個完整的例子中。為了說明用ASP.NET 發送一個email 的全部可能性,我們還包含了一些“小技巧”。下面是使用VB.NET的完整例子:
<%@page language="VB" %> <%@Import Namespace="System.Web.Util" %> <HTML><BODY> <SCRIPT LANGUAGE="VB" RUNAT="server"> ' This method is called on the server when the submit ' button is clicked on the client and when the page ' posts back to itself Sub SendMail (Obj As Object, E As EventArgs) ' Instantiate a MailMessage object. This serves as a message object ' on which we can set properties. Dim mailObj AS new MailMessage ' Set the from and to address on the email mailObj.From = Request.Form("From") mailObj.To = Request.Form("To") mailObj.Subject = "Subject Of the Mail" mailObj.Body = "Body of the Mail" ' Optional: HTML format for the email mailObj.BodyFormat = MailFormat.Html ' Optional: Encoding for the message mailObj.BodyEncoding = MailFormat.Base64 ' Optional: Set the priority of the message to high mailObj.Priority = MailPriority.High ' Optional: Attach a file to the email. ' Note here that we have created a MailAttachment object to ' attach a file to the email mailObj.Attachments.Add(new MailAttachment("c:\test.doc")) ' Send the email using the SmtpMail object SmtpMail.Send(mailObj) End Sub </SCRIPT> <asp:label ID="Headingmsg" Text="Enter Your Email Address:" RUNAT="server"/> <FORM METHOD="post" RUNAT="server"> Email Recipient: <INPUT TYPE="text" NAME="to"> <br> Email Sender: <INPUT TYPE="text" NAME="from"> <INPUT TYPE="submit" NAME="Submit" VALUE="Send Mail" RUNAT="server" OnServerClick="SendMail"> </FORM> </BODY>
在以上例子中,From(發件人)和 To(收件人)的Email地址是從相應的文本框中收集的,點擊“Send Mail”(發送郵件)按鈕時,郵件就被發送出去。當“Send Mail”(發送郵件)按鈕被點擊時,表單回遞到它自己,在服務器上“SendMail”(發送郵件)程序被觸發,郵件被發送。下面是使用C#的例子:
<%@page language="C#" %> <%@Import Namespace="System.Web.Util" %> <HTML><BODY> <SCRIPT LANGUAGE="C#" RUNAT="server"> // This method is called on the server when the submit // button is clicked on the client and when the page // posts back to itself public void SendMail (Object Obj, EventArgs E) { // Instantiate a MailMessage object. This serves as a message object // on which we can set properties. MailMessage mailObj = new MailMessage(); // Set the from and to address on the email mailObj.From = Request.Form("From"); mailObj.To = Request.Form("To"); mailObj.Subject = "Subject Of the Mail"; mailObj.Body = "Body of the Mail"; // Optional: HTML format for the email mailObj.BodyFormat = MailFormat.Html; // Optional: Encoding for the message mailObj.BodyEncoding = MailFormat.Base64; // Optional: Set the priority of the message to high mailObj.Priority = MailPriority.High; // Optional: Attach a file to the email. // Note here that we have created a MailAttachment object to // attach a file to the email mailObj.Attachments.Add(new MailAttachment("c:\\test.doc")); // Send the email using the SmtpMail object SmtpMail.Send(mailObj); } </SCRIPT> <asp:label ID="Headingmsg" Text="Enter Your Email Address:" RUNAT="server"/> <FORM METHOD="post" RUNAT="server"> Email Recipient: <INPUT TYPE="text" NAME="to"> <br> Email Sender: <INPUT TYPE="text" NAME="from"> <INPUT TYPE="submit" NAME="Submit" VALUE="Send Mail" RUNAT="server" OnServerClick="SendMail"> </FORM> <
|