SmimeDecrypt Method (MailMessage, CertificateStore)
Decrypts an e-mail message if it's encrypted.

Namespace: MailBee.Security
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 12.5.0 build 687 for .NET 4.5
Syntax
public SmimeResult Decrypt(
	MailMessage message,
	CertificateStore[] stores
)

Parameters

message
Type: MailBee.MimeMailMessage
The original encrypted message to be decrypted.
stores
Type: MailBee.SecurityCertificateStore
The array of the certificate stores to be searched for the appropriate certificate for decrypting the message, or a null reference (Nothing in Visual Basic) if Personal system storage should be used.

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.
Exceptions
ExceptionCondition
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.
MailBeeCertificateExceptionA certificate error occurred ThrowExceptions is true. Typically, if the error message is "Keyset does not exist", indicates that there is no private key in the certicate required for decryption.
Remarks

This method looks for the appropriate certificate for decryption in the specified certificate stores. The certificate must contain a private key.

To access the decrypted message, use DecryptedMessage property of the returned SmimeResult object. To get the certificate used for decryption, use DecryptionCertificate property of the same object.

To check if the original message was encrypted, examine IsEncrypted property value of the original MailMessage object.

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

Note Note
This method is not available in .NET Standard 2.0 and newer (because it relies on Win32 API). Use Decrypt2(MailMessage, X509Certificate2Collection) instead.
Examples
This sample loads the message from the disk file, decrypts it, and displays its contents.
// 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();
msg.LoadMessage(@"C:\Temp\encrypted_only.eml");

Smime objSmime = new Smime();

try
{
    CertificateStore[] stores = new CertificateStore[1];
    stores[0] = new CertificateStore(CertificateStore.Personal, CertStoreType.System, null);

    // Decrypt the message.
    SmimeResult sResult = objSmime.Decrypt(msg, stores);

    // Display the content of the message.
    if (sResult.DecryptedMessage != null)
    {
        Console.WriteLine(sResult.DecryptedMessage.BodyPlainText);
    }
    if (sResult.DecryptionCertificate != null)
    {
        Console.WriteLine(sResult.DecryptionCertificate.Subject);
    }
}
catch (MailBeeException ex)
{
    Console.WriteLine(ex.Message);
}
See Also