Email address validity test by detecting mail servers

xiaoxiao2021-03-05  21

The test of the effectiveness of the Email address is a frequent problem! The general inspection method is a simple format test for the Email address string, such as whether it contains @. This approach can only ensure that the address seems to be valid from the format and cannot guarantee the address to reach. Recently, a large number of address checks, write a small program, you can detect whether the Email address is really arrogant.

The Email address includes two parts: username and mail server. Therefore, the verification email address can be divided into two steps: first check the mail server, then check the username. Such as Brookes_luan@yahoo.com.cn, first check if the Yahoo.com.cn server is a valid mail server, if it is again confirmed whether there is a Brookes_luan user.

By querying the DNS server, obtain the MX (Mail Exchange record of the domain name, you can determine if a domain name corresponds to whether the mail server is valid. In the Windows system, you can use the NSLookUP program to view this record.

// query by the program nslookup MX record, obtaining the domain name corresponding to the mail server 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 excsrver = (? [^ // s] ) "); string strresponse =" "; while (Strresponse = Sout.Readline () )! = NULL) {match Amatch = reg.match (strretponse); if (reg.match (strret ".success) return avatch.groups [" mailserver "]. Value;} Return Null;}

Step 2, connect the mail server, 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-ZA-Z0-9 -] //.) {1,} (COM | NET | EDU | MIZ | BIZ | CN | CC) $ ");

IF (! reg.ismatch (mailaddress) return 405; // email address form is not

string mailServer = getMailServer (mailAddress); if (mailServer == null) {return 404; // mail server error detection} 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 stratefrom = "brookes_luan@yahoo.com.cn"; sw.writeline ("Helo" mailserver); sw.writeline ("Mail from: <" mailaddress "); sw.writeline RCPT TO: <" strTestFrom >"); strretponse = sr.readline (); if (! strretponse.startswith ("2")) Return 403; // User name is incorrect SW.WRITELINE ("quit"); Return 200; // email address check} Catch (Exception EE) {RETURN 403; // Error or mail server unreachable}} This program is implemented according to the basic process of SMTP. The basic process of connecting an email with a MAIL server may be like this:

Telnet mail.brookes.com 25

>> 220 Brookes.com

Helo

>> 250 mail.brookes.com

Mail from: brookes@tsinghua.org.cn

>> 250 ok

RCPT to: me@brookes.com

>> 250 ok its for me@brookes.com

Data

>> Ok.send it; end with .

SOEM DATA.

>> 250 Message Queued

Quit

>> 221 Goodbye.

Gray partial code is a conventional email address check method to check the validity of the address form.

The program is used in System.IO, System.Net.Sockets, System.Diagnostics namespace, called by Checkmail (MailAddress).

Description:

1. This method can further check the effectiveness of the Email address, which has great progress than only verified from form. For applications that need to be registered with the email address, send passwords, etc., can be further guaranteed;

2. Due to the diverse and configurability of the Email server, the secondary program does not guarantee the general applicability of the results;

转载请注明原文地址:https://www.9cbs.com/read-38249.html

New Post(0)