当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 创建基于ASP.NET的SMTP邮件系统

ASP.NET
如何在ASP.NET中使用SmtpMail发送邮件
在VB.NET中利用Split和Replace函数计算字数
Attribute应用:简化ANF自定义控件初始化过程
ASP.NET 2.0移动开发入门之使用样式
ASP.NET 2.0中使用OWC生成图表
ASP.NET 2.0中控件的简单异步回调
一个无法捕获ADO.NET Dataset的内存错误
深入解读ADO.NET2.0的十大最新特性
.Net平台下的分布式缓存设计
ASP.NET全局异常处理浅析
ASP.NET 2.0中文验证码的实现
浅析.NET平台编程语言的未来走向
.net 框架程序设计收藏
使用ASP.NET MVC Futures 中的异步Action
详解.NET中的XmlReader与XmlWriter
关于.NET中的Server push技术
asp.net页面执行机制
对比JSP和ASP.NET的存储过程
.NET 4.0不会包含System.Shell.CommandLine
ASP.NET十个有效性能优化的方法

创建基于ASP.NET的SMTP邮件系统


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-23   浏览: 162 ::
收藏到网摘: n/a

您可能习惯了在ASP程序中使用JMAIL组件收发邮件,Asp.net在System.Web.Mail名称空间中有一个发送email的内建类,但这仅是cdosys的一个假象。开发者能使用一个替代的它smtp邮件服务。在这篇文章里面,我将会展示如何创建一个用于asp.net的功能齐全的smtp邮件服务。

首先,我们创建一个继承命名空间System.Net.Sockets的TcpClient类的类。TcpClient类提供简单的方法用于连接,发送,接收网络的数据流。GetStream方法用于创建一个网络流(NetworkStream)。读和写网络流(NetworkStream)的方法用于发送数据给远程主机和从远程主机接收网络流。

以下为引用的内容:
public class ClientConnection : TcpClient
{
private NetworkStream _NetworkStream = null;
private StreamReader _StreamReader = null;
private StreamWriter _StreamWriter = null;
public void Initialise()
{
_NetworkStream = this.GetStream();
_StreamReader = new StreamReader(_NetworkStream,
System.Text.Encoding.Default, false, this.ReceiveBufferSize);
_StreamWriter = new StreamWriter(_NetworkStream,
System.Text.Encoding.Default, this.SendBufferSize);
}
public void Send(string s)
{
_StreamWriter.WriteLine(s);
_StreamWriter.Flush();
}
public string Read()
{
return _StreamReader.ReadLine();
}

接下来,我们创建发送邮件出去的类,这个类有几个属性去设置关于被发送的邮件的一些信息。

以下为引用的内容:
public string MailServer = "127.0.0.1" ;
public string From = "" ;
public string To = "" ;
public string Body = "" ;
public string Subject = "" ; 

和一个发送邮件的方法。这个方法将会用服务器名和它的端口去创建一个连接。指令可以被发送到远程主机。

以下为引用的内容:
public void Send()
{
tcp = new ClientConnection();
tcp.Connect(MailServer,25);
tcp.Initialise();
SendCommandToServer("HELO " + System.Net.Dns.GetHostName());
SendCommandToServer("MAIL FROM: " + From + "\r\n");
SendCommandToServer("RCPT T " + To + "\r\n");
string StrHeaders = "";
StrHeaders += "From: " + From + "\r\n";
StrHeaders += "T " + To + "\r\n";
StrHeaders += "Subject: " + Subject + "\r\n";
StrHeaders += "Content-type: text/plain; charset=\"ISO-8859-1\"" + "\r\n";
SendCommandToServer("DATA\r\n" + StrHeaders);
SendCommandToServer(Body + "\r\n.\r\n");
SendCommandToServer("QUIT\r\n");
}

还有一个私有的送一个指令到本地服务器的方法。

以下为引用的内容:
private void SendCommandToServer(string cmd)
{
tcp.Send(cmd);
Response=tcp.Read();
System.Web.HttpContext.Current.Trace.Warn("Response",Response);
}

现在,这个类基本完成了。开发者还可以建立错误回执和释放资源(用TcpClient类中的Close()方法)。同样,这几个属性可以被扩展到在邮件中包含更多的信息。

最后,我们可以在aspx文件中写一些发送邮件的代码。在这个例子中,"localhost"是邮件服务器。你可以根据情况改变这个,或者可以看我的关于如何设置你的发送邮件的本地服务器的文章。

以下为引用的内容:
<%@ Import Namespace="MyComponents" %>
<script runat="server">
protected void Page_Load(Object Src, EventArgs E)
{
if (IsPostBack)
{
Mail Mailer=new Mail();
Mailer.From=From.Text;
Mailer.To=To.Text;
Mailer.Subject=Subject.Text;
Mailer.Body=Body.Text;
Mailer.MailServer="localhost";
Mailer.Send();
}
}
</script>

技术交流 永无止境