SmimeSign Method (MailMessage, Certificate) |
Namespace: MailBee.Security
Exception | Condition |
---|---|
MailBeeInvalidArgumentException | message or signingCert is a null reference (Nothing in Visual Basic). |
MailBeeSmimeWin32Exception | A cryptographic error occurred and ThrowExceptions is true (.NET Core 1.0/1.1 edition). |
MailBeeCertificateException | A cryptographic error occurred and ThrowExceptions is true. |
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.
// 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); }