首先,我们创建一个继承命名空间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 TO: " + To + "\r\n"); string StrHeaders = ""; StrHeaders += "From: " + From + "\r\n"; StrHeaders += "To: " + 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> |
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 + "\r\n"); SendCommandToServer("RCPT TO: " + To + "\r\n"); string StrHeaders = ""; StrHeaders += "From: " + From + "\r\n"; StrHeaders += "To: " + 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); } } } |
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/