MailBee.NET Objects 3.1

ClientServerCertificates Class

Provides methods and properties for accessing client and server certificates which are used in order to establish secure SSL connection with a mail server.

For a list of all members of this type, see ClientServerCertificates Members.

System.Object
   MailBee.Security.ClientServerCertificates

public class ClientServerCertificates : IDisposable

Thread Safety

Public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe.

Remarks

You can use this class to assign the client certificate to be presented to the mail server during SSL handshakes, or to access the server certificate which becomes available to the client once SSL handshake is done, or both. Also, you can tell MailBee to automatically validate the server certificate against any of the available criteria and throw exception if the server certificate does not pass the test.

ClientServerCertificates instance is available through SmtpServer.SslCertificates, Pop3.SslCertificates and Imap.SslCertificates properties.

Note   To use this class, make sure MailBee.NET Security Powerup is licensed (see LicenseKey property for details).

Example

This sample attempts to select a client certificate to be presented to the mail server, connects to the server (dedicated SSL connection, not STARTTLS), and automatically validates the server certificate. If the server certificate is invalid, the sample reports what exactly is wrong with the server certificate.

[C#]
using System;
using MailBee;
using MailBee.Pop3Mail;
using MailBee.Security;

class Sample
{
    static void Main(string[] args)
    {
        Pop3 pop = new Pop3();

        // Start SSL handshake on "connecting to server" stage (dedicated port connection).
        // If you wish to use STARTTLS (regular port connection), select UseStartTls value
        // (port would be 110 and SSL handshake would occur later on StartTls or Login call).
        pop.SslMode = SslStartupMode.OnConnect;

        // Open Personal store of certificates, attempt to find the certificate containing
        // "john" in the email address or name, and present this certificate to the server.
        CertificateStore store = new CertificateStore(CertificateStore.Personal,
            CertStoreType.System, null);
        CertificateCollection certs = store.FindCertificates("john",
            CertificateFields.EmailAddress | CertificateFields.Name);
        store.Dispose();
        if (certs.Count > 0)
        {
            pop.SslCertificates.Client = certs[0];
            Console.WriteLine("Client certificate set.");
        }
        else
        {
            // Most servers do not require clients to authenticate themselves via SSL
            // certificates so that anonymous SSL connection usually works too unless
            // your server is an exception.
            Console.WriteLine("Client certificate not set, the connection will be anonymous.");
        }

        // Tell MailBee to automatically validate the server certificate and
        // throw exception if any of the available conditions is not met.
        pop.SslCertificates.AutoValidation = CertificateValidationFlags.All;
        try
        {
            pop.Connect("mail.domain.com", 995);    // 995 is dedicated S/POP3 port.
            pop.Disconnect();
        }
        catch (MailBeeCertificateValidationException e)
        {
            // Server certificate is not valid.
            Console.WriteLine(e.Message);

            // Build a string which lists the names of all the flags
            // the certificate validation process has failed for.
            string reasons = string.Empty;
            CertificateValidationFlags flags = e.Status;
            int mask = 1;
            while (flags > 0)
            {
                CertificateValidationFlags flag = flags & (CertificateValidationFlags)mask;
                if (flag != CertificateValidationFlags.None)
                {
                    if (reasons.Length > 0)
                    {
                        reasons += ", ";
                    }
                    reasons += flag.ToString();
                    flags &= (CertificateValidationFlags)~mask;
                }
                mask <<= 1;
            }

            Console.WriteLine("Reasons: " + reasons);
        }
    }
}
[Visual Basic]
Imports System
Imports MailBee
Imports MailBee.Pop3Mail
Imports MailBee.Security

Class Sample
    Shared Sub Main(ByVal args() As String)
        Dim pop As Pop3 = New Pop3

        ' Start SSL handshake on "connecting to server" stage (dedicated port connection).
        ' If you wish to use STARTTLS (regular port connection), select UseStartTls value
        ' (port would be 110 and SSL handshake would occur later on StartTls or Login call).
        pop.SslMode = SslStartupMode.OnConnect

        ' Open Personal store of certificates, attempt to find the certificate containing
        ' "john" in the email address or name, and present this certificate to the server.
        Dim store As CertificateStore = New CertificateStore(CertificateStore.Personal, _
                         CertStoreType.System, Nothing)
        Dim certs As CertificateCollection = store.FindCertificates("john", _
                            CertificateFields.EmailAddress Or CertificateFields.Name)
        store.Dispose()
        If certs.Count > 0 Then
            pop.SslCertificates.Client = certs(0)
            Console.WriteLine("Client certificate set.")
        Else
            ' Most servers do not require clients to authenticate themselves via SSL
            ' certificates so that anonymous SSL connection usually works too unless
            ' your server is an exception.
            Console.WriteLine("Client certificate not set, the connection will be anonymous.")
        End If

        ' Tell MailBee to automatically validate the server certificate and
        ' throw exception if any of the available conditions is not met.
        pop.SslCertificates.AutoValidation = CertificateValidationFlags.All
        Try
            pop.Connect("mail.domain.com", 995) ' 995 is dedicated S/POP3 port.
            pop.Disconnect()
        Catch e As MailBeeCertificateValidationException
            ' Server certificate is not valid.
            Console.WriteLine(e.Message)

            ' Build a string which lists the names of all the flags
            ' the certificate validation process has failed for.
            Dim reasons As String = String.Empty
            Dim flags As CertificateValidationFlags = e.Status
            Dim mask As Integer = 1
            While flags > 0
                Dim flag As CertificateValidationFlags = flags & CType(mask, CertificateValidationFlags)
                If flag <> CertificateValidationFlags.None Then
                    If reasons.Length > 0 Then
                        reasons &= ", "
                    End If
                    reasons &= flag.ToString()
                    flags = flags And CType(Not (mask), CertificateValidationFlags)
                End If
                mask <<= 1
            End While

            Console.WriteLine("Reasons: " & reasons)
        End Try
    End Sub
End Class

Requirements

Namespace: MailBee.Security

Assembly: MailBee.NET (in MailBee.NET.dll)

See Also

ClientServerCertificates Members | MailBee.Security Namespace | Certificate