Encrypts an e-mail message.
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.
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 method to encrypt a message and sign it with a digital signature in a single method call.
| Exception Type | Condition |
|---|---|
| MailBeeInvalidArgumentException | message is a null reference (Nothing in Visual Basic) or encryptionCerts is a null reference or an empty collection. |
| MailBeeSmimeWin32Exception | An error occurred and ThrowExceptions is true. |
The following sample loads the message from disk, encrypts it with the recipient's certificate, and then saves it back to disk.
[C#] // 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); }
[Visual Basic] ' To use the code below, import these namespaces at the top of your code Imports MailBee Imports MailBee.Mime Imports MailBee.Security ' The actual code (put it into a method of your class) ' Load the message from file. Dim msg As MailMessage = New MailMessage msg.LoadMessage("C:\Temp\original.eml") Dim objSmime As Smime = New Smime Try ' Open the system certificate store which contains the recipients certificates. Dim store As CertificateStore = New CertificateStore(CertificateStore.OtherPeople, CertStoreType.System, Nothing) ' Search the recipient by e-mail. Dim encryptionCerts As CertificateCollection = store.FindCertificates("user1@domain.com", CertificateFields.EmailAddress) ' Encrypt the message. Dim encMsg As MailMessage = objSmime.Encrypt(msg, encryptionCerts) ' Save the encrypted message to disk. encMsg.SaveMessage("C:\Temp\encrypted.eml") Console.WriteLine("Done.") Catch ex As MailBeeException Console.WriteLine(ex.Message) End Try
Smime Class | MailBee.Security Namespace | SignAndEncrypt