Introduction
This is a web
based SMTP email program. This program can be used to send an email
through a SMTP server. The user can specify a SMTP server's IP, sender's mail
address, recipient's email address and mail content. When the user
clicks on the "send mail" button , the mail is forwarded to the SMTP
server which in turn forwards the mail to the recipient.
Details
This SMTP mail program is for .NET beta 2. I have written a SMTP email component in C# and compiled it
into a dll. Then this dll is accessed through an aspx file which can be viewed in
a browser.
Note: 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 a SMTP server. Once you grasp this technique, you can
modify it to communicate with any TCP socket server like FTP (for file access),
HTTP (for web access), IRC (for chat) etc.
First I'll give the source to the SMTP mail client.
namespace Ravindra
{
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 ;
}
}
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
{
set
{
_Subject = value ;
}
}
public System.String Identity
{
set
{
_Identity = value ;
}
}
public System.String MailFrom
{
set
{
_MailFrom = value ;
}
}
public System.String MailTo
{
set
{
_MailTo = value ;
}
}
public System.String MailData
{
set
{
_MailData = value ;
}
}
public System.String SMTPServerIP
{
get
{
return _SMTPServerIP ;
}
set
{
_SMTPServerIP = value ;
}
}
public System.String ErrorMessage
{
get
{
return _errmsg ;
}
}
public System.String ServerResponse
{
get
{
return _ServerResponse ;
}
}
public void SendMail()
{
try
{
System.String ServerResponse ;
SMTPClient tcp = new SMTPClient() ;
tcp.Connect(SMTPServerIP,25) ;
bool blnConnect = tcp.isConnected();
if (!blnConnect)
{
_errmsg = "Connetion Failed..." ;
return;
}
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 ;
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++ ;
}
}
catch(System.Net.Sockets.SocketException se)
{
_errmsg += se.Message + " " + se.StackTrace;
}
catch(System.Exception e)
{
_errmsg += e.Message + " " + e.StackTrace;
}
}
}
}
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 the 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. ravindra@hotmail.com
|
MailTo:
| Recepient's email
address
|
Subject:
| Topic of Mail
|
MailData:
| mail contents
|
SMTPServerIP:
| IP
address of SMTP server
|
All these inputs are received
from smtpMail.aspx file. In the SendMail()
method of SMTPMail
class
an instance of SMTPClient
class
is created and connection to smtp server is attempted .The blnconnect flag indicates
success/failure connection. If a connection fails, then an error message is generated
and the program Terminates.
On a successful connection (i.e. blnconnect is
true) , the SMTP server's response is analyzed for success code "220".
If SMTP responds with success
code "220", then the action starts.
All SMTP commands are stored in
array SendBuffer[]
. All possible success codes from the SMTP server are stored in
an array ResponseCode[]
. So the commands from the array SendBuffer[]
are sent to
the SMTP server one by one and for each command the response from the SMTP server is analysed by
comparing the SMTP server reply code with any of the possible success codes in
the ResponseCode
array. If the command executes successfully then only the next
command is attempted. If any of commands is unsuccessful, then an error message is
generated and the program terminates immediately.
Compile this smtpmail.cs file
and put it into the 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 the default website in IIS.
This page can be viewed in browser by Typing url http://localhost/SMTPMAil.aspx.
The page shows some textboxes to
capture input data like SMTP server ip ,sender's email , recepient;s
email, subject, Mail Data etc. and a "Send Mail" button .
When user enters data into the textboxes and clicks
on the "send Mail" button , a function btnSend_Click()
is called. In this function
an instance of the SMTPMail
class is created. Please remember that this
SMTPMail
class has been defined in the SMTPMAIL.cs file. Iin
btnSend_Click()
we capture
inputs from user and set various properties of smail (an instance of
SMTPMail
) and call its
sendmail()
method.
If everything is OK ,
then a message "Mail Has been sent successfully" is displayed at the top of the
page.
If any error occurs, then an error message is displayed at the top.
<%@ 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
{
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>
MCSD.NET IT Professional with over 11 years of experience.Worked at various levels as Business Analyst, Technical Architect, System Analyst and Developer.