SmimeSignAndEncrypt Method (MailMessage, Certificate, CertificateCollection) |
Namespace: MailBee.Security
public MailMessage SignAndEncrypt( MailMessage message, Certificate signingCert, CertificateCollection encryptionCerts )
Exception | 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. |
See Sign(MailMessage, Certificate) and Encrypt(MailMessage, CertificateCollection) topics for details on signing and encryption process.
If you have the signing certificate as an X509Certificate2 object, you can use Certificate(X509Certificate2) constructor to create Certificate object from it.
If you have the encryption certificates as an X509Certificate2Collection object, you can use FromX509Certificate2Collection(X509Certificate2Collection) method to create CertificateCollection object from it.
// 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); }