ASP.NET中發送Email完整執行個體(轉)

來源:互聯網
上載者:User
asp.net 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>
</BODY>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.