tags:

views:

20

answers:

1

The function below works in a console application but it's not working in my asp.net web site. I am getting an error: The remote certificate is invalid according to the validation procedure.

public static void SendEmail(MailMessage mm)
{
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.Port = 587;
    smtp.EnableSsl = true;
    smtp.Credentials = new NetworkCredential("[email protected]", "Pass");

    smtp.Send(mm);
}
A: 

You could try to handle the certificate validation event to make it easier to determine the reason why the remote certificate is considered invalid. For this, before calling the SendEmail method add the following line:

ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);

and then provide an implementation for ValidateServerCertificate, like

    public bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    // replace with proper validation
    if (sslPolicyErrors == SslPolicyErrors.None) 
        return true;
    else
        return false;
}

The parameters received by ValidateServerCertificate should give you details about why the validation fails (check sslPolicyErrors). You also have access to the remote certificate and chain of certificate authorities.

One possible scenario is that you run the website under a different user than the console application, and this user does not trust the issuer (or a intermediary authority in the chain) of the gmail's certificate. This might happen if you somehow have deleted certificates from the Local Machine/Trusted Root Certificates in Windows Certificate Store.

andrei m