SmimeSign Method (MailMessage, Certificate)
Signs an e-mail message with a digital signature.

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

Parameters

message
Type: MailBee.MimeMailMessage
The original e-mail message to be signed.
signingCert
Type: MailBee.SecurityCertificate
The certificate to be used for signing the message. This certificate must contain a private key.

Return Value

Type: MailMessage
A reference to the signed message if the signing went successfully; a reference to the original message if it was already signed; a null reference (Nothing in Visual Basic) if the signing process failed.
Exceptions
ExceptionCondition
MailBeeInvalidArgumentExceptionmessage or signingCert is a null reference (Nothing in Visual Basic).
MailBeeSmimeWin32ExceptionA cryptographic error occurred and ThrowExceptions is true (.NET Core 1.0/1.1 edition).
MailBeeCertificateExceptionA cryptographic error occurred and ThrowExceptions is true.
Remarks

Make sure the Certificate used for signing has the same EmailAddress as Email value of MailMessage.From of the message being signed. Otherwise, the message will still be signed successfully but it might not be trusted by its recipients when it gets delivered.

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

To create a signature, MailBee uses HashAlgorithm and the private key of signingCert to calculate a hash of the message data.

To produce signatures trusted by Thunderbird, you need to force MailBee to use SHA-256 algorithm. The default algorithm (OS-dependent, usually SHA1) may not suffice. Use HashAlgorithm property for that.

The developer can also use the SignAndEncrypt(MailMessage, Certificate, CertificateCollection) method to sign and encrypt a message within a single method call.

If you're getting "System.Security.Cryptography.CryptographicException: the buffer supplied to a function was too small" on signing, you will have to set IncludeWholeChainOnSign to false.

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

Smime objSmime = new Smime();

try
{
    // Load certificate from the specified file.
    Certificate signingCert = new Certificate(@"C:\Docs\cert.pfx", CertFileType.Pfx, "secret");

    // Sign the message.
    MailMessage signMsg = objSmime.Sign(msg, signingCert);

    // Save the signed message to disk.
    signMsg.SaveMessage(@"C:\Docs\signed.eml");

    Console.WriteLine("Done.");
}
catch (MailBeeException ex)
{
    Console.WriteLine(ex.Message);
}
See Also