The remote certificate is invalid according to the validation procedure
If you get “The remote certificate is invalid according to the validation procedure” exception while trying to establish SSL connection, most likely your’s server certificate is self-signed.
You can accept self-signed certificates using Mail.dll IMAP POP3 and SMTP client.
First you need to subscribe to ServerCertificateValidate event.
Then you need to create ValidateCertificate method that validates the certificate (ignores certificate chain errors).
The sample below focuses on Imap class but exactly the same steps apply to Pop3 and Smtp classes.
// C# version
using System.Net.Security;
using System;
using Limilabs.Mail;
using Limilabs.Client.IMAP;
internal class Program
{
private static void Main(string[] args)
{
using (Imap client = new Imap())
{
// we will use custom validation
client.ServerCertificateValidate +=
new ServerCertificateValidateEventHandler(Validate);
// Minimalistic version to accept any certificate:
//client.ServerCertificateValidate += (sender, e) => { e.IsValid = true; };
client.ConnectSSL("server.example.com");
client.Login("user", "password");
foreach (long uid in client.GetAll())
{
IMail email = new MailBuilder().CreateFromEml(
client.GetMessageByUID(uid));
Console.WriteLine("subject: {0}", email.Subject);
}
client.Close();
}
}
private static void Validate(
object sender,
ServerCertificateValidateEventArgs e)
{
const SslPolicyErrors ignoredErrors =
SslPolicyErrors.RemoteCertificateChainErrors |
SslPolicyErrors.RemoteCertificateNameMismatch;
if ((e.SslPolicyErrors & ~ignoredErrors) == SslPolicyErrors.None)
{
e.IsValid = true;
return;
}
e.IsValid = false;
}
} ;
' VB.NET version
Imports System.Net.Security
Imports System
Imports Limilabs.Mail
Imports Limilabs.Client.IMAP
Public Module Module1
Public Sub Main(ByVal args As String())
Using client As New Imap()
' we will use custom validation
AddHandler client.ServerCertificateValidate, AddressOf ValidateCertificate
client.ConnectSSL("server.example.com")
client.Login("user", "password")
For Each uid As Long In client.GetAll()
Dim email As IMail = New MailBuilder().CreateFromEml(client.GetMessageByUID(uid))
Console.WriteLine("subject: {0}", email.Subject)
Next
client.Close()
End Using
End Sub
Private Sub ValidateCertificate( _
ByVal sender As Object, _
ByVal e As ServerCertificateValidateEventArgs)
Const ignoredErrors As SslPolicyErrors = _
SslPolicyErrors.RemoteCertificateChainErrors Or _
SslPolicyErrors.RemoteCertificateNameMismatch
If (e.SslPolicyErrors And Not ignoredErrors) = SslPolicyErrors.None Then
e.IsValid = True
Return
End If
e.IsValid = False
End Sub
End Module
November 15th, 2010 at 15:26
[...] The remote certificate is invalid according to the validation procedure [...]
November 4th, 2011 at 17:47
I’m trying to connect to an Exchange Server. I have bypassed the certificate problems by using the “minimalistic” version described above, with the delegate (s, e) => {e.IsValid = true;}. But I keep getting the message, “No connection could be made because the target machine actively refused it.”
The credentials are definitely correct. Any idea what could be causing the problem?
Thanks.
November 5th, 2011 at 12:13
@aroy,
It seems you have problem with connection, rather then certificate validation or authentication/authorization.
If it’s Exchange you most likely forgot to turn IMAP on. Please check this blog post for details on how to resole this issue:
http://www.limilabs.com/blog/connection-attempt-failed
May 8th, 2012 at 19:54
[...] If you are using self-signed certificates you may encounter this error: The remote certificate is invalid according to the validation procedure. [...]
May 8th, 2012 at 20:00
[...] If you are using self-signed certificates you may encounter this error: The remote certificate is invalid according to the validation procedure. [...]
May 8th, 2012 at 20:01
[...] If you are using self-signed certificates you may encounter this error: The remote certificate is invalid according to the validation procedure. [...]