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
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
Receiver: 250 OK
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.
- 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();
-
- dataBuffer = BytesFromString("HELO KirtanHere" + CRLF);
- netStream.Write(dataBuffer, 0, dataBuffer.Length);
- ResponseString = reader.ReadLine();
- dataBuffer = BytesFromString("MAIL FROM:<[email protected]>" + CRLF);
- netStream.Write(dataBuffer, 0, dataBuffer.Length);
- ResponseString = reader.ReadLine();
-
- 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);
- }
-
- 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));
- }
- }