Thursday, 19 November 2015

How to ignore certificate validation when sending an email using SmtpClient in Dotnet


Issue:
If there is some Issue with Server certificate below error will occur,
The remote certificate is invalid according to the validation procedure


Solution:
1)      Use valid server certificates.
2)      Right code to ignore invalid certificate (refer below code lines).


Code lines:
1.      ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;
2.      smtpClient.Send(mailMessage);

Add Callback handler (ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate) before sending email (before smtpClient.Send() method)


ValidateServerCertificate() definition:

private static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            if (sslPolicyErrors == SslPolicyErrors.None)
            {
                return true;
            }
            //The server certificate is not valid. Continuing with sending email.;
            return true;
        }


No comments:

Post a Comment