SSL Certificate Validation with POP

By default, PopClient tries to connect to a mail server using Secure Sockets Layer (SSL) connection. If successful, it will do basic certificate validation, but for advanced uses, the GemBox.Email component provides a way to do custom certificate validation.

The following example shows how to validate a certificate using a custom method delegate, which is specified in the PopClient constructor.

Screenshot
SSL Certificate Validation Screenshot

See the full code below.

using GemBox.Email;
using GemBox.Email.Pop;
using GemBox.Email.Security;
using System;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

namespace Samples
{
    class Sample
    {
        [STAThread]
        static void Main(string[] args)
        {
            // If using Professional version, put your serial key below.
            ComponentInfo.SetLicense("FREE-LIMITED-KEY");

            // Define certificate validation delegate which will ignore 
            // 'Certificate name mismatch' errors
            RemoteCertificateValidationCallback validationDelegate =
                (object sender, 
                 X509Certificate certificate, 
                 X509Chain chain, 
                 SslPolicyErrors sslPolicyErrors) =>
                {
                    if (sslPolicyErrors == SslPolicyErrors.None ||
                        sslPolicyErrors == SslPolicyErrors.RemoteCertificateNameMismatch)
                    {
                        Console.WriteLine("Server certificate is valid.");
                        return true;
                    }
                    else
                    {
                        Console.WriteLine("Server certificate is invalid. Errors: " + 
                            sslPolicyErrors.ToString());
                        return false;
                    }
                };

            // Create new PopClient and specify IP port, 
            // security type and certificate validation callback
            using (PopClient pop = new PopClient("<ADDRESS> (e.g. pop.gmail.com)",
                                                 995,
                                                 ConnectionSecurity.Ssl,
                                                 validationDelegate))
            {
                // Connect to mail server
                pop.Connect();
                Console.WriteLine("Connected.");

                // Authenticate with specified username,
                // password and authentication mechanism
                pop.Authenticate("<USERNAME>", "<PASSWORD>", PopAuthentication.Plain);
                Console.WriteLine("Authenticated.");
            }
        }
    }
}
Imports System.Net.Security
Imports System.Security.Cryptography.X509Certificates
Imports GemBox.Email
Imports GemBox.Email.Pop
Imports GemBox.Email.Security

Namespace Samples
    Module Sample

        Sub Main()

            ' If using Professional version, put your serial key below.
            ComponentInfo.SetLicense("FREE-LIMITED-KEY")

            ' Define certificate validation delegate which will ignore
            ' 'Certificate name mismatch' errors
            Dim validationDelegate As RemoteCertificateValidationCallback =
                Function(sender As Object,
                         certificate As X509Certificate,
                         chain As X509Chain,
                         sslPolicyErrors As SslPolicyErrors) As Boolean

                    If sslPolicyErrors = SslPolicyErrors.None OrElse
                       sslPolicyErrors = SslPolicyErrors.RemoteCertificateNameMismatch Then
                        Console.WriteLine("Server certificate is valid.")
                        Return True
                    Else
                        Console.WriteLine("Server certificate is invalid. Errors: " &
                                          sslPolicyErrors.ToString())
                        Return False
                    End If
                End Function

            ' Create new PopClient and specify IP port,
            ' security type And certificate validation callback
            Using pop As New PopClient("<ADDRESS> (e.g. pop.gmail.com)",
                                       995,
                                       ConnectionSecurity.Ssl,
                                       validationDelegate)

                ' Connect to mail server
                pop.Connect()
                Console.WriteLine("Connected.")

                ' Authenticate with specified username,
                ' password And authentication mechanism
                pop.Authenticate("<USERNAME>", "<PASSWORD>", PopAuthentication.Plain)
                Console.WriteLine("Authenticated.")
            End Using

        End Sub

    End Module
End Namespace