基于 Web 的 SMTP 客户端程序






4.73/5 (10投票s)
2001 年 9 月 14 日
3分钟阅读

313267

2339
这是一个基于Web的SMTP客户端程序,可用于通过SMTP服务器发送电子邮件。
引言
这是一个基于Web的SMTP电子邮件程序。该程序可用于通过SMTP服务器发送电子邮件。用户可以指定SMTP服务器的IP地址、发件人的邮件地址、收件人的电子邮件地址和邮件内容。当用户点击“发送邮件”按钮时,邮件将被转发到SMTP服务器,然后SMTP服务器将邮件转发给收件人。
详细说明
这个SMTP邮件程序适用于.NET beta 2。我已经用C#编写了一个SMTP电子邮件组件,并将其编译成一个dll文件。然后,可以通过一个aspx文件访问该dll文件,该文件可以在浏览器中查看。
注意:System.Web.Mail命名空间中已经有一个内置的SMTP类。但是,值得看看下面的程序,因为它展示了如何与SMTP服务器通信的内部细节。一旦你掌握了这项技术,你就可以修改它来与任何TCP套接字服务器通信,比如FTP(用于文件访问)、HTTP(用于Web访问)、IRC(用于聊天)等。
首先,我将提供SMTP邮件客户端的源代码。
/******************SMTPMail.cs ************ Name : SMTPMail.cs Description : SMTP email client . Version : 1.0 Author : Ravindra Sadaphule Email : ravi_sadaphule@hotmail.com Last Modified: 13th September , 2001 To compile: csc /t:library /out:SMTPMail.dll SMTPMail.cs Note : save this file in c:/inetpub/wwwroot/bin directory */ namespace Ravindra { /* SMTPClient is a class which inherits from TcpClient . Hence it can access all protected and public membert of TcpClient */ public class SMTPClient : System.Net.Sockets.TcpClient { public bool isConnected() { return Active ; } public void SendCommandToServer(string Command) { System.Net.Sockets.NetworkStream ns = this.GetStream() ; byte[] WriteBuffer ; WriteBuffer = new byte[1024] ; System.Text.ASCIIEncoding en = new System.Text.ASCIIEncoding() ; WriteBuffer = en.GetBytes(Command) ; ns.Write(WriteBuffer,0,WriteBuffer.Length); return ; } public string GetServerResponse() { int StreamSize ; string ReturnValue = "" ; byte[] ReadBuffer ; System.Net.Sockets.NetworkStream ns = this.GetStream() ; ReadBuffer = new byte[1024] ; StreamSize = ns.Read(ReadBuffer,0,ReadBuffer.Length); if (StreamSize==0) { return ReturnValue ; } else { System.Text.ASCIIEncoding en = new System.Text.ASCIIEncoding() ; ReturnValue = en.GetString(ReadBuffer) ; return ReturnValue; } } public bool DoesStringContainSMTPCode(System.String s,System.String SMTPCode) { return (s.IndexOf(SMTPCode,0,10)==-1)?false:true ; } }// end of class SMTPClient public class SMTPMail { private System.String _SMTPServerIP = "127.0.0.1" ; private System.String _errmsg = "" ; private System.String _ServerResponse = "" ; private System.String _Identity = "expowin2k" ; private System.String _MailFrom = "" ; private System.String _MailTo = "" ; private System.String _MailData = "" ; private System.String _Subject = "" ; public System.String Subject // This property contains Subject of email { set { _Subject = value ; } } public System.String Identity // This property contains Sender's Identity { set { _Identity = value ; } } public System.String MailFrom // This property contains sender's email address { set { _MailFrom = value ; } } public System.String MailTo // This property contains recepient's email address { set { _MailTo = value ; } } public System.String MailData // This property contains email message { set { _MailData = value ; } } public System.String SMTPServerIP // This property contains of SMTP server IP { get { return _SMTPServerIP ; } set { _SMTPServerIP = value ; } } public System.String ErrorMessage // This property contais error message { get { return _errmsg ; } } public System.String ServerResponse // This property contains server response { get { return _ServerResponse ; } } public void SendMail() { try { System.String ServerResponse ; SMTPClient tcp = new SMTPClient() ; tcp.Connect(SMTPServerIP,25) ; // first connect to smtp server bool blnConnect = tcp.isConnected(); // test for successful connection if (!blnConnect) { _errmsg = "Connetion Failed..." ; return; } //read response of the server ServerResponse = tcp.GetServerResponse() ; if (tcp.DoesStringContainSMTPCode(ServerResponse,"220")) { _ServerResponse += ServerResponse ; } else { _errmsg = "connection failed" + ServerResponse ; return ; } System.String[] SendBuffer = new System.String[6] ;; System.String[] ResponseCode = {"220","250","251","354","221"}; System.String StrTemp = "" ; StrTemp = System.String.Concat("HELO ",_Identity); StrTemp = System.String.Concat(StrTemp,"\r\n"); SendBuffer[0] = StrTemp ; StrTemp = "" ; StrTemp = System.String.Concat("MAIL FROM: ",_MailFrom); StrTemp = System.String.Concat(StrTemp,"\r\n"); SendBuffer[1] = StrTemp ; StrTemp = "" ; StrTemp = System.String.Concat("RCPT TO: ",_MailTo); StrTemp = System.String.Concat(StrTemp,"\r\n"); SendBuffer[2] = StrTemp ; StrTemp = "" ; StrTemp = System.String.Concat("DATA","\r\n"); SendBuffer[3] = StrTemp ; StrTemp = "" ; StrTemp = System.String.Concat("From: ",_MailFrom ); StrTemp = System.String.Concat(StrTemp,"\r\n" ); StrTemp = System.String.Concat(StrTemp,"To: " ); StrTemp = System.String.Concat(StrTemp,_MailTo); StrTemp = System.String.Concat(StrTemp,"\r\n" ); StrTemp = System.String.Concat(StrTemp,"Subject: " ); StrTemp = System.String.Concat(StrTemp,_Subject); StrTemp = System.String.Concat(StrTemp,"\r\n" ); StrTemp = System.String.Concat(StrTemp,_MailData); StrTemp = System.String.Concat(StrTemp,"\r\n.\r\n"); SendBuffer[4] = StrTemp ; StrTemp = "" ; StrTemp = System.String.Concat(StrTemp,"QUIT\r\n"); SendBuffer[5] = StrTemp ; int i = 0 ; /* for debugging _errmsg = "" ; for(i=0;i<5;i++) { _errmsg += SendBuffer[i] ; += "<br>" ; } return ; // end of debugging */ while(i < SendBuffer.Length) { tcp.SendCommandToServer(SendBuffer[i]) ; ServerResponse = tcp.GetServerResponse() ; for(int j=0;j<ResponseCode.Length;j++) { if (tcp.DoesStringContainSMTPCode(ServerResponse,ResponseCode[j])) { _ServerResponse += ServerResponse; _ServerResponse += "<br>"; break; } else { if(j==ResponseCode.Length-1) { _errmsg += ServerResponse ; _errmsg += SendBuffer[i] ; return ; } } } i++ ; } // end of while loop ; }// end of try block catch(System.Net.Sockets.SocketException se) { _errmsg += se.Message + " " + se.StackTrace; } catch(System.Exception e) { _errmsg += e.Message + " " + e.StackTrace; } } // end of SendMail method } // end of class client } // end of namespace Ravindra /*****************end of Modified SMTPMail.cs*********/
程序很大,但很简单。一个名为 SMTPClient
的类是从 TcpClient
派生的。因此, TcpClient
的所有受保护和公共属性都可用于 SMTPClient
类。
现在直接转到 SendMail
类。在这个类中,我添加了几个私有变量和相应的属性,比如smtpserverIP
、MailFrom
、MailTo
等。这些属性由下面的smtpMail.aspx程序设置。
属性 |
描述 |
身份: |
这是发件人的身份,例如ravindra |
MailFrom: |
发件人的电子邮件地址,例如ravindra@hotmail.com |
MailTo: |
收件人的电子邮件地址 |
主题: |
邮件的主题 |
MailData: |
邮件内容 |
SMTPServerIP: |
SMTP服务器的IP地址 |
所有这些输入都来自 smtpMail.aspx 文件。在SMTPMail
类的SendMail()
方法中,创建了SMTPClient
类的一个实例,并尝试连接到smtp服务器。blnconnect 标志指示连接成功/失败。如果连接失败,则会生成一条错误消息,并且程序终止。
如果连接成功(即 blnconnect 为true),则分析SMTP服务器的响应以获取成功代码“220”。如果SMTP响应成功代码“220”,则开始执行操作。
所有SMTP命令都存储在数组 SendBuffer[]
中。SMTP服务器的所有可能的成功代码都存储在一个数组ResponseCode[]
中。因此,来自数组 SendBuffer[]
的命令被逐个发送到SMTP服务器,对于每个命令,通过将SMTP服务器的回复代码与ResponseCode
数组中的任何可能的成功代码进行比较来分析SMTP服务器的响应。如果命令成功执行,则仅尝试下一个命令。如果任何命令不成功,则会生成一条错误消息,并且程序立即终止。
编译此smtpmail.cs文件并将其放入c:/inetpub/wwwroot/bin目录。
基于Web的前端如下所示。 SMTPMail.aspx文件放在c:\inetpub\wwwroot目录中,该目录被配置为IIS中的默认网站。可以通过在浏览器中输入url https:///SMTPMAil.aspx来查看此页面。 页面显示了一些文本框,用于捕获输入数据,例如SMTP服务器ip、发件人的电子邮件、收件人的电子邮件、主题、邮件数据等,以及一个“发送邮件”按钮。
当用户在文本框中输入数据并单击“发送邮件”按钮时,将调用函数 btnSend_Click()
。在这个函数中,创建了SMTPMail
类的一个实例。请记住,这个 SMTPMail
类已经在SMTPMAIL.cs文件中定义。在btnSend_Click()
中,我们捕获用户输入并设置smail(SMTPMail
的一个实例)的各种属性,并调用其 sendmail()
方法。
如果一切正常,则在页面顶部显示消息“邮件已成功发送”。如果发生任何错误,则在顶部显示错误消息。
<%@ Import NameSpace="Ravindra" %> <script language="C#" runat="server"> void btnSend_Click(Object Sender,EventArgs e) { if(Page.IsPostBack) { SMTPMail smail = new SMTPMail() ; ; smail.MailFrom = txtfrom.Text ; smail.MailTo = txtto.Text ; smail.MailData = txtmessage.Text ; smail.Subject = txtSubject.Text ; smail.SMTPServerIP = txtSMTPServerIP.Text ; smail.SendMail(); if (smail.ErrorMessage!="") { Response.Write("Error: " + smail.ErrorMessage) ; } else { /* for debugging only. Response.Write(smail.ServerResponse + "Mail Sent successfully") ; */ Response.Write("<br>Mail Sent successfully<br>") ; } } } </script> <html> <head> <title>SMTP E-Mail Example by Ravindra Sadaphule </title> </head> <body> <form name="frm1" runat="server" id="Form1"> <table width="100%"> <tr> <td colspan="2" align="center"> <h3> SMTP MAIL </h3> </td> </tr> <tr> <td width="30%" align="center" valign="middle"> SMTP SERVER IP: </td> <td width="70%" align="left" valign="middle"> <asp:textbox id="txtSMTPServerIP" runat="server" /> e.g. 127.0.0.1 </td> </tr> <tr> <td width="30%" align="center" valign="middle"> From: </td> <td width="70%" align="left" valign="middle"> <asp:textbox id="txtfrom" runat="server" /> e.g. ravi_sadaphule@hotmail.com </td> </tr> <tr> <td width="30%" align="center" valign="middle"> To: </td> <td width="70%" align="left" valign="middle"> <asp:textbox id="txtto" runat="server" /> e.g. tarunt@bharat.com </td> </tr> <tr> <td width="30%" align="center" valign="middle"> Sub: </td> <td width="50%" align="left" valign="middle"> <asp:textbox id="txtSubject" runat="server" /> e.g. Marriage Proposal </td> </tr> <tr> <td width="30%" align="center" valign="middle"> Message: </td> <td width="70%" align="left" valign="middle"> <asp:textbox id="txtmessage" textmode="MultiLine" rows="5" runat="server" /> </td> </tr> <tr> <td width="30%" align="center" valign="middle"> </td> <td width="70%" align="left" valign="middle"> <asp:button text="Send Mail" id="btnSend" onclick="btnSend_Click" runat="server" /> </td> </tr> <tr> <td width="30%" align="center" valign="middle"> </td> <td width="70%" align="left" valign="middle"> Developer: <a href="mailto:ravi_sadaphule@hotmail.com">Ravindra Sadaphule</a> </td> </tr> </table> </form> </body> </html>