邮箱有效性验证 C#,net

2021/7/6 17:39:13

本文主要是介绍邮箱有效性验证 C#,net,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 private bool SendCommand(string command)
        {
            string read01 = "";
            try
            {

                arrayToSend = Encoding.UTF8.GetBytes(command.ToCharArray());
                s.Write(arrayToSend, 0, arrayToSend.Length);
                len = s.Read(bb, 0, bb.Length);
                read = Encoding.UTF8.GetString(bb);
                //read01 = Encoding.Default.GetString(bb, 0, len);
                //this.textBoxShow.Text += "收到:" + read.Substring(0, len) + "\r\n";
            }
            catch (IOException e)
            {
                return false;  //MessageBox.Show(e.ToString());                //MessageBox.Show(e.ToString());
            }
            if (read.StartsWith("250"))
            {
                return true;
            }

            else
            {
                return false;
            }

        }

 

  public int CheckEmailAndReturnCode(string mailAddress,out string Errstr)
        {
            Errstr = "";
            List<string> Result = new List<string>();
            Regex reg = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");

            if (!reg.IsMatch(mailAddress))
            {
                return 2;
                //return "Email地址形式上就不对";
            }


            string mailServer = getMailServer(mailAddress);

            if (mailServer == null)
            {
                return 3;
                //return "邮件服务器不存在";

            }
            //this.textBoxShow.Text += "解析出域名" + strDomain + "的mx记录:" + mailServer + "\r\n";
            tcpc = new TcpClient();      //为 TCP 网络服务提供客户端连接。
            tcpc.NoDelay = true;
            tcpc.ReceiveTimeout = 3000;
            tcpc.SendTimeout = 3000;
            bb = new byte[512];

            try
            {

                Connect(mailServer);//创建连接

                stringTosend = "helo " + mailServer + "\r\n";// 写入HELO命令
                //this.textBoxShow.Text += "发送:" + stringTosend;                
                flag = SendCommand(stringTosend);
                if (flag == false)
                {
                    return 4;
                    ////timer1.Enabled = false;
                    //return "写入HELO命令失败";
                }



                stringTosend = "mail from:<webmaster@aol.com>" + "\r\n";
                //this.textBoxShow.Text += "发送:" + stringTosend;
                flag = SendCommand(stringTosend);
                if (flag == false)
                {
                    return 5;
                    //return "写入Mail From命令";
                }
                stringTosend = "rcpt to:<" + mailAddress + ">" + "\r\n";//写入RCPT命令,这是关键的一步,后面的参数便是查询的Email的地址
                //this.textBoxShow.Text += "发送:" + stringTosend;
                flag = SendCommand(stringTosend);

                if (flag == true)
                {
                    //邮箱存在
                    return 1;
                    //return "邮箱存在";
                }
                else
                {
                    return 6;
                    //邮箱不存在
                    //return "邮箱不存在";
                }
 

            }
            catch (Exception ee)
            {
                Errstr = ee.Message;
                //MessageBox.Show(ee.ToString());   //发生错误或邮件服务器不可达
                return 7;
            }
        }
 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;
        }

 



这篇关于邮箱有效性验证 C#,net的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程