I have written a web based front end for SMTP email program. This SMTP mail program is for .net beta 2 version and I have compiled it into a DLL. Then this DLL is accessed through aspx file which can be viewed in browser.
There is built in SMTP class already available in System.Web.Mail namespace . However it is worth taking a look at program below as it shows inner details of how to communicate with SMTP server. Once u grasp this technique, u can modify it communicate any TCP socket server like FTP (for file access), http(for web access), IRC (for chat) etc.
First of all, let's see SMTP mail client.
/*
Name : SMTPMail.cs
Description : SMTP email client .
Version : 1.0
Author : Ravindra Sadaphule
Email : [email protected]
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 whicjh 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 IP address of SMTP server
{
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 SMTP 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)
{
// System.Console.WriteLine("connection failed...");
_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 ;
}
//String[] SendBuffer = {"HELO thravindras\r\n","MAIL FROM: megha@thravindras\r\n","RCPT TO: ravindras@expowin2k\r\n","DATA \r\n"," Blah Blah Blah This is test mail from ravindra\r\n.\r\n","QUIT\r\n"};
System.String[] SendBuffer = new System.String[6] ;;
System.String[] ResponseCode = {"220","250","251","354","221"}; // success codes from SMTP server
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] ;
_errmsg += "<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 ;
}
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*********
The program is big but quite simple . A class SMTPClient is derived from TcpClient . Hence all protected and public properties of TcpClient are available to SMTPClient class.
Now go directly to SendMail class. In this class i have added several private variables and corresponding properties like smtpserverIP,MailFrom, MailTo etc. These properties are set by smtpMail.aspx program which is given below.
Property :
Description
Identity
This is identity of sender e.g. ravindra
MailFrom
Sender's email address e.g. [email protected]
MailTo
Recepient's email address
Subject
Topic of Mail
MailData
mail contents
SMTPServerIP
IP address of SMTP server
Loading
Join the conversation! Your thoughts help the community grow.