成人午夜激情影院,小视频免费在线观看,国产精品夜夜嗨,欧美日韩精品一区二区在线播放

當前位置:首頁>>軟件教程>>新聞內容  
ASP.NET中發送Email完整實例
作者:青蘋果工作室 發布時間:2003-11-28 13:21:48 | 【字體:
本文舉例說明在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>


文章來源:賽迪網
 放生
 愚愛
 夠愛
 觸電
 白狐
 葬愛
 光榮
 畫心
 火花
 稻香
 小酒窩
 下雨天
 右手邊
 安靜了
 魔杰座
 你不像她
 邊做邊愛
 擦肩而過
 我的答鈴
 懷念過去
 等一分鐘
 放手去愛
 冰河時代
 你的承諾
 自由飛翔
 原諒我一次
 吻的太逼真
 左眼皮跳跳
 做你的愛人
 一定要愛你
 飛向別人的床
 愛上別人的人
 感動天感動地
 心在跳情在燒
 玫瑰花的葬禮
 有沒有人告訴你
 即使知道要見面
 愛上你是一個錯
 最后一次的溫柔
 愛上你是我的錯
 怎么會狠心傷害我
 不是因為寂寞才想
 親愛的那不是愛情
 難道愛一個人有錯
 寂寞的時候說愛我
主站蜘蛛池模板: 海宁市| 阜南县| 华宁县| 永顺县| 大安市| 安阳县| 香格里拉县| 正安县| 克山县| 新营市| 苍梧县| 图们市| 岢岚县| 西吉县| 高唐县| 若尔盖县| 临江市| 太仓市| 巨野县| 石楼县| 都江堰市| 苏尼特右旗| 噶尔县| 淅川县| 祁阳县| 安远县| 西安市| 东方市| 凯里市| 景德镇市| 夏河县| 宁远县| 平果县| 论坛| 凤凰县| 石城县| 清苑县| 察雅县| 西青区| 固原市| 十堰市|