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 + "
");
SendCommandToServer("RCPT TO: " + To + "
");
string StrHeaders = "";
StrHeaders += "From: " + From + "
";
StrHeaders += "To: " + To + "
";
StrHeaders += "Subject: " + Subject + "
";
StrHeaders += "Content-type: text/plain; charset="ISO-8859-1"" + "
";
SendCommandToServer("DATA
" + StrHeaders);
SendCommandToServer(Body + "
.
");
SendCommandToServer("QUIT
");
}
还有一个私有的送一个指令到本地服务器的方法。
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>
下面是完整的代码。
using System.IO;
using System.Net.Sockets;
namespace MyComponents
{
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 class Mail
{
public string MailServer = "127.0.0.1" ;
public string From = "" ;
public string To = "" ;
public string Body = "" ;
public string Subject = "" ;
private ClientConnection tcp=null;
private string Response="";
public void Send()
{
tcp = new ClientConnection();
tcp.Connect(MailServer,25);
tcp.Initialise();
SendCommandToServer("HELO " + System.Net.Dns.GetHostName());
SendCommandToServer("MAIL FROM: " + From + "
");
SendCommandToServer("RCPT TO: " + To + "
");
string StrHeaders = "";
StrHeaders += "From: " + From + "
";
StrHeaders += "To: " + To + "
";
StrHeaders += "Subject: " + Subject + "
";
StrHeaders += "Content-type: text/plain; charset="ISO-8859-1"" + "
";
SendCommandToServer("DATA
" + StrHeaders);
SendCommandToServer(Body + "
.
");
SendCommandToServer("QUIT
");
}
private void SendCommandToServer(string cmd)
{
tcp.Send(cmd);
Response=tcp.Read();
System.Web.HttpContext.Current.Trace.Warn("Response",Response);
}
}
}