SmimeSignAndEncrypt Method (MailMessage, Certificate, CertificateCollection)
Signs and encrypts an e-mail message.

Namespace: MailBee.Security
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 12.5.0 build 687 for .NET 4.5
Syntax
public MailMessage SignAndEncrypt(
	MailMessage message,
	Certificate signingCert,
	CertificateCollection encryptionCerts
)

Parameters

message
Type: MailBee.MimeMailMessage
The original e-mail message to be signed and encrypted.
signingCert
Type: MailBee.SecurityCertificate
The certificate to be used for signing the message. This certificate must contain a private key.
encryptionCerts
Type: MailBee.SecurityCertificateCollection
The collection of public certificates of all the recipients of the message.

Return Value

Type: MailMessage
A reference to the signed and encrypted message if the signing and encryption went successfully; a reference to the original message if it was already signed and encrypted; a null reference (Nothing in Visual Basic) if the signing or encryption failed.
Exceptions
ExceptionCondition
MailBeeInvalidArgumentExceptionmessage or signingCert 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

See Sign(MailMessage, Certificate) and Encrypt(MailMessage, CertificateCollection) topics for details on signing and encryption process.

If you have the signing certificate as an X509Certificate2 object, you can use Certificate(X509Certificate2) constructor to create Certificate object from it.

If you have the encryption certificates as an X509Certificate2Collection object, you can use FromX509Certificate2Collection(X509Certificate2Collection) method to create CertificateCollection object from it.

Examples
This sample signs and encrypts a message.
// 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\original.eml");

Smime objSmime = new Smime();

try
{
    // Search for the sender certificate in the system certificate store.
    CertificateStore myStore = new CertificateStore(CertificateStore.Personal, CertStoreType.System, null);
    CertificateCollection signingCerts = myStore.FindCertificates("user@afterlogic.com", CertificateFields.EmailAddress);
    Certificate signingCert = null;
    if (signingCerts.Count > 0) signingCert = signingCerts[0];

    // Get the recipients certificates from the the system certificate store.
    CertificateCollection encryptionCerts = new CertificateStore(CertificateStore.OtherPeople, CertStoreType.System, null).GetAllCertificates();

    // Sign and encrypt the message.
    MailMessage signEncMsg = objSmime.SignAndEncrypt(msg, signingCert, encryptionCerts);

    // Save the resulting message.
    signEncMsg.SaveMessage(@"C:\Temp\result.eml");
}
catch (MailBeeException ex)
{
    Console.WriteLine(ex.Message);
}
See Also