+1 vote

Hello,
I'm trying to connect to a SMTP server in SSL mode. I have to specify this server with IP address but when I try to connect I have an error:

The remote certificate is invalid according to the validation procedure

If I use no IP but server name it works perfectly. In some procedures I need to use IP address...what can I do?

by (450 points)

1 Answer

0 votes
 
Best answer

SSL certificate is issued for a specific name.

When connecting to a SMTP server, name on the certificate presented by the server, must match the server name you have used when connecting with SMTP client.

When you are using IP address, they obviously don't match.

You need to use ServerCertificateValidate callback and accept the certificate yourself:

using (Smtp smtp = new Smtp())
{
    smtp.ServerCertificateValidate += ValidateCertificate;

    smtp.ConnectSSL("192.168.0.1");

    ....
}

private static void ValidateCertificate(
    object sender,  
    ServerCertificateValidateEventArgs e)
{
    const SslPolicyErrors ignoredErrors = 
        SslPolicyErrors.RemoteCertificateChainErrors 
        | SslPolicyErrors.RemoteCertificateNameMismatch;

    if ((e.SslPolicyErrors & ignoredErrors) != 0)
    {
        e.IsValid = true;
        return;
    }

    e.IsValid = false;
}
by (297k points)
selected by
...