初秋的风似一支七彩的画笔,走到哪里,哪里就披上秋的盛装,五光十色的鲜花和着七彩的蝴蝶,迎着丰收的景象。五彩缤纷的鲜花撒满大地,丰收的果园一派欣欣向荣的景象,金黄色的稻谷堆满仓,大地一片喜气洋洋。
首先,我们创建一个继承命名空间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);
}
}
}
到此这篇关于创建基于ASP.NET的SMTP邮件服务的具体方法就介绍到这了。心灵是有品级的,它的品级决定人格、 品格,决定人一生的命运。最高境界是敬畏之心,如同信仰和宗教,虔诚如山不可动摇。第二境界是慈悲之心,不以善小而不为。第三境界是感恩之心,不爱其亲而爱他人者悖德;不敬其亲而敬他人者悖礼。第四境界是宽容之心,对人宽容,对己宽容,有容乃大!更多相关创建基于ASP.NET的SMTP邮件服务的具体方法内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!