熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ASP.NET中發送Email完整實例

2013-11-13 12:11:58  來源: .NET編程 
 本文舉例說明在ASPNET中發送Email的眾多可能性內容覆蓋了諸如Email格式優先權附件及Email編碼等方面

  ASPNET被賦予了一個發送Email的新對象名為SmtpMail使用SmtpMail對象從ASPNET頁面中發送Email時可以遵循以下簡單步驟

  ▲包含與郵件有關類所需要的名稱空間
  ▲例示一個信息對象設置屬性
  ▲使用SmtpMail對象實例的send方法發送郵件

  現在我們就來一步一步地研究從一個ASPNET頁面發送Email的過程我們使用了VB來說明這個例子最後將包含VB和C#的完整代碼

  第一步包含名稱空間

  在ASPNET 頁面中引入SystemWebUtil 名稱空間這個名稱空間中包括了發送一個email所必須的所有對象這些對象是

SmtpMail代表郵件系統用於發送email
MailMessage代表一個信息其屬性包括發件人地址收件人地址等
MailFormat代表信息的格式HTML文本等
MailAttachment代表一個email附件
MailEncoding enum代表Base 或Uuencode的任何編碼取值范圍BaseUUencode
MailPriority enum用來為信息設置優先權值為一般
<% @Import Namespace = SystemWebUtil %>

  二步例示 MailMessage 對象

  使用以下語句來例示MailMessage對象

Dim mailObj AS new MailMessage

  用MailMessage對象的屬性來准備郵件MailMessage對象有下列屬性

From發件人的Email地址
To收件人的Email地址
Subjectemail的主題
Bodyemail的主體
CCemail抄送的收件人列表
BCCemail暗送的收件人列表
Priority信息的優先權低或一般
BodyEncoding信息體的編碼如果有的話就是Base或UUencode
BodyFormat信息的格式Html 或text
Attachments附加到email 的MailAttachment對象列表主要就是對這個對象集合的一個引用

  下面這段代碼示范了使用MailMessage 對象屬性的方法它們代表了將在本例中創建的一個信息這個信息要用SmtpMail對象來發送在例子中mailObj引用了信息對象的例示

mailObjFrom = abc@mydomaincom
mailObjTo = RequestForm (to)
mailObjSubject = subject of the mail
mailObjBody = Message of the mail

  第三步發送Email

  這時我們就可以使用SmtpMail 對象的Send方法來發送郵件了

SmtpMailSend(mailObj)

  第四步完整實例

  最後我們把以上解釋的屬性結合在一個完整的例子中為了說明用ASPNET 發送一個email 的全部可能性我們還包含了一些小技巧下面是使用VBNET的完整例子

<%@page language=VB %>
<%@Import Namespace=SystemWebUtil %>
<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
mailObjFrom = RequestForm(From)
mailObjTo = RequestForm(To)
mailObjSubject = Subject Of the Mail
mailObjBody = Body of the Mail
Optional: HTML format for the email
mailObjBodyFormat = MailFormatHtml
Optional: Encoding for the message
mailObjBodyEncoding = MailFormatBase
Optional: Set the priority of the message to high
mailObjPriority = MailPriorityHigh
Optional: Attach a file to the email
Note here that we have created a MailAttachment object to
attach a file to the email
mailObjAttachmentsAdd(new MailAttachment(c:\testdoc))
Send the email using the SmtpMail object
SmtpMailSend(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=SystemWebUtil %>
<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
mailObjFrom = RequestForm(From);
mailObjTo = RequestForm(To);
mailObjSubject = Subject Of the Mail;
mailObjBody = Body of the Mail;
// Optional: HTML format for the email
mailObjBodyFormat = MailFormatHtml;
// Optional: Encoding for the message
mailObjBodyEncoding = MailFormatBase;
// Optional: Set the priority of the message to high
mailObjPriority = MailPriorityHigh;
// Optional: Attach a file to the email
// Note here that we have created a MailAttachment object to
// attach a file to the email
mailObjAttachmentsAdd(new MailAttachment(c:\\testdoc));
// Send the email using the SmtpMail object
SmtpMailSend(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>


From:http://tw.wingwit.com/Article/program/net/201311/15379.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.