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 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 PopClient constructor.

Screenshot

SSL Certificate Validation Screenshot

See the full code below.

1using GemBox.Email;
2using GemBox.Email.Pop;
3using GemBox.Email.Security;
4using System;
5using System.Net.Security;
6using System.Security.Cryptography.X509Certificates;
7
8namespace Samples
9{
10    class Sample
11    {
12        [STAThread]
13        static void Main(string[] args)
14        {
15            // If using Professional version, put your serial key below.
16            ComponentInfo.SetLicense("FREE-LIMITED-KEY");
17
18            // Define certificate validation delegate which will ignore 
19            // 'Certificate name mismatch' errors
20            RemoteCertificateValidationCallback validationDelegate =
21                (object sender, 
22                 X509Certificate certificate, 
23                 X509Chain chain, 
24                 SslPolicyErrors sslPolicyErrors) =>
25                {
26                    if (sslPolicyErrors == SslPolicyErrors.None ||
27                        sslPolicyErrors == SslPolicyErrors.RemoteCertificateNameMismatch)
28                    {
29                        Console.WriteLine("Server certificate is valid.");
30                        return true;
31                    }
32                    else
33                    {
34                        Console.WriteLine("Server certificate is invalid. Errors: " + 
35                            sslPolicyErrors.ToString());
36                        return false;
37                    }
38                };
39
40            // Create new PopClient and specify IP port, 
41            // security type and certificate validation callback
42            using (PopClient pop = new PopClient("<ADDRESS> (e.g. pop.gmail.com)",
43                                                 995,
44                                                 ConnectionSecurity.Ssl,
45                                                 validationDelegate))
46            {
47                // Connect to mail server
48                pop.Connect();
49                Console.WriteLine("Connected.");
50
51                // Authenticate with specified username,
52                // password and authentication mechanism
53                pop.Authenticate("<USERNAME>", "<PASSWORD>", PopAuthentication.Plain);
54                Console.WriteLine("Authenticated.");
55            }
56        }
57    }
58}
1Imports System.Net.Security
2Imports System.Security.Cryptography.X509Certificates
3Imports GemBox.Email
4Imports GemBox.Email.Pop
5Imports GemBox.Email.Security
6
7Namespace Samples
8    Module Sample
9
10        Sub Main()
11
12            ' If using Professional version, put your serial key below.
13            ComponentInfo.SetLicense("FREE-LIMITED-KEY")
14
15            ' Define certificate validation delegate which will ignore
16            ' 'Certificate name mismatch' errors
17            Dim validationDelegate As RemoteCertificateValidationCallback =
18                Function(sender As Object,
19                         certificate As X509Certificate,
20                         chain As X509Chain,
21                         sslPolicyErrors As SslPolicyErrors) As Boolean
22
23                    If sslPolicyErrors = SslPolicyErrors.None OrElse
24                       sslPolicyErrors = SslPolicyErrors.RemoteCertificateNameMismatch Then
25                        Console.WriteLine("Server certificate is valid.")
26                        Return True
27                    Else
28                        Console.WriteLine("Server certificate is invalid. Errors: " &
29                                          sslPolicyErrors.ToString())
30                        Return False
31                    End If
32                End Function
33
34            ' Create new PopClient and specify IP port,
35            ' security type And certificate validation callback
36            Using pop As New PopClient("<ADDRESS> (e.g. pop.gmail.com)",
37                                       995,
38                                       ConnectionSecurity.Ssl,
39                                       validationDelegate)
40
41                ' Connect to mail server
42                pop.Connect()
43                Console.WriteLine("Connected.")
44
45                ' Authenticate with specified username,
46                ' password And authentication mechanism
47                pop.Authenticate("<USERNAME>", "<PASSWORD>", PopAuthentication.Plain)
48                Console.WriteLine("Authenticated.")
49            End Using
50
51        End Sub
52
53    End Module
54End Namespace

Check next sample or find out more about GemBox.Email and GemBox Software.