SmimeDecryptAndVerify Method (MailMessage, MessageVerificationFlags)
Decrypts an e-mail message if it's encrypted and verifies its signature if it's signed.

Namespace: MailBee.Security
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 12.3.1 build 666 for .NET 4.5
public SmimeResult DecryptAndVerify(
	MailMessage message,
	MessageVerificationFlags flags


Type: MailBee.MimeMailMessage
The original message to be decrypted and verified.
Type: MailBee.SecurityMessageVerificationFlags
A set of flags which specify the verification criteria.

Return Value

Type: SmimeResult
A reference to SmimeResult object containing either the decrypted message or the original message if it was not encrypted; a null reference (Nothing in Visual Basic) if the decryption process failed.
MailBeeInvalidArgumentExceptionmessage is a null reference (Nothing in Visual Basic).
MailBeeCertificateStoreWin32ExceptionA WinAPI error occurred during opening Personal certificate store and ThrowExceptions is true.
MailBeeSmimeWin32ExceptionA WinAPI error occurred while performing S/MIME operation and ThrowExceptions is true.

The returned SmimeResult object provides access to the return values of this method, including DecryptedMessage, DecryptionCertificate, VerificationResult, and SignatureCertificate.

Thus, to access the decrypted message, use DecryptedMessage property; to get the certificate used for decryption, use DecryptionCertificate property.

To check the result of the message signature verification, examine VerificationResult property value. To access the signature certificate, use SignatureCertificate property.

See Decrypt(MailMessage) and Verify(MailMessage, MessageVerificationFlags, CertificateStore) topics for more details on decryption and verification process.

The sample loads the message from the disk file, decrypts and verifies it.
// To use the code below, import these namespaces 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();

Smime objSmime = new Smime();

    // Verify the message.
    SmimeResult smResult = objSmime.DecryptAndVerify(msg, MessageVerificationFlags.All);
    MessageVerificationFlags resultOptions = smResult.VerificationResult;
    // Check whether verification has been passed successfully.
    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)
    if (smResult.DecryptionCertificate != null)
    if (smResult.DecryptedMessage != null)
catch (MailBeeException ex)
See Also