asp.net
ASP.NET中发送Email完整实例
本文举例说明在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>