在ASP.NET中使用SMTP服务

类别:Asp 点击:0 评论:0 推荐:
SMTP Service(Simple Mail Transport Protocol简单邮件传输协议)是Microsoft Windows 2000的组件,它能执行发送和获取电子邮件的基本工作,但不支持多个邮箱。此服务使用两个主要目录处理电子邮件,它们是Pickup和Drop,这两个目录位于InetPub\MailRoot目录下。服务不断地检查Pickup目录,当找到电子邮件消息时,就尝试发送这个电子邮件。若服务无法立即投递这个消息,就将它保存于Queue目录中,同时服务继续尝试投递消息。如果电子邮件消息无法投递,而且返回给发送者,消息就转移到Badmail目录。
      在.NET中可以通过SmtpMail类进行发送电子邮件消息.在进行程序调试前应确保SMTP服务正常运行,并且系统装有Microsoft .NET Framework以便编译ASPX文件,如果发现不能正常编译,记住要检查相应的服务是否已启动。下面以例子来描述SMTP的般用法。本人使用的编程环境是:一机作邮件服务器,运行Exchange Server2003,另一机运行Windows 2000 Server作开发端.例子中进行了三种常用的邮件内容:文本,附件,HTML文本.同时也进行了提取信息的方法演示,具体代码如下:

<%@ page validateRequest=false %>
<%@ Import Namespace="System.Web.Mail"%>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
sub Page_Load
if not IsPostBack then
dim conn as Sqlconnection
dim cmd as SqlCommand
dim dtrMailto as SqlDataReader
conn = new SqlConnection("server=localhost;UID=sa;PWD=sa;DataBase=MailTest")
conn.open()
cmd = new SqlCommand("select email from MailTable",conn)
dtrMailto = cmd.ExecuteReader()
mailtoList.DataSource = dtrMailto
mailtoList.DataTextField ="email"
mailtoList.DataBind()
dtrMailto.close()
conn.close()
end if
end sub

sub Button_click(s as Object,e as EventArgs)
SmtpMail.Send(mailfrom.text,mailto.text,txtSubject.text,txtBody.text)
end sub

sub Button_Attachment_click(s as Object,e as EventArgs)
dim objMailMessage as MailMessage
dim objMailAttachment as MailAttachment
objMailAttachment = new MailAttachment(filename.PostedFile.FileName)
objMailMessage = new MailMessage
objMailMessage.From = mailfrom.text
objMailMessage.To = mailto.text
objMailMessage.Subject=txtSubject.text
objMailMessage.Body=txtBody.text
objMailMessage.Attachments.add(objMailAttachment)
SmtpMail.send(objMailMessage)
end sub

sub Button_html_click(s as Object,e as EventArgs)
dim objMailMessage as MailMessage
dim strContent as String
strContent = txtHtmlBody.text
objMailMessage = new MailMessage
objMailMessage.From = mailfrom.text
objMailMessage.To = mailto.text
objMailMessage.Subject=txtSubject.text
objMailMessage.Body=strContent
objMailMessage.BodyFormat = MailFormat.HTML
SmtpMail.send(objMailMessage)
end sub

sub Button_DB_click(s as Object,e as EventArgs)
dim objMailMessage as MailMessage
objMailMessage = new MailMessage
objMailMessage.From = mailfrom.text
objMailMessage.To = mailtoList.SelectedItem.text
objMailMessage.Subject=txtSubject.text
objMailMessage.Body=txtBody.text
SmtpMail.send(objMailMessage)
end sub

</script>
<html>
<body>
<h3><font color=green>发送邮件</font></h3>
<form runat="server">
<b>发件人:</b>
<asp:TextBox id="mailfrom" Columns="30" runat="server"/>
<br>
<b>收件人</b>
<asp:TextBox id="mailto" columns="30" runat="server"/>
<br><b>标&nbsp;&nbsp;&nbsp;&nbsp;题:</b>
<asp:TextBox id="txtSubject" columns="30" runat="server"/>
<br><b>内&nbsp;&nbsp;&nbsp;&nbsp;容:</b>
<asp:TextBox id="txtBody" TextMode="MultiLine" columns="30" runat="server"/>
<br>
<asp:Button text="发送" OnClick="Button_click" runat="server"/>
<br>
<b>收件人列表:</b><asp:DropDownList id="mailtoList" runat="server"/>
<br>
<asp:Button text="从数据库得收件人的发送" OnClick="Button_DB_click" runat="server"/>
<br><br>
<b>附&nbsp;&nbsp;&nbsp;&nbsp;件:</b><input id="filename" type="File" runat="server"/>
<br>
<asp:Button text="带附件的发送" Onclick="Button_Attachment_click" runat="server"/>
<br><br>
<b>HTML内容:</b><asp:TextBox id="txtHtmlBody" TextMode="MultiLine" columns="30" runat="server"/>
<br>
<asp:Button text="发送HTML" OnClick="Button_html_click" runat="server"/>
</form>
</body>
</html>
运行结果:


    此处没有指定SmtpMail.SmtpServer属性,所以使用的SMTP服务器就是本地的默认SMTP服务器,如果要使用其它的SMTP服务器可通过此属性进行设置.

本文地址:http://com.8s8s.com/it/it7916.htm