我可以使用.net检查电子邮件地址是否存在吗?

hof1towb  于 2023-02-20  发布在  .NET
关注(0)|答案(7)|浏览(321)

我看过一些php的例子,你可以ping一个收件箱(不发送任何邮件到它),以检查它是否存在。我想知道是否有人知道这是可能的与。net?如果它是我要写一个应用程序做批量检查的电子邮件列表,我已经通过我的网站捕获。

roejwanj

roejwanj1#

SMTP为此定义了VRFY命令,但由于垃圾邮件发送者的滥用完全压倒了合法使用的数量,实际上世界上的每一个电子邮件服务器都是configured to lie

83qze16e

83qze16e2#

如果你写“检查邮件”是什么意思?如果不给邮件所有者发送一些独特的链接,你就不能检查这个,你只能检查邮件的语法和连接到smtp。

public static bool isEmail(string inputEmail)
{
   inputEmail  = NulltoString(inputEmail);
   string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
         @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" + 
         @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
   Regex re = new Regex(strRegex);
   if (re.IsMatch(inputEmail))
    return (true);
   else
    return (false);
}

smtp检查

string[] host = (address.Split('@'));
string hostname = host[1];

IPHostEntry IPhst = Dns.Resolve(hostname);
IPEndPoint endPt = new IPEndPoint(IPhst.AddressList[0], 25);
Socket s= new Socket(endPt.AddressFamily, 
        SocketType.Stream,ProtocolType.Tcp);
s.Connect(endPt);
au9on6nz

au9on6nz3#

不,原则上不可能检查电子邮件是否存在--与语言无关。根本没有协议可以做到这一点。
有一些局部的解决办法,但没有一个是可靠的。
有关详细信息,请参见How to check if an email address exists without sending an email?

ghhkc1vu

ghhkc1vu4#

1.使用以下命令获取电子邮件提供商的MX记录:

nslookup -type=mx gmail.com

1.调用tcp客户端以检查电子邮件是否有效:

private static void Main(string[] args)
{            
  var gMail = IsEmailAccountValid("gmail-smtp-in.l.google.com", "aa.aa@gmail.com");
  Console.WriteLine($"Gmail account is valid - {gMail.ToString()}");

  var live = IsEmailAccountValid("live-com.olc.protection.outlook.com", "aa.aa@live.com");
  Console.WriteLine($"Live account is valid - {live.ToString()}");
}

private static byte[] BytesFromString(string str)
{
  return Encoding.ASCII.GetBytes(str);
}

private static int GetResponseCode(string ResponseString)
{
  return int.Parse(ResponseString.Substring(0, 3));
}

private static bool IsEmailAccountValid(string tcpClient, string emailAddress)
{
  TcpClient tClient = new TcpClient(tcpClient, 25);
  string CRLF = "\r\n";
  byte[] dataBuffer;
  string ResponseString;
  NetworkStream netStream = tClient.GetStream();
  StreamReader reader = new StreamReader(netStream);
  ResponseString = reader.ReadLine();

  /* Perform HELO to SMTP Server and get Response */
  dataBuffer = BytesFromString("HELO Hi" + CRLF);
  netStream.Write(dataBuffer, 0, dataBuffer.Length);
  ResponseString = reader.ReadLine();
  dataBuffer = BytesFromString("MAIL FROM:<YourGmailIDHere@gmail.com>" + CRLF);
  netStream.Write(dataBuffer, 0, dataBuffer.Length);
  ResponseString = reader.ReadLine();

  /* Read Response of the RCPT TO Message to know from google if it exist or not */
  dataBuffer = BytesFromString($"RCPT TO:<{emailAddress}>" + CRLF);
  netStream.Write(dataBuffer, 0, dataBuffer.Length);
  ResponseString = reader.ReadLine();
  var responseCode = GetResponseCode(ResponseString);

  if (responseCode == 550)
  {
    return false;
  }

  /* QUITE CONNECTION */
  dataBuffer = BytesFromString("QUITE" + CRLF);
  netStream.Write(dataBuffer, 0, dataBuffer.Length);
  tClient.Close();
  return true;
}

MX记录可通过以下代码获取:

var lookup = new LookupClient();
var result = lookup.QueryAsync("gmail.com", QueryType.ANY).Result;
var domainName = result.Additionals[result.Additionals.Count - 1].DomainName.Value;

使用上面的代码找到MX查找并使用该MX查找来检查电子邮件是否有效。

fnx2tebb

fnx2tebb5#

http://www.codicode.com/art/free_asp_net_email_validator_verifier.aspx。使用dll引用到你的代码。它是免费的个人使用和重新分发。它检查域名的存在,而实际上没有发送电子邮件。

xeufq47z

xeufq47z6#

这并不是万无一失的。你能做的最好的事情就是检查语法,看看域名是否能解析。
电子邮件语法RegEx:(?<username>#?[_a-zA-Z0-9-+]+(\.[_a-zA-Z0-9-+]+)*)@(?<domain>[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|arpa|asia|coop|info|jobs|mobi|museum|name|travel)))

6g8kf2rb

6g8kf2rb7#

protected bool checkDNS(string host, string recType = "MX")
{
    bool result = false;
    try
    {
        using (Process proc = new Process())
        {
            proc.StartInfo.FileName = "nslookup";
            proc.StartInfo.Arguments = string.Format("-type={0} {1}", recType, host);
            proc.StartInfo.CreateNoWindow = true;
            proc.StartInfo.ErrorDialog = false;
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            proc.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
                {
                    if ((e.Data != null) && (!result))
                        result = e.Data.StartsWith(host);
                };
            proc.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =>
                {
                    if (e.Data != null)
                    {
                        //read error output here, not sure what for?
                    }
                };
            proc.Start();
            proc.BeginErrorReadLine();
            proc.BeginOutputReadLine();
            proc.WaitForExit(30000); //timeout after 30 seconds.
        }
    }
    catch
    {
        result = false;
    }
    return result;
}

相关问题