首页 > 编程 > C# > 正文

使用C#正则表达式验证邮件的代码

2023-05-17 11:42:01
字体:
来源:转载
供稿:网友

1、验证输入的正确性

public static bool isEmail(string inputEmail)
        {
                inputEmail = NulltoString(inputEmail);
                string strRegex = @"^([a-zA-Z0-9_/-/.]+)@((/[[0-9]{1,3}/.[0-9]{1,3}/.[0-9]{1,3}/.)|(([a-zA-Z0-9/-]+/.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(/]?)$";
                Regex re = new Regex(strRegex);
                if (re.IsMatch(inputEmail))
                        return (true);
                else
                        return (false);
        }

2、验证邮件地址的正确性:

string[] host = (address.Split(@));

string hostname = host[1];

IPHostEntry IPhst = Dns.Resolve(hostname);
        IPEndPoint endPt = new IPEndPoint(IPhst.AddressList[0], 25);
        Socket s= new Socket(endPt.AddressFamily, SocketType.Stream,ProtocolType.Tcp);
        s.Connect(endPt);

3、//连接
        if(!Check_Response(s, SMTPResponse.CONNECT_SUCCESS))
        { 
                s.Close();
                return false;
        }

//HELLO服务器

Senddata(s, string.Format("HELO {0}/r/n", Dns.GetHostName() ));
        if(!Check_Response(s, SMTPResponse.GENERIC_SUCCESS))
        {
                s.Close();
                return false;
        }

//识别自己

//服务器可能解析你的域名并检测你是否在黑名单etc.

Senddata(s, string.Format("MAIL From: {0}/r/n","testexample@deepak.portland.co.uk"));
        if(!Check_Response(s, SMTPResponse.GENERIC_SUCCESS))
        {
                s.Close();
                return false;
        }

//开始分发 (我可以使用VRFY, 但是大多数人使用SMTP服务器,由于安全等方面的原因而禁用了前者)

Senddata(s, address);
        if(!Check_Response(s, SMTPResponse.GENERIC_SUCCESS))
        {
                s.Close();
                return false;
        }
        return (true);

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表