如何使用 C# 验证邮件地址

    技术2022-05-11  17

    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})(/]?)$");        }

     http://blog.csdn.net/kimiqiu/archive/2009/07/03/4319649.aspx


    最新回复(0)