验证邮箱是否真实存在 c#
·
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.IO;
using System.Diagnostics;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
TcpClient tcpc;
NetworkStream s;
string strDomain;
byte[] bb;
int len;
string read;
string stringTosend;
byte[] arrayToSend;
bool flag;
int count = 0;
public string getMailServer(string strEmail, bool IsCheck)
{
strDomain = strEmail.Split('@')[1];
if (IsCheck == true)
this.textBoxShow.Text += "分离出邮箱域名:" + strDomain + "\r\n";
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); // Match 表示单个正则表达式匹配的结果。
if (reg.Match(strResponse).Success)
{
return amatch.Groups["mailServer"].Value; //获取由正则表达式匹配的组的集合
}
}
return null;
}
private void Connect(string mailServer)
{
try
{
tcpc.Connect(mailServer, 25);
s = tcpc.GetStream();
len = s.Read(bb, 0, bb.Length);
read = Encoding.UTF8.GetString(bb);
if (read.StartsWith("220") == true)
this.textBoxShow.Text += "连接服务器成功!" + "\r\n";
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
private bool SendCommand(string command)
{
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);
this.textBoxShow.Text += "收到:" + read.Substring(0, len) + "\r\n";
}
catch (IOException e)
{
MessageBox.Show(e.ToString());
}
if (read.StartsWith("250"))
return true;
else
return false;
}
public void checkEmail(string mailAddress)
{
Regex reg = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
if (!reg.IsMatch(mailAddress))
{
this.textBoxShow.Text += "Email地址形式上就不对" + "\r\n";
return;
}//Email地址形式上就不对
string mailServer = getMailServer(mailAddress, true);
if (mailServer == null)
{
this.textBoxShow.Text += "邮件服务器不存在!" + "\r\n";
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)
{
//timer1.Enabled = false;
return;
}
stringTosend = "mail from:<" + mailAddress + ">" + "\r\n"; ////写入Mail From命令
this.textBoxShow.Text += "发送:" + stringTosend;
flag = SendCommand(stringTosend);
if (flag == false)
{
return;
}
stringTosend = "rcpt to:<" + mailAddress + ">" + "\r\n";//写入RCPT命令,这是关键的一步,后面的参数便是查询的Email的地址
this.textBoxShow.Text += "发送:" + stringTosend;
flag = SendCommand(stringTosend);
if (flag == true)
{
//邮箱存在
}
else
{
//邮箱不存在
}
}
catch (Exception ee)
{
MessageBox.Show(ee.ToString()); //发生错误或邮件服务器不可达
}
}
private void button1_Click(object sender, EventArgs e)
{
checkEmail("wangzh300@163.com");
}
}
}
更多推荐



所有评论(0)