SmimeEncrypt Method
Encrypts an e-mail message.

Namespace: MailBee.Security
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 11.2.0 build 590 for .NET 4.5
Syntax
public MailMessage Encrypt(
	MailMessage message,
	CertificateCollection encryptionCerts
)

Parameters

message
Type: MailBee.MimeMailMessage
The original message which should be encrypted.
encryptionCerts
Type: MailBee.SecurityCertificateCollection
The collection of public certificates of all the recipients of the message.

Return Value

Type: MailMessage
A reference to the encrypted message if the encryption went successfully; a reference to the original message if it was already encrypted; a null reference (Nothing in Visual Basic) if the encryption failed.
Exceptions
ExceptionCondition
MailBeeInvalidArgumentExceptionmessage is a null reference (Nothing in Visual Basic) or encryptionCerts is a null reference or an empty collection.
MailBeeSmimeWin32ExceptionAn error occurred and ThrowExceptions is true.
Remarks

This method encrypts the specified message using EncryptionAlgorithm and public keys of all the certificates in encryptionCerts collection. If the e-mail message contains multiple recipients, this collection should contain all their certificates. Otherwise, those recipients which have no their certificates listed in encryptionCerts will not be able to decrypt the message on their end.

Thus, to encrypt a message, you need to have public certificates of its recipients. They can be stored in a file, in Windows registry, etc. You can check S/MIME Demo projects shipped with MailBee on how to open certificate stores and find out recipient certificates based on the recipient list of the message (which can be obtained with GetAllRecipients method).

The developer can also use SignAndEncrypt(MailMessage, Certificate, CertificateCollection) method to encrypt a message and sign it with a digital signature in a single method call.

Examples
The following sample loads the message from disk, encrypts it with the recipient's certificate, and then saves it back to disk.
// 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\original.eml");

Smime objSmime = new Smime();

try
{
    // Open the system certificate store which contains the recipients certificates.
    CertificateStore store = new CertificateStore(CertificateStore.OtherPeople, CertStoreType.System, null);
    // Search the recipient by e-mail.
    CertificateCollection encryptionCerts = store.FindCertificates("user1@domain.com", CertificateFields.EmailAddress);
    // Encrypt the message.
    MailMessage encMsg = objSmime.Encrypt(msg, encryptionCerts);
    // Save the encrypted message to the disk file.
    encMsg.SaveMessage(@"C:\Temp\encrypted.eml");
    Console.WriteLine("Done.");
}
catch (MailBeeException ex)
{
    Console.WriteLine(ex.Message);
}
See Also