C# 通過探測郵件服務器進行Email地址有效性檢驗


      Email地址有效性的檢驗是一個經常遇到的問題啦!一般的檢驗方法是對Email地址字符串進行簡單的格式檢驗,如是否含有@ .等有效字符等。這種方法只能保證該地址從格式上看似有效,並不能保證地址可達。最近進行大量的地址校驗,寫了一個小程序,可以檢測Email地址是否真 正可達。
 
      Email地址包括兩個部分:用戶名和郵件服務器。因此,檢驗郵件地址可以分為兩步進行:首先檢驗郵件服務器,然后檢驗用戶名。如 abc@163.com,首先檢驗163.com服務器是否是有效的郵件服務器,如果是再在該服務器上確認是否存在abc用戶。
 
      通過查詢DNS服務器,獲取域名的MX(Mail Exchanger)記錄,可以確定某一域名對應的郵件服務器是否有效。在Windows系統中,可以使用nslookup程序來查看這一記錄。
 
  //通過nslookup程序查詢MX記錄,獲取域名對應的mail服務器

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;
        }

 第二步,連接郵件服務器,確認服務器的可用性和用戶是否存在

public int checkEmail(string mailAddress,out string errorInfo)
        {
            Regex reg = new Regex("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$");
            if (!reg.IsMatch(mailAddress))
            {
                errorInfo = "Email Format error!";
                return 405;

            }
            string mailServer = getMailServer(mailAddress);
            if (mailServer == null)
            {
                errorInfo = "Email Server error!";
                return 404;
            }
            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 = mailAddress;
                sw.WriteLine("helo " + mailServer);
                sw.WriteLine("mail from:<" + mailAddress + ">");
                sw.WriteLine("rcpt to:<" + strTestFrom + ">");
                strResponse = sr.ReadLine();
                if (!strResponse.StartsWith("2"))
                {
                    errorInfo = "UserName error!";
                    return 403;
                }
                sw.WriteLine("quit");
                errorInfo = String.Empty;
                return 200; 

            }
            catch (Exception ee)
            {
                errorInfo = ee.Message.ToString();
                return 403;
            }
        }

 程序的調用方法:

if (checkEmail("abc@163.com", out errorInfo) == 200)
{
       return true;
}

這個程序是根據SMTP的基本過程實現的。與一個mail服務器連接發郵件的基本過程可能是這樣的:
 
  telnet mail.brookes.com 25
 
  >>220 brookes.com<IMail 8.02>
 
  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 <CRLF>.<CRLF>
 
  soem data.
 
  >>250 message queued
 
  QUIT
 
  >>221 Goodbye.
 
 
  灰色部分代碼是一個常規的Email地址檢查方法,檢查地址形式上的有效性。
 
  程序用到了System.IO,System.Net.Sockets,System.Diagnostics命名空間,通過checkMail(mailAddress)調用。
 
  說明:
 
  1.這種方法可以進一步檢查Email地址的有效性,比只從形式上驗證有了很大的進步。對於需要通過Email地址進行注冊信息驗證、發送密碼等應用,可以更進一步保證有效;
 
  2.由於Email服務器的多樣和可配置性,因此次程序並不能保證結果的普遍適用;
 
  3.對於一些大的郵件服務器,通常具有較強的反垃圾郵件功能,對於此類探測可能會作出反應,因此不適合於大量的地址探測。比如,我在探測過程中就發現了163.com服務器停止對次進行響應。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM