简体   繁体   中英

How to check an email address exists?

I am trying to check whether an email address exists, or not. I can verify @gmail.com addresses against the SMTP server gmail-smtp-in.l.google.com , but I do not know how to get the SMTP server for other domains, to check against. Please advise how to get the SMTP server of a domain, or alternatively another way to reliably check if email addresses exist.

This is my code to check @gmail.com addresses:

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 AnkurHere" + CRLF);
netStream.Write(dataBuffer, 0, dataBuffer.Length);
ResponseString = reader.ReadLine();

dataBuffer = BytesFromString("MAIL FROM:<abc@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)
   {
       label1.Text = "Mai Address Does not Exist !";
       label2.Text = "Original Error from Smtp Server" + ResponseString;
   }

/* QUITE CONNECTION */
dataBuffer = BytesFromString("QUITE" + CRLF);
netStream.Write(dataBuffer, 0, dataBuffer.Length);
tClient.Close();

For the approach you are making, you will need to make a DNS lookup for the MX record of the domain. As far as I am aware, there is no built in way in .NET to do this, so you'll either have to find a component or take a look at this question .

However you need to be advised that this method will not give 100% accurate results and may give false positives - some domains will accept a message for any address before either putting it into a "catch all" mailbox, silently deleting it or sending a bounce message. Accepting then bouncing is not a best practice but it does still occur on occasion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM