SSL Certificate Validation with SMTP

GemBox.Email supports secure connections via the SMTPS (SMTP Secure) communication. By default, SmtpClient will try to connect to your email server using a Secure Sockets Layer (SSL)/Transport Layer Security (TLS) encrypted connection.

If successful, it will do basic SSL/TLS certificate validation. For advanced uses, GemBox.Email provides a way to achieve custom client certificate validation by creating a certificate validation delegate and providing it to a client with SmtpClient Constructor.

The following example shows how you can validate a certificate using a custom method delegate.

Validating SSL certificate with SMPT client in C# and VB.NET
Screenshot of SmtpClient's certificate validation
using System;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using GemBox.Email;
using GemBox.Email.Smtp;
using GemBox.Email.Security;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Create certificate validation delegate.
        RemoteCertificateValidationCallback validationDelegate =
            (object sender,
             X509Certificate certificate,
             X509Chain chain,
             SslPolicyErrors errors) =>
            {
                if (errors == SslPolicyErrors.None || errors == SslPolicyErrors.RemoteCertificateNameMismatch)
                {
                    Console.WriteLine("Server certificate is valid.");
                    return true;
                }
                else
                {
                    Console.WriteLine($"Server certificate is invalid: {errors}");
                    return false;
                }
            };

        // Create new SmtpClient and specify host, port, security and certificate validation callback.
        using (SmtpClient smtp = new SmtpClient(
            "<ADDRESS> (e.g. smtp.gmail.com)",
            465,
            ConnectionSecurity.Ssl,
            validationDelegate))
        {
            // Connect to email 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
Imports System.Net.Security
Imports System.Security.Cryptography.X509Certificates
Imports GemBox.Email
Imports GemBox.Email.Smtp
Imports GemBox.Email.Security

Module Program

    Sub Main()

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

        ' Create certificate validation delegate.
        Dim validationDelegate As RemoteCertificateValidationCallback =
            Function(sender As Object,
                     certificate As X509Certificate,
                     chain As X509Chain,
                     errors As SslPolicyErrors) As Boolean

                If errors = SslPolicyErrors.None OrElse errors = SslPolicyErrors.RemoteCertificateNameMismatch Then
                    Console.WriteLine("Server certificate is valid.")
                    Return True
                Else
                    Console.WriteLine($"Server certificate is invalid: {errors}")
                    Return False
                End If
            End Function

        ' Create new SmtpClient and specify host, port, security and certificate validation callback.
        Using smtp As New SmtpClient(
            "<ADDRESS> (e.g. smtp.gmail.com)",
            465,
            ConnectionSecurity.Ssl,
            validationDelegate)

            ' Connect to email 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

For more information about SmtpClient, check out our SMTP Client Connection example.

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Email yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.