Validate existence of an email with C #

0

I have this function in which I intend to validate the existence of an email. But I mark an error in the creation of the instance of the function:

static void Main(string[] args) {
  bool bandera = checkDNS("[email protected]", "MX");
}

public bool checkDNS(string host, string recType) {
  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;
}

The error is thrown here:

bool bandera = checkDNS("[email protected]","MX");

With the exception:

  

An object reference is required for the nonstatic field, method, or property

    
asked by Ernesto Emmanuel Yah Lopez 15.06.2017 в 13:59
source

1 answer

1

The checkDNS method must be static.

static void Main(string[] args)
{
    bool bandera = checkDNS("[email protected]","MX");
}



public static bool checkDNS(string host, string recType) // Aqui esta el cambio
{
    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;
}
    
answered by 15.06.2017 / 14:22
source