Can I check if an email address exists using .net?

后端 未结 7 533

Ive seen some php examples of how you can ping an inbox(without sending any mail to it) to check whether it exists. I was wondering if anyone knows if this is possible with

7条回答
  •  长情又很酷
    2020-11-30 09:19

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

提交回复
热议问题