SmimeSign Method |
Namespace: MailBee.Security
Exception | Condition |
---|---|
MailBeeInvalidArgumentException | message or signingCert is a null reference (Nothing in Visual Basic). |
MailBeeSmimeWin32Exception | An 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.
To create a signature, MailBee uses HashAlgorithm and the private key of signingCert to calculate a hash of the message data.
The developer can also use the SignAndEncrypt(MailMessage, Certificate, CertificateCollection) method to sign and encrypt a message within a single method call.
// 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); }