Check if Email Address Really Exist or not Using C#

This article demonstrate basic technique for checking if particular email is exist or not. using SMTP protocol specifications in RFC 821.

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));
     }
 }