初步测试,待后续优化:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Mail.Lib
{
public class MailUtilities
{
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];
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)
Console.WriteLine("连接服务器成功!" + "\r\n");
}
catch (Exception e)
{
Console.WriteLine(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);
Console.WriteLine("收到:" + read.Substring(0, len) + "\r\n");
}
catch (IOException e)
{
Console.WriteLine(e.ToString());
}
if (read.StartsWith("250"))
return true;
else
return false;
}
public bool checkEmail(string mailAddress)
{
Regex reg = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
if (!reg.IsMatch(mailAddress))
{
Console.WriteLine("Email地址形式上就不对" + "\r\n邮箱不存在!");
return false;
}//Email地址形式上就不对
string mailServer = getMailServer(mailAddress, true);
if (mailServer == null)
{
Console.WriteLine("邮件服务器不存在!" + "\r\n邮箱不存在!");
return false;
//邮件服务器探测错误
}
Console.WriteLine("解析出域名" + strDomain + "的mx记录:" + mailServer + "\r\n");
tcpc = new TcpClient(); //为 TCP 网络服务提供客户端连接。
tcpc.NoDelay = true;
tcpc.ReceiveTimeout = 3000;
tcpc.SendTimeout = 3000;
bb = new byte[512];
try
{
string strResponse = "";
string strTestFrom = "haha@163.com";
Console.WriteLine("连接服务器:" + mailServer + "\r\n");
Connect(mailServer);
string mailServer2 = getMailServer(strTestFrom, false);
stringTosend = "helo " + mailServer2 + "\r\n";
Console.WriteLine("发送:" + stringTosend);
flag = SendCommand(stringTosend);
if (flag == false)
{
return false;
}
stringTosend = "mail from:<" + strTestFrom + ">" + "\r\n";
Console.WriteLine("发送:" + stringTosend);
flag = SendCommand(stringTosend);
if (flag == false)
{
return false;
}
stringTosend = "rcpt to:<" + mailAddress + ">" + "\r\n";
Console.WriteLine("发送:" + stringTosend);
flag = SendCommand(stringTosend);
if (flag == true)
{
Console.WriteLine("邮箱存在!");
return true;
}
else
{
Console.WriteLine("用户不存在!" + "\r\n邮箱不存在!");
return false;
}
}
catch (Exception ee)
{
Console.WriteLine(ee.ToString()); //发生错误或邮件服务器不可达
}
return false;
}
}
}