MailBee.NET Objects 4.0

Smime.Verify Method 

Verifies if the signature of the specified e-mail message is valid.

public SmimeResult Verify(
   MailMessage message,
   MessageVerificationFlags flags,
   CertificateStore extraStore
);

Parameters

message
The message to be verified.
flags
A set of flags which specify the verification criteria.
extraStore
A reference to the certificate store containing additional certificates of the certification authorities, or a null reference (Nothing in Visual Basic) if only the system default certification authorities should be used for validating the certificate of the message signature.

Return Value

A reference to SmimeResult object containing either a bitwise combination of MessageVerificationFlags indicating which conditions of flags criteria have not been passed the verification or None if the verification completed successfully or the message was not signed.

Remarks

extraStore usually needs to be set if the system default store lacks a certification authority which issued the certificate the message is signed with. This is common case for ASP.NET web applications because ASP.NET user has fewer certification authorities in its system default store than regular Windows users. See ASP.NET S/MIME Demo sample project shipped with MailBee for details.

To check the message verification result, examine VerificationResult property of the returned SmimeResult object. To access the signature certificate, use SignatureCertificate property of the same object.

To check if the original message had a digital signature, examine IsSigned property value of the original MailMessage object.

To decrypt and verify a message within a single method call, use DecryptAndVerify method or its overloads.

Exceptions

Exception TypeCondition
MailBeeInvalidArgumentExceptionmessage is a null reference (Nothing in Visual Basic).

Example

This sample verifies the message signature using all the available criteria.

[C#]
// To use the code below, import these namespace at the top of your code
using System;
using MailBee;
using MailBee.Mime;
using MailBee.Security;

// The actual code (put it into a method of your class)

// Load the message from file.
MailMessage msg = new MailMessage();
msg.LoadMessage(@"C:\Temp\signed_only.eml");

Smime objSmime = new Smime();

try
{
    // Verify the message.
    SmimeResult smResult = objSmime.Verify(msg, MessageVerificationFlags.All, null);
    MessageVerificationFlags resultOptions = smResult.VerificationResult;
    // Check for the errors.
    if (resultOptions != MessageVerificationFlags.None)
    {
        if ((resultOptions & MessageVerificationFlags.CertificateRevoked) == MessageVerificationFlags.CertificateRevoked)
        {
            Console.WriteLine("Error! Certificate revoked...");
        }
        if ((resultOptions & MessageVerificationFlags.MessageTampered) == MessageVerificationFlags.MessageTampered)
        {
            Console.WriteLine("Error! Message has been tampered...");
        }
        if ((resultOptions & MessageVerificationFlags.SignatureExpired) == MessageVerificationFlags.SignatureExpired)
        {
            Console.WriteLine("Error! Signature expired...");
        }
        if ((resultOptions & MessageVerificationFlags.SignerAndSenderDoNotMatch) == MessageVerificationFlags.SignerAndSenderDoNotMatch)
        {
            Console.WriteLine("Error! Signer and sender do not match...");
        }
        if ((resultOptions & MessageVerificationFlags.Untrusted) == MessageVerificationFlags.Untrusted)
        {
            Console.WriteLine("Error! Untrusted certificate...");
        }
    }
    if (smResult.SignatureCertificate != null)
    {
        Console.WriteLine(smResult.SignatureCertificate.Subject);
    }
}
catch (MailBeeException ex)
{
    Console.WriteLine(ex.Message);
}
[Visual Basic]
' To use the code below, import MailBee namespace at the top of your code
Imports MailBee
Imports MailBee.Mime
Imports MailBee.Security

' The actual code (put it into a method of your class)

' Load the message from file.
Dim msg As MailMessage = New MailMessage
msg.LoadMessage("C:\Temp\signed_only.eml")

Dim objSmime As Smime = New Smime

Try
    ' Verify the message.
    Dim smResult As SmimeResult = objSmime.Verify(msg, MessageVerificationFlags.All, Nothing)
    Dim resultOptions As MessageVerificationFlags = smResult.VerificationResult
    ' Check for the errors.
    If (resultOptions <> MessageVerificationFlags.None) Then
        If ((resultOptions & MessageVerificationFlags.CertificateRevoked) = MessageVerificationFlags.CertificateRevoked) Then
            Console.WriteLine("Error! Certificate revoked...")
        End If
        If ((resultOptions & MessageVerificationFlags.MessageTampered) = MessageVerificationFlags.MessageTampered) Then
            Console.WriteLine("Error! Message has been tampered...")
        End If
        If ((resultOptions & MessageVerificationFlags.SignatureExpired) = MessageVerificationFlags.SignatureExpired) Then
            Console.WriteLine("Error! Signature expired...")
        End If
        If ((resultOptions & MessageVerificationFlags.SignerAndSenderDoNotMatch) = MessageVerificationFlags.SignerAndSenderDoNotMatch) Then
            Console.WriteLine("Error! Signer and sender do not match...")
        End If
        If ((resultOptions & MessageVerificationFlags.Untrusted) = MessageVerificationFlags.Untrusted) Then
            Console.WriteLine("Error! Untrusted certificate...")
        End If
    End If
    If (Not IsNothing(smResult.SignatureCertificate)) Then
        Console.WriteLine(smResult.SignatureCertificate.Subject)
    End If
Catch ex As MailBeeException
    Console.WriteLine(ex.Message)
End Try

See Also

Smime Class | MailBee.Security Namespace | DecryptAndVerify