如何使用 C# 驗證郵件地址 .

using System.Text.RegularExpressions;
using System.Diagnostics;
using System.IO;
//檢查郵件服務器,如果mail exchanger不爲null,返回mail server地址
        public string getMailServer(string strEmail)
        {
            if (!IsEmail(strEmail))
            {
                return null;
            }
            string strDomain = strEmail.Trim().ToLower().Split('@')[1];
            ProcessStartInfo PSinfo = new ProcessStartInfo();
            PSinfo.UseShellExecute = false;
            PSinfo.RedirectStandardInput = true;
            PSinfo.RedirectStandardOutput = true;
            PSinfo.FileName = "nslookup";
            PSinfo.CreateNoWindow = true;
            PSinfo.Arguments = "-type=mx " + strDomain;
            Process proc = Process.Start(PSinfo);
            StreamReader Sreader = proc.StandardOutput;
            Regex rgx = new Regex("mail exchanger = (?<mailServer>[^//s]+)");
            string strResponse = "";
            while ((strResponse = Sreader.ReadLine()) != null)
            {
                Match aMatch = rgx.Match(strResponse);
                if (rgx.Match(strResponse).Success)
                {
                    string Gvalue = aMatch.Groups["mailServer"].Value;
                    return Gvalue;
                }
            }
            return null;
        }
        //正則表達式驗證Email地址格式
        public bool IsEmail(string str_Email)
        {
            return Regex.IsMatch(str_Email, @"^([/w-/.]+)+[a-zA-Z0-9]+@((/[[0-9]{1,3}/.[0-9]{1,3}/.[0-9]{1,3}/.)|(([/w-]+/.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(/]?)$");
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章