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

时间:2010-10-07 15:59:41

标签: c# .net email

我见过一些关于如何ping收件箱(不向其发送任何邮件)的php示例,以检查它是否存在。我想知道是否有人知道这是否可能与.net?如果我打算写一个应用程序来批量检查我通过我的网站捕获的电子邮件列表。

7 个答案:

答案 0 :(得分:45)

SMTP defines the VRFY command for this,但由于垃圾邮件发送者的滥用完全压倒了合法使用的数量,因此世界上几乎每个电子邮件服务器都是configured to lie

答案 1 :(得分:9)

如果您撰写“支票电子邮件”,您的意思是什么?如果没有为电子邮件所有者发送一些唯一链接,则无法检查此信息,您只能检查电子邮件的语法以及与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);

答案 2 :(得分:9)

不,原则上不可能检查是否存在电子邮件 - 与语言无关。 根本没有协议可以做到。

有一些部分解决方案,但它们都不可靠。

有关详细信息,请参阅How to check if an email address exists without sending an email?

答案 3 :(得分:3)

http://www.codicode.com/art/free_asp_net_email_validator_verifier.aspx。 使用代码的dll引用。它对个人使用和再分配都是免费的。它会在不实际发送电子邮件的情况下检查域名是否存在。

答案 4 :(得分:1)

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查找来检查电子邮件是否有效。

答案 5 :(得分:0)

这不是万无一失的。您可以做的最好的事情是检查语法并查看域名是否已解析。

电子邮件语法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)))

答案 6 :(得分:-1)

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;
}