創建用於asp.net的smtp郵件服務
Asp.net在System.Web.Mail命名空間中有一個發送email的內建類,這僅是cdosys的一個包裝。開發者可能希望使用一個替代的它smtp郵件服務。在這篇文章裡面,我將會展示如何創建一個用於asp.net的功能齊全的smtp郵件服務。(僅僅是70行的c#語句)
首選,我們創建一個繼承命名空間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);
}
}
}
本文地址:http://com.8s8s.com/it/it45286.htm