0

Tengo esta función en la cual pretendo validar la existencia de un email. Pero me marca un error en la creación de la instancia de la función:

static void Main(string[] args) {
  bool bandera = checkDNS("soyplato3@gmail.com", "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;
}

El error se lanza aquí:

bool bandera = checkDNS("soyplato3@gmail.com","MX");

Con la excepción:

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

18
  • Que error? Donde exactamente?
    – Pikoh
    el 15 jun. 2017 a las 14:11
  • 1
    Pero que excepción de da exactamente?
    – Pikoh
    el 15 jun. 2017 a las 14:12
  • 1
    Creo que tiene que ver con que estas llamando desde una funcion estatica a una que no lo es. Prueba con public static bool checkDNS(string host, string recType)
    – Lithorell
    el 15 jun. 2017 a las 14:19
  • 1
    Exacto. Como dice @Lithorell el problema es que el método checkDNS no es estático. De todas maneras...esto no es asp.net?
    – Pikoh
    el 15 jun. 2017 a las 14:21
  • 2
    No es posible validar una cuenta de correo. Existe un comando verify conectandote al servidor de correo para ver si existe, pero a cuenta de los spammers, actualmente yo diría que todos los servidores o lo bloquean o directamente mienten
    – Pikoh
    el 15 jun. 2017 a las 14:36

1 respuesta 1

1

El metodo checkDNS debe ser estatico.

static void Main(string[] args)
{
    bool bandera = checkDNS("soyplato3@gmail.com","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;
}
6

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.