本文举例说明在ASP
NET中发送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>