SIGN UP MEMBER LOGIN:    
ARTICLE

Check if Email Address Really Exist or not Using C#

Posted by Kirtan Patel Articles | .NET 4.5 December 22, 2011
This article demonstrate basic technique for checking if particular email is exist or not. using SMTP protocol specifications in RFC 821.
Reader Level:

index.png

Note : Code written here by me is for checking if particular Gmail address exist or not.To make this code work with other Mail Providers you need to find Mail Server of the particular email provider by Querying MX Record Entry of particular mail provider.

Here is how you can determine MX Record of particular mail server using cmd

index1.png

Here I used basic technique of SMTP commands to check if mail address exist or not.

Here is how typical mail server communication is done.

Receiver: 220 server.com Simple Mail Transfer Service Ready

Sender  : HELO server.com

Receiver: 250 server.com

Sender  : MAIL FROM: <abc@gmail.com>

Receiver: 250 OK

Sender  : RCPT TO: <somemailaddress@gmail.com>

Receiver: 250 OK


Here when we perform RCPT TO command server checks existence of particular mail address by querying the server and if it find that Mail Address is correct it respond with 550 code and error message.

So we will fire RCPT TO command against gmail server and gmail server will respond with error message if email address in recipient field is not correct.if we get error that means email address is not correct one and we can display error to
user.

Now lets do the actual code work in which we will perform communication to SMTP server exactly as shown
  • Connect to server
  • Pefrom HELO
  • Fire MAIL FROM command
  • Fire RCPT TO command and check Response

Now to perform all this we need to communicate with server using Sockets i selected TcpClient to perform code. code like below to perform communication with server.

using System.Net.Sockets;
using System.IO;
using System.Text;
public partial class _Default : System.Web.UI.Page
 
{
     
protected void btnCheck_Click(object sender, EventArgs e)
     {
         
TcpClient tClient = new TcpClient("gmail-smtp-in.l.google.com", 25);
         
string CRLF = "\r\n";
         
byte[] dataBuffer;
         
string ResponseString;
        
NetworkStream netStream = tClient.GetStream();
         
StreamReader reader = new StreamReader(netStream);
        ResponseString = reader.ReadLine();
        
/* Perform HELO to SMTP Server and get Response */
 
        dataBuffer = BytesFromString("HELO KirtanHere" + CRLF);
         netStream.Write(dataBuffer, 0, dataBuffer.Length);
         ResponseString = reader.ReadLine();
        dataBuffer = BytesFromString(
"MAIL FROM:<YourGmailIDHere@gmail.com>" + CRLF);
         netStream.Write(dataBuffer, 0, dataBuffer.Length);
         ResponseString = reader.ReadLine();
        
/* Read Response of the RCPT TO Message to know from google if it exist or not */
 
        dataBuffer = BytesFromString("RCPT TO:<"+TextBox1.Text.Trim()+">"+CRLF);
         netStream.Write(dataBuffer, 0, dataBuffer.Length);
        ResponseString = reader.ReadLine();
         
if (GetResponseCode(ResponseString) == 550)
         {
             Response.Write(
"Mai Address Does not Exist !<br/><br/>");
             Response.Write(
"<B><font color='red'>Original Error from Smtp Server :</font></b>"+ResponseString);
        }
        
/* QUITE CONNECTION */
 
        dataBuffer = BytesFromString("QUITE" + CRLF);
         netStream.Write(dataBuffer, 0, dataBuffer.Length);
         tClient.Close();
     }
    
private byte[] BytesFromString(string str)
     {
         
return Encoding.ASCII.GetBytes(str);
     }
    
private int GetResponseCode(string ResponseString)
     {
         
return int.Parse(ResponseString.Substring(0, 3));
     }
 }

Login to add your contents and source code to this article
Article Extensions
Contents added by sunil mohanty on Feb 23, 2012
share this article :
post comment
 

its check only domail not user id

Posted by Syed Ali Mar 20, 2012

Thanks all for your feedback :)

Posted by Kirtan Patel Dec 24, 2011

Thanks Mr. Kirtan

Posted by Jimmy Underwood Dec 24, 2011

Nice article, Kirtan. I was searching for such article.

Posted by Alok Pandey Dec 23, 2011

Nice article. Thanks for sharing it.

Posted by Daisy Krause Dec 23, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Become a Sponsor