Validate S/MIME emails

To check if the message has been signed use IsSigned property on IMail object.
CheckSignature(bool verifySignatureOnly) method is used for signature validation.

Using IMAP protocol
// C# 

using (Imap imap = new Imap())
{
    imap.Connect("imap.example.com"); // or ConnectSSL
    imap.UseBestLogin("user", "password");

    MailBuilder builder = new MailBuilder();
    foreach (long uid in imap.GetAll())
    {
        IMail email = builder.CreateFromEml(
            imap.GetMessageByUID(uid));

        // Check signature
        if (email.IsSigned == true)
            email.CheckSignature(true);
    }
    imap.Close();
}
' VB.NET

Using imap As New IMAP()
    imap.Connect("imap.example.com") ' or ConnectSSL
    imap.UseBestLogin("user", "password")

    Dim builder As New MailBuilder()
    For Each uid As Long In imap.GetAll()
        Dim email As IMail = builder.CreateFromEml( _
            imap.GetMessageByUID(uid))

        ' Check signature
        If email.IsSigned = True Then
            email.CheckSignature(True)
        End If
    Next
    imap.Close()
End Using

Using POP3 protocol

using (Pop3 pop3 = new Pop3())
{
    pop3.Connect("pop3.example.com"); // or ConnectSSL
    pop3.Login("user", "password");

    MailBuilder builder = new MailBuilder();
    foreach (string uid in pop3.GetAll())
    {
        IMail email = builder.CreateFromEml(
            pop3.GetMessageByUID(uid));

        // Check signature
        if (email.IsSigned == true)
            email.CheckSignature(true);
    }
    pop3.Close();
}
Using pop3 As New Pop3()
    pop3.Connect("pop3.example.com") ' or ConnectSSL
    pop3.Login("user", "password")

    Dim builder As New MailBuilder()
    For Each uid As String In pop3.GetAll()
        Dim email As IMail = builder.CreateFromEml( _
            pop3.GetMessageByUID(uid))

        ' Check signature
        If email.IsSigned = True Then
            email.CheckSignature(True)
        End If
    Next
    pop3.Close()
End Using

CheckSignature method will throw an exception if it fails to verify the signature.

Tags: , , , , ,

One Response to “Validate S/MIME emails”

  1. Send signed email receive encrypted Says:

    [...] Validate S/MIME emails [...]

Leave a Reply