国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

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>
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 鹤庆县| 南宁市| 乌什县| 当涂县| 泸西县| 北安市| 波密县| 通河县| 潮安县| 涿州市| 云南省| 射洪县| 连州市| 乌兰浩特市| 正宁县| 营口市| 崇明县| 夏邑县| 安仁县| 胶南市| 两当县| 平定县| 萨迦县| 黄冈市| 曲松县| 栾城县| 东山县| 即墨市| 永福县| 揭阳市| 绥中县| 榆树市| 抚宁县| 延庆县| 会理县| 文安县| 延吉市| 宜丰县| 金阳县| 德江县| 云霄县|