Check email address validity by detecting the email server

Source: Internet
Author: User
Tags nslookup

The verification of email address validity is a common problem! The general check method is to perform a simple format check on the email address string, such as whether it contains valid characters such. This method can only ensure that the address looks valid in the format, and cannot guarantee that the address can be reached. Recently, a large number of address verification tasks have been carried out, and a small program has been written to check whether the email address is truly reachable.

The email address consists of the user name and email server. Therefore, the verification email address can be divided into two steps: first, check the email server and then check the user name. For example, the first checks whether the Server is a valid email server, and then checks whether the brookes_luan user exists on the server.

Query the DNS server and obtain the MX (Mail exchanger) Record of the domain name to determine whether the email server corresponding to a domain name is valid. In Windows, you can use the NSLookup program to view this record.

// Use the NSLookup program to query MX records and obtain the mail server corresponding to the domain name
Public String getmailserver (string stremail)
String strdomain = stremail. Split ('@') [1];
Processstartinfo info = new processstartinfo ();
Info. useshellexecute = false;
Info. redirectstandardinput = true;
Info. redirectstandardoutput = true;
Info. filename = "NSlookup ";
Info. createnowindow = true;
Info. Arguments = "-type = Mx" + strdomain;
Process NS = process. Start (Info );
Streamreader sout = NS. standardoutput;
RegEx Reg = new RegEx ("mail exchanger = (? <MailServer> [^ // s] + )");
String strresponse = "";
While (strresponse = sout. Readline ())! = NULL ){
Match amatch = reg. Match (strresponse );
If (Reg. Match (strresponse). Success) return amatch. Groups ["MailServer"]. value;

Return NULL;

Step 2: connect to the email server and check whether the server availability and users exist

Public int checkemail (string mailaddress)


RegEx Reg = new RegEx ("^ [a-zA-Z0-9 _-] + @ ([a-zA-Z0-9-] + //.) {1 ,}( com | net | Edu | Miz | biz | CN | CC) $ ");

If (! Reg. ismatch (mailaddress) return 405; // the email address format is incorrect.

String MailServer = getmailserver (mailaddress );
If (MailServer = NULL)
Return 404; // email server Detection Error
Tcpclient TCPC = new tcpclient ();
TCPC. nodelay = true;
TCPC. receivetimeout = 3000;
TCPC. sendtimeout = 3000;
Try {
TCPC. Connect (MailServer, 25 );
Networkstream S = TCPC. getstream ();
Streamreader sr = new streamreader (S, encoding. Default );
Streamwriter Sw = new streamwriter (S, encoding. Default );
String strresponse = "";
String strtestfrom = " ";
Sw. writeline ("HELO" + MailServer );
Sw. writeline ("mail from: <" + mailaddress + "> ");
Sw. writeline ("rcpt to: <" + strtestfrom + "> ");
Strresponse = Sr. Readline ();
If (! Strresponse. startswith ("2") return 403; // incorrect user name
Sw. writeline ("quit ");
Return 200; // check the email address

} Catch (exception ee)
Return 403; // an error occurs or the email server is inaccessible.

This program is implemented based on the basic process of SMTP. The basic process of connecting to an email server may be as follows:

Telnet 25

> 220 <IMail 8.02>


> 250

Mail from:

> 250 OK

Rcpt to:

> 250 OK its for


> OK. Send it; end with <CRLF>. <CRLF>

Soem data.

> 250 message queued


> 221 goodbye.


The gray part of the code is a conventional email address check method that checks the validity of the address form.

The program uses the system. Io, system. net. sockets, system. Diagnostics namespace, and calls it through checkmail (mailaddress.


1. This method can further check the validity of the email address, which is much more advanced than formal verification. For applications that require verification of registration information and password sending through an email address, this can be further ensured;


2. Due to the diversity and configurability of the email server, the sub-program cannot guarantee the general application of the results;

3. Some large mail servers usually have strong anti-spam functions, which may respond to such detection, so it is not suitable for a large number of address detection. For example, during the probe process, I found that the server stopped responding to the request.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.