創(chuàng)建基于ASP.NET的SMTP郵件服務(wù)
發(fā)表時(shí)間:2024-06-18 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]Asp.net在System.Web.Mail名稱空間中有一個(gè)發(fā)送email的內(nèi)建類,但這僅是cdosys的一個(gè)假象。開發(fā)者能使用一個(gè)替代的它smtp郵件服務(wù)。在這篇文章里面,我將會(huì)展示如何創(chuàng)建一個(gè)用于asp.net的功能齊全的smtp郵件服務(wù)! ∈紫,我們創(chuàng)建一個(gè)繼承命名空間System.Ne...
Asp.net在System.Web.Mail名稱空間中有一個(gè)發(fā)送email的內(nèi)建類,但這僅是cdosys的一個(gè)假象。開發(fā)者能使用一個(gè)替代的它smtp郵件服務(wù)。在這篇文章里面,我將會(huì)展示如何創(chuàng)建一個(gè)用于asp.net的功能齊全的smtp郵件服務(wù)。
首先,我們創(chuàng)建一個(gè)繼承命名空間System.Net.Sockets的TcpClient類的類。TcpClient類提供簡(jiǎn)單的方法用于連接,發(fā)送,接收網(wǎng)絡(luò)的數(shù)據(jù)流。GetStream方法用于創(chuàng)建一個(gè)網(wǎng)絡(luò)流(NetworkStream)。讀和寫網(wǎng)絡(luò)流(NetworkStream)的方法用于發(fā)送數(shù)據(jù)給遠(yuǎn)程主機(jī)和從遠(yuǎn)程主機(jī)接收網(wǎng)絡(luò)流。
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();
}
}
接下來,我們創(chuàng)建發(fā)送郵件出去的類,這個(gè)類有幾個(gè)屬性去設(shè)置關(guān)于被發(fā)送的郵件的一些信息。
public string MailServer = "127.0.0.1" ;
public string From = "" ;
public string To = "" ;
public string Body = "" ;
public string Subject = "" ;
和一個(gè)發(fā)送郵件的方法。這個(gè)方法將會(huì)用服務(wù)器名和它的端口去創(chuàng)建一個(gè)連接。指令可以被發(fā)送到遠(yuǎn)程主機(jī)。
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");
}
還有一個(gè)私有的送一個(gè)指令到本地服務(wù)器的方法。
private void SendCommandToServer(string cmd)
{
tcp.Send(cmd);
Response=tcp.Read();
System.Web.HttpContext.Current.Trace.Warn("Response",Response);
}
現(xiàn)在,這個(gè)類基本完成了。開發(fā)者還可以建立錯(cuò)誤回執(zhí)和釋放資源(用TcpClient類中的Close()方法)。同樣,這幾個(gè)屬性可以被擴(kuò)展到在郵件中包含更多的信息。
最后,我們可以在aspx文件中寫一些發(fā)送郵件的代碼。在這個(gè)例子中,"localhost"是郵件服務(wù)器。你可以根據(jù)情況改變這個(gè),或者可以看我的關(guān)于如何設(shè)置你的發(fā)送郵件的本地服務(wù)器的文章。
<%@ 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);
}
}
}