• 邮箱有效性验证 C#,net


    1.检测邮箱格式 2.获取邮箱的服务地址 3.创建链接 4.发送helo命令 5.写入from 和 to 
      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 + "
    ";
                tcpc = new TcpClient();      //为 TCP 网络服务提供客户端连接。
                tcpc.NoDelay = true;
                tcpc.ReceiveTimeout = 3000;
                tcpc.SendTimeout = 3000;
                bb = new byte[512];
    
                try
                {
    
                    Connect(mailServer);//创建连接
    
                    stringTosend = "helo " + mailServer + "
    ";// 写入HELO命令
                    //this.textBoxShow.Text += "发送:" + stringTosend;                
                    flag = SendCommand(stringTosend);
                    if (flag == false)
                    {
                        return 4;
                        ////timer1.Enabled = false;
                        //return "写入HELO命令失败";
                    }
    
    
    
                    stringTosend = "mail from:<webmaster@aol.com>" + "
    ";
                    //this.textBoxShow.Text += "发送:" + stringTosend;
                    flag = SendCommand(stringTosend);
                    if (flag == false)
                    {
                        return 5;
                        //return "写入Mail From命令";
                    }
                    stringTosend = "rcpt to:<" + mailAddress + ">" + "
    ";//写入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;
            }
     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) + "
    ";
                }
                catch (IOException e)
                {
                    return false;  //MessageBox.Show(e.ToString());                //MessageBox.Show(e.ToString());
                }
                if (read.StartsWith("250"))
                {
                    return true;
                }
    
                else
                {
                    return false;
                }
    
            }
  • 相关阅读:
    03-三维空间刚体运动-基础知识体系
    特征值和特征向量
    齐次坐标和一般坐标
    c++中 重载 覆盖 隐藏的区别 附加 mutable笔记
    快速学习理解网络协议4
    快速学习理解网络协议3
    快速学习理解网络协议2
    快速学习理解网络协议1
    c++的直接初始化与复制初始化 未完成!!!!!!!!!!!!
    高性能网络编程(二)
  • 原文地址:https://www.cnblogs.com/hanke123/p/14977762.html
Copyright © 2020-2023  润新知