SMTP Email Client
Summary: This is a webbased smtp email program. This program can be used to send a emailthrough a SMTP server. User can specify SMTP server IP, sender's mailaddress, recipient's email address and mail content. When the userclicks on "send mail" button , mail is forwarded to SMTPserver which in turn forwards the mail to recepient.
Author: Ravindra Sadaphule is currently working as a Programmer Analyst in Silverline Technologies, Mumbai . He has around 5.5 years of software development experience. His skill includes ASP, VB 6.0, Javascript , MTS. His current interests are .NET & C# . He can be reachedat ravi_sadaphule@hotmail.com .
Article Details: This smtp mail program is for .net beta 2 version . i have written a SMTP email component in C# and compiled it into a dll. Then this dll is accessed through aspx file which can be viewed in browser.
There is built in SMTP classalready available in System.Web.Mail namespace . However it is worthtaking a look at program below as it shows inner details of how tocommunicate with SMTP server. Once u grasp this technique, u canmodify it communicate any tcp socket server like ftp(for fileaccess),http(for web access),IRC (for chat) etc.
First i am giving source ofSMTP mail client.
/******************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 voidSendCommandToServer(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 stringGetServerResponse()
{
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)
{
returnReturnValue ;
}
else
{
System.Text.ASCIIEncoding en = new System.Text.ASCIIEncoding() ;
ReturnValue = en.GetString(ReadBuffer) ;
return ReturnValue;
}
 
; &
nbsp; }
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.StringMailData // This property contains email message
{
set
{
&n
bsp; _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 ;
}
}
publicSystem.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 successfulconnection
if (!blnConnect)
{
_errmsg = "ConnetionFailed…" ;
return;
}
&nbs
p; //read response of theserver
ServerResponse = tcp.GetServerResponse() ;
if(tcp.DoesStringContainSMTPCode(ServerResponse,"220"))
{
_ServerResponse += ServerResponse ;
}
else
{
_errmsg = "connectionfailed" + ServerResponse ;
return ;
}
System.String[] SendBuffer = new System.String[6] ;;
System.String[] ResponseCode = {"220","250","251","354","221"};
System.StringStrTemp = "" ;
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] ;
_errmsg += "<br>";
}
return ;
// end of debugging
*/
while(i <SendBuffer.Length)
{
tcp.SendCommandToServer(SendBuffer[i]) ;
&n
bsp; ServerResponse = tcp.GetServerResponse() ;
for(intj=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 ofModified SMTPMail.cs*********
The program is big but quitesimple . A class SMTPClient is derived from TcpClient . Hence allprotected and public properties of TcpClient are available toSMTPClient class.
Now go directly to SendMailclass. In this class i have added several private variables andcorresponding properties like smtpserverIP,MailFrom, MailTo etc.These properties are set
by smtpMail.aspx program whichis given below.
Property : Description
——————————————————————-
Identity: Thisis identity of sender e.g. ravindra
MailFrom: Sender's emailaddress e.g. ravindra@hotmail.com
MailTo: Recepient's emailaddress
Subject:&nb
sp; Topic of Mail
MailData: mail contents
SMTPServerIP: IPaddress of SMTP server
All these inputs are receivedfrom smtpMail.aspx file. In SendMail() method of SMTPMail class
an instance of SMTPClient classis created and connection to smtp server is attempted .
blnconnect flag indicates success/failure connection. If connection fails, then an error message is genearated and program Terminates.On successful connection(i.e. blnconnect is true) , SMTP server's response is analysed for success code "220".If SMTP responds with successcode "220", then tha action starts.
All SMTP commands are stored in array SendBuffer[] . All possible sucsess codes from SMTP server are stored in array ResponseCode[].So commandes from array SendBuffer[] are sent to smtp server one by one and for each command response from smtp server is analysed by comparing smtp server reply code with any of possible success codes in ResponseCode array. If the command executes successfully then only the next command is attempted. If any of commands is unsuccessful, then error message is generated and program terminates immediately. Compile this smtpmail.cs fileand put it into c:/inetpub/wwwroot/bin directory .
Web based front end is given below. SMTPMail.aspx is put into c:\inetpub\wwwroot directory which is configured as default website in IIS. This page can be viewed in browser by Typing url http://localhost/SMTPMAil.aspx. The page shows some texboxes tocapture input data like SMTP server ip ,sender's email , recepient;semail, subject, Mail Data etc. and a "Send Mail" button .
When user enters data into textboxes and clicks on "send Mail" button , a function btnSend_Click() is called. In this function , an instance SMTPMail() class is created. Please remember that this SMTPMail class() has been defined in SMTPMAIL.cs file .in btnSend_Click() we capureinputs from user and set various properties of smail(an instance ofSMTPMail) and call its sendmail() method.
If everythis is OK , then a message "Mail Has been sent successfully" is displayed at the top of the page. If any error occurs ., thenerror message is displayeed at the top.
***************** Web basedFront end (smtpMail.aspx)*******************
<%@ ImportNameSpace="Ravindra" %>
<Script language="C#"runat="server">
void btnSend_Click(ObjectSender,EventArgs e)
{
if(Page.IsPostBack)
{
SMTPMail smail = newSMTPMail() ; ;
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 Sentsuccessfully") ;
*/
Response.Write("<br>MailSent successfully<br>") ;
}
}
}
</script>
<html>
<head>
<title>SMTP E-Mail Exampleby Ravindra Sadaphule
</title>
</head>
<body>
<form name="frm1"runat="server">
<table width="100%">
<tr>
<td colspan="2"align="center"><h3>SMTP MAIL </h3></td>
</tr>
<tr>
<td width="30%"align="center" valign="middle" >SMTP SERVERIP:</td>
<td width="70%"align="left" valign="middle" >
<asp:textboxid="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:textboxid="txtSubject" runat="server" />e.g. MarriageProposal
</td>
</tr>
r /> <tr>
<td width="30%"align="center"valign="middle" >
Message:
</td>
<td width="70%"align="left" valign="middle" >
<asp:textboxid="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="SendMail" 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:
<ahref="mailto:ravi_sadaphule@hotmail.com"> RavindraSadaphule</a>
</td>
</tr>
</table>
</form>
</body>
</html>












No comments yet... Be the first to leave a reply!