Check if Email Address Really Exist or not Using C#

index.png
 
Note: The code written here by me is for checking if a particular Gmail address exists or not. To make this code work with other Mail Providers you need to find the Mail Server of the particular email provider by Querying MX Record Entry of the particular mail provider.
 
Here is how you can determine MX Record of a particular mail server using cmd
 
index1.png
 
Here I used the basic technique of SMTP commands to check if mail addresses 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: <[email protected]>
Receiver: 250 OK
Sender: RCPT TO: <[email protected]>
Receiver: 250 OK
 
Here when we perform RCPT TO command server checks the existence of a particular mail address by querying the server and if it finds that the Mail Address is correct it responds with a 550 code and error message.
 
So we will fire RCPT TO command against the Gmail server and the Gmail server will respond with an error message if the email address in the recipient field is not correct. if we get error that means the email address is not the correct one and we can display an error to the user.
 
Now let's do the actual code work in which we will perform communication to the SMTP server exactly as shown
  • Connect to server
  • Perform HELO
  • Fire MAIL FROM command
  • Fire RCPT TO command and check Response
Now to perform all this we need to communicate with the server using Sockets I selected TcpClient to perform code. code like below to perform communication with server.
  1. using System.Net.Sockets;  
  2. using System.IO;  
  3. using System.Text;  
  4. public partial class _Default: System.Web.UI.Page {  
  5.   protected void btnCheck_Click(object sender, EventArgs e) {  
  6.     TcpClient tClient = new TcpClient("gmail-smtp-in.l.google.com", 25);  
  7.     string CRLF = "\r\n";  
  8.     byte[] dataBuffer;  
  9.     string ResponseString;  
  10.     NetworkStream netStream = tClient.GetStream();  
  11.     StreamReader reader = new StreamReader(netStream);  
  12.     ResponseString = reader.ReadLine();  
  13.     /* Perform HELO to SMTP Server and get Response */  
  14.     dataBuffer = BytesFromString("HELO KirtanHere" + CRLF);  
  15.     netStream.Write(dataBuffer, 0, dataBuffer.Length);  
  16.     ResponseString = reader.ReadLine();  
  17.     dataBuffer = BytesFromString("MAIL FROM:<[email protected]>" + CRLF);  
  18.     netStream.Write(dataBuffer, 0, dataBuffer.Length);  
  19.     ResponseString = reader.ReadLine();  
  20.     /* Read Response of the RCPT TO Message to know from google if it exist or not */  
  21.     dataBuffer = BytesFromString("RCPT TO:<" + TextBox1.Text.Trim() + ">" + CRLF);  
  22.     netStream.Write(dataBuffer, 0, dataBuffer.Length);  
  23.     ResponseString = reader.ReadLine();  
  24.     if (GetResponseCode(ResponseString) == 550) {  
  25.       Response.Write("Mai Address Does not Exist !<br/><br/>");  
  26.       Response.Write("<B><font color='red'>Original Error from Smtp Server :</font></b>" + ResponseString);  
  27.     }  
  28.     /* QUITE CONNECTION */  
  29.     dataBuffer = BytesFromString("QUITE" + CRLF);  
  30.     netStream.Write(dataBuffer, 0, dataBuffer.Length);  
  31.     tClient.Close();  
  32.   }  
  33.   
  34.   private byte[] BytesFromString(string str) {  
  35.     return Encoding.ASCII.GetBytes(str);  
  36.   }  
  37.   
  38.   private int GetResponseCode(string ResponseString) {  
  39.     return int.Parse(ResponseString.Substring(0, 3));  
  40.   }  


Similar Articles