Smime Class
Provides properties and methods for encrypting, decrypting, signing and verifying e-mail messages.
Inheritance Hierarchy
SystemObject
  MailBee.SecuritySmime

Namespace: MailBee.Security
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 12.4 build 677 for .NET 4.5
Syntax
public class Smime

The Smime type exposes the following members.

Constructors
  NameDescription
Public methodSmime
Creates an instance of Smime class.
Public methodSmime(String)
Creates and unlocks an instance of Smime class.
Top
Methods
  NameDescription
Public methodCode exampleDecrypt(MailMessage)
Decrypts an e-mail message if it's encrypted.
Public methodCode exampleDecrypt(MailMessage, CertificateStore)
Decrypts an e-mail message if it's encrypted.
Public methodDecrypt2
Decrypts an e-mail message if it's encrypted.
Public methodCode exampleDecryptAndVerify(MailMessage, MessageVerificationFlags)
Decrypts an e-mail message if it's encrypted and verifies its signature if it's signed.
Public methodCode exampleDecryptAndVerify(MailMessage, MessageVerificationFlags, CertificateStore, CertificateStore)
Decrypts an e-mail message if it's encrypted and verifies its signature if it's signed.
Public methodDecryptAndVerify2
Decrypts an e-mail message if it's encrypted and verifies its signature if it's signed.
Public methodCode exampleEncrypt
Encrypts an e-mail message.
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodCode exampleResetToDefaults
Resets S/MIME settings to default values.
Public methodCode exampleSign
Signs an e-mail message with a digital signature.
Public methodCode exampleSignAndEncrypt
Signs and encrypts an e-mail message.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Public methodCode exampleVerify
Verifies if the signature of the specified e-mail message is valid.
Public methodCode exampleVerify2
Verifies if the signature of the specified e-mail message is valid.
Top
Properties
  NameDescription
Public propertyCode exampleEncryptionAlgorithm
Gets or sets the algorithm to use for encrypting messages.
Public propertyCode exampleHashAlgorithm
Gets or sets the hash algorithm to use for signing messages with a digital signature.
Public propertyIncludeWholeChainOnSign
Gets or sets if Sign(MailMessage, Certificate) or SignAndEncrypt(MailMessage, Certificate, CertificateCollection) should include the whole certificate chain into the signature.
Public propertyLastResult
Gets a numeric code of the last error.
Public propertyCode exampleProvider
Gets or sets the CSP to be used for cryptographic operations.
Public propertySetEnvelopedCmsOnDecrypt
Public propertySetSignedCmsOnVerify
Public propertyThrowExceptions
Gets or sets whether the object will throw exceptions on errors.
Public propertyTimestampOnSign
Top
Remarks
Using this class, you can perform the following operations with e-mail messages:
  • Encrypt or decrypt e-mails
  • Sign e-mails with a digital signature
  • Verify signatures of e-mails

The class by default uses FIPS compliant algorithms: RSA DES for encryption and SHA1 for signing (although you can set other algorithms like AES-256 for encryption with and SHA256 for signing with EncryptionAlgorithm and HashAlgorithm properties).

Note Note
To use this class, make sure MailBee.NET Security is licensed (MailBee.NET Objects full bundle does include the license).
Note Note
The methods of this class work a bit differently in different editions of MailBee.NET Objects. .NET Standard 2.0 version is based on EnvelopedCms class but it's not available in .NET Core 1.0/1.1 (where Win32 Interop is used instead). .NET Framework edition supports both EnvelopedCms way of doing things and Win32 Interop. UWP version prior to Windows 10 Fall Creators update doesn't support Smime class at all.

In .NET Standard 2.0 and higher (including .NET Core 2.0 and higher), use methods which have "2" suffix in their name. This includes Decrypt2(MailMessage, X509Certificate2Collection), Verify2(MailMessage, MessageVerificationFlags, X509Certificate2Collection), DecryptAndVerify2(MailMessage, MessageVerificationFlags, X509Certificate2Collection, X509Certificate2Collection).

Examples
This sample retrieves the last e-mail from the specified POP3 account, decrypts it if it's encrypted and verifies its signature if any. Then, it prints the plain-text version of the message to the console.
// To use the code below, import these namespaces at the top of your code
using System;
using MailBee;
using MailBee.Pop3Mail;
using MailBee.Mime;
using MailBee.Security;

Smime objSmime = new Smime();
Pop3 pop = new Pop3();
pop.Connect("server", 110, true);
pop.Login("login", "password");
MailMessage msg = pop.DownloadEntireMessage(pop.InboxMessageCount);
pop.Disconnect();

try
{
    if (msg.IsEncrypted)
    {
        SmimeResult smResult = objSmime.DecryptAndVerify(msg, MessageVerificationFlags.All);
        if (smResult.VerificationResult > 0)
        {
            Console.WriteLine("Verification failed");
        }
        else if (smResult.DecryptedMessage != null)
        {
            Console.WriteLine(smResult.DecryptedMessage.BodyPlainText);
        }
    }
}
catch (MailBeeException ex)
{
    Console.WriteLine(ex.Message);
}
See Also