Signs and encrypts an e-mail message.
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.
See Sign and Encrypt topics for details on signing and encryption process.
| Exception Type | Condition |
|---|---|
| MailBeeInvalidArgumentException | message or signingCert 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. |
This sample signs and encrypts a message.
[C#] // 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); }
[Visual Basic] ' To use the code below, import MailBee namespace 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 ' Search for the sender certificate in the system certificate store. Dim myStore As CertificateStore = New CertificateStore(CertificateStore.Personal, CertStoreType.System, Nothing) Dim signingCerts As CertificateCollection = myStore.FindCertificates("user@afterlogic.com", CertificateFields.EmailAddress) Dim signingCert As Certificate = Nothing If (signingCerts.Count > 0) Then signingCert = signingCerts(0) End If ' Get the recipients certificates from the the system certificate store. Dim encryptionCerts As CertificateCollection = New CertificateStore(CertificateStore.OtherPeople, CertStoreType.System, Nothing).GetAllCertificates() ' Sign and encrypt the message. Dim signEncMsg As MailMessage = objSmime.SignAndEncrypt(msg, signingCert, encryptionCerts) ' Save the resulting message. signEncMsg.SaveMessage("C:\Temp\result.eml") Catch ex As MailBeeException Console.WriteLine(ex.Message) End Try
Smime Class | MailBee.Security Namespace | Sign | Encrypt