邮箱有效性验证 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的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具