The verification of the validity of email address is a problem often encountered! The general test method is to the email address string for a simple format test, such as whether or not to contain the valid characters such as @. This method only guarantees that the address is in a format that appears to be valid and does not guarantee an address to be reached. Recently a large number of address verification, wrote a small program, can detect whether the email address is really up to.
The email address includes two parts: User name and mail server. Therefore, the test email address can be divided into two steps: First verify the mail server, and then verify the user name. such as brookes_luan@yahoo.com.cn, first verify that the yahoo.com.cn server is a valid mail server, and if so, to confirm the presence of Brookes_luan users on the server.
By querying the DNS server to obtain an MX (mail exchanger) record for the domain name, you can determine whether a domain name corresponds to a mail server that is valid. In Windows systems, you can use the Nslookup program to view this record.
Query MX records via nslookup program to get mail server for 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;
}
The second step is to connect the mail server to confirm the availability of the server and whether the user exists
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;//email address form is not correct
String Mailserver=getmailserver (MailAddress);
if (mailserver==null)
{
return 404; Mail server probing error
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:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.