2

i am creating a web service to validate if an email inputted by a user exists or not. here is the web method.The problem is the method always returns true when the email is wrong or right.

[WebMethod(Description = "This method is used to validate email")]
public bool ValidateEmail(string email)
{
    bool isValid = false;

    try
    {
        string[] host = (email.Split('@'));
        string hostname = host[1];

        IPHostEntry IPhost = Dns.GetHostByName(hostname);

        IPEndPoint endPt = new IPEndPoint(IPhost.AddressList[0], 25);
        Socket soc = new Socket(endPt.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

        soc.Connect(endPt); //open connection to host
        soc.Close();
        isValid = true;

    }
    catch (Exception ex)
    {
        //ex.Message.ToString();
        isValid = false;
    }

    return isValid = true;

 }
gbubemi smith
  • 135
  • 4
  • 10
  • `Dns.GetHostByName()` queries the DNS *A records* for the domain which is not at all related to the address of the SMTP server that services the domain - that information is in *MX records* - the IP you resolve is worthless in relation to email. – Alex K. Jul 27 '17 at 10:28
  • 1
    The correct way to validate an email is to send it an email with a confirmation link. – Alex K. Jul 27 '17 at 10:29

0 Answers0