ASP.NET中發(fā)送Email完整實例(轉(zhuǎn))
2024-07-10 12:58:14
供稿:網(wǎng)友
asp.net中發(fā)送email完整實例
本文舉例說明在asp.net中發(fā)送email的眾多可能性,內(nèi)容覆蓋了諸如email格式、優(yōu)先權(quán)、附件及email編碼等方面。
asp.net被賦予了一個發(fā)送email的新對象,名為smtpmail。使用smtpmail對象從asp.net頁面中發(fā)送email時,可以遵循以下簡單步驟:
▲包含與郵件有關(guān)類所需要的名稱空間;
▲例示一個信息對象,設(shè)置屬性;
▲使用smtpmail對象實例的send方法發(fā)送郵件。
現(xiàn)在我們就來一步一步地研究從一個asp.net頁面發(fā)送email的過程。我們使用了vb來說明這個例子,最后將包含vb和c#的完整代碼。
第一步:包含名稱空間
在asp.net 頁面中引入system.web.util 名稱空間,這個名稱空間中包括了發(fā)送一個email所必須的所有對象。這些對象是:
smtpmail:代表郵件系統(tǒng),用于發(fā)送email。
mailmessage:代表一個信息,其屬性包括發(fā)件人地址、收件人地址等。
mailformat:代表信息的格式:html、文本等。
mailattachment:代表一個email附件。
mailencoding enum:代表base64 或uuencode的任何編碼。取值范圍:base64、uuencode
mailpriority enum:用來為信息設(shè)置優(yōu)先權(quán)。值為:高、低、一般。
<% @import namespace = "system.web.util" %>
第二步:例示 mailmessage 對象
使用以下語句來例示mailmessage對象:
dim mailobj as new mailmessage
用mailmessage對象的屬性來準(zhǔn)備郵件。mailmessage對象有下列屬性:
from:發(fā)件人的email地址
to:收件人的email地址
subject:email的主題
body:email的主體
cc:email抄送的收件人列表
bcc:email暗送的收件人列表
priority:信息的優(yōu)先權(quán):高、低或一般
bodyencoding:信息體的編碼,如果有的話,就是base64或uuencode
bodyformat:信息的格式:html 或text
attachments:附加到email 的mailattachment對象列表,主要就是對這個對象集合的一個引用
下面這段代碼示范了使用mailmessage 對象屬性的方法,它們代表了將在本例中創(chuàng)建的一個信息,這個信息要用smtpmail對象來發(fā)送。在例子中,mailobj引用了信息對象的例示:
mailobj.from = "[email protected]"
mailobj.to = request.form ("to")
mailobj.subject = "subject of the mail"
mailobj.body = "message of the mail"
第三步:發(fā)送email
這時,我們就可以使用smtpmail 對象的send方法來發(fā)送郵件了:
smtpmail.send(mailobj)
完整實例
最后,我們把以上解釋的屬性結(jié)合在一個完整的例子中。為了說明用asp.net 發(fā)送一個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(發(fā)件人)和 to(收件人)的email地址是從相應(yīng)的文本框中收集的,點擊“send mail”(發(fā)送郵件)按鈕時,郵件就被發(fā)送出去。當(dāng)“send mail”(發(fā)送郵件)按鈕被點擊時,表單回遞到它自己,在服務(wù)器上“sendmail”(發(fā)送郵件)程序被觸發(fā),郵件被發(fā)送。下面是使用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>