ASP

本类阅读TOP10

·asp常用数据库连接方法和技巧
·无组件生成BMP验证码
·一些常用的辅助代码 (网络收藏)
·JavaScript实现的数据表格:冻结列、调整列宽和客户端排序
·VisualStudio.NET_2003及其 MSDN 下载地址
·ASP模拟MVC模式编程
·图片以二进制流输出到网页
·MD5加密算法 ASP版
·ASP.NET编程中的十大技巧
·改进 ASP 的字符串处理性能

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
在ASP.NET中使用SMTP服务

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

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服务器可通过此属性进行设置.




相关文章

相关软件