职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 1303|回复: 0

ASP.NET中发送Email完整实例

[复制链接]
joe 发表于 2006-12-18 22:25 | 显示全部楼层 |阅读模式
本文举例说明在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的完整例子:
[code]
<%@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>
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-5-12 10:23 , Processed in 0.112372 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表