Friday, October 3, 2008

Hi,

Here is my sample website to check the particular mail id is present or not in any domain.


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.CSharp;


public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void BtnCheck_Click(object sender, EventArgs e)
{
int result;
result = ChatMailServer(TextBox1.Text.Trim());

if (result == 1)
{
Label1.Text = "Valid E-mail Address";
}
if (result == 2)
{
Label1.Text = "Not a Valid E-mail Address";
}
if (result == 3)
{
Label1.Text = "Error Occured but Domain was GOOD";
}
if (result == 4)
{
Label1.Text = "Domain was invalid";
}
}
public int ChatMailServer(string sEmail)
{
NetworkStream oStream;
string sFrom;
string sTo;
string sResponse;
string Remote_Addr;
string mserver;
string[] sText;
int returnvalue = 0;
sTo = "<" + sEmail + ">";
sText = sEmail.Split('@');
mserver = NSLookup(sText[1]);
if (mserver == "")
{
returnvalue = 4;
return returnvalue;

}
Remote_Addr = "sify.com";
sFrom = "";
TcpClient oConnection = new TcpClient();

try
{
oConnection.SendTimeout = 3000;
oConnection.Connect(mserver, 25);
oStream = oConnection.GetStream();
sResponse = GetData(oStream);
sResponse = TalkToServer(oStream, "HELO " + Remote_Addr + "\n");
sResponse = TalkToServer(oStream, "MAIL FROM: " + sFrom + "\n");
if (ValidResponse(sResponse))
{
sResponse = TalkToServer(oStream, "RCPT TO: " + sTo + "\n");
if (ValidResponse(sResponse))
{
returnvalue = 1;
}
else
{
returnvalue = 2;
}
}
TalkToServer(oStream, "QUIT" + "\n");
oConnection.Close();
oStream = null;
}
catch
{
returnvalue = 3;
}

return returnvalue;
}
private string GetData(NetworkStream oStream)
{
byte[] bResponse = new byte[1024];
string sResponse = "";
int lenStream = 0;

lenStream = oStream.Read(bResponse, 0, 1024); // Doubt

if (lenStream > 0)
{
sResponse = Encoding.ASCII.GetString(bResponse, 0, 1024);
}
return sResponse;

}
private string SendData(NetworkStream oStream, string sToSend)
{
string sResponse;
byte[] bArray = Encoding.ASCII.GetBytes(sToSend.ToCharArray());

oStream.Write(bArray, 0, bArray.Length);
sResponse = GetData(oStream);

return sResponse;
}
private bool ValidResponse(string sResult)
{
bool bResult = false;
int iFirst;

if (sResult.Length > 1)
{
iFirst = Convert.ToInt32(sResult.Substring(0, 1));
if (iFirst < 3)
{
bResult = true;
}

}

return bResult;
}
private string TalkToServer(NetworkStream oStream, string sToSend)
{
string sresponse;

sresponse = SendData(oStream, sToSend);

return sresponse;
}
private string NSLookup(string sDomain)
{
ProcessStartInfo info = new ProcessStartInfo();
info.UseShellExecute = false;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.FileName = "nslookup";
info.Arguments = "-type=MX " + sDomain.ToUpper().Trim();

Process ns;
ns = Process.Start(info);
StreamReader sout;
sout = ns.StandardOutput;
Regex reg = new Regex("mail exchanger = (?[^\\\\\\s]+)");
string mailserver = "";
string response = "";
do
{
response = sout.ReadLine();
Match amatch = reg.Match(response);
Debug.WriteLine(response);
if (amatch.Success)
{
mailserver = amatch.Groups["server"].Value;
}
} while (sout.Peek() > -1);

return mailserver;

}
}

No comments: