GemBox.Email is a .NET component that enables you to read, write, receive, and send emails from your .NET applications using one simple API.

SSL Certificate Validation with SMTP

By default, SmtpClient tries to connect to a mail server using Secure Sockets Layer (SSL) connection. If successful, it will do basic certifacte 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 SmtpClient constructor.

Screenshot
SSL Certificate Validation Screenshot

See the full code below.

using GemBox.Email;
using GemBox.Email.Smtp;
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 SmtpClient and specify IP port, 
            // security type and certificate validation callback
            using (SmtpClient smtp = new SmtpClient("<ADDRESS> (e.g. smtp.gmail.com)",
                                                    465,
                                                    ConnectionSecurity.Ssl,
                                                    validationDelegate))
            {
                // Connect to mail server
                smtp.Connect();
                Console.WriteLine("Connected.");

                // Authenticate with specified username,
                // password and authentication mechanism
                smtp.Authenticate("<USERNAME>", "<PASSWORD>", SmtpAuthentication.Plain);
                Console.WriteLine("Authenticated.");
            }
        }
    }
}
Imports System.Net.Security
Imports System.Security.Cryptography.X509Certificates
Imports GemBox.Email
Imports GemBox.Email.Smtp
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 SmtpClient and specify IP port,
            ' security type And certificate validation callback
            Using smtp As New SmtpClient("<ADDRESS> (e.g. smtp.gmail.com)",
                                     465,
                                     ConnectionSecurity.Ssl,
                                     validationDelegate)

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

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

        End Sub

    End Module
End Namespace

Check next sample.