SmimeDecrypt Method (MailMessage) |
Namespace: MailBee.Security
Exception | Condition |
---|---|
MailBeeInvalidArgumentException | message is a null reference (Nothing in Visual Basic). |
MailBeeCertificateStoreWin32Exception | A WinAPI error occurred during opening Personal certificate store and ThrowExceptions is true. |
MailBeeSmimeWin32Exception | A WinAPI error occurred while performing S/MIME operation and ThrowExceptions is true. |
MailBeeCertificateException | A certificate error occurred ThrowExceptions is true. Typically, if the error message is "Keyset does not exist", indicates that there is no private key in the certicate required for decryption. |
This method looks for the appropriate certificate for decryption in the standard system certificate store named "MY" (Personal). The certificate must contain a private key. In the case if the required certificate resides in another store, use Decrypt(MailMessage, CertificateStore) overload.
To access the decrypted message, use DecryptedMessage property of the returned SmimeResult object. To get the certificate used for decryption, use DecryptionCertificate property of the same object.
To check if the original message was encrypted, examine IsEncrypted property value of the original MailMessage object.
To decrypt and verify a message within a single method call, use DecryptAndVerify(MailMessage, MessageVerificationFlags) method or its overloads.
// 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\encrypted.eml"); Smime objSmime = new Smime(); try { // Decrypt the message. SmimeResult sResult = objSmime.Decrypt(msg); // Display the content of decrypted message. if (sResult.DecryptedMessage != null) { Console.WriteLine(sResult.DecryptedMessage.BodyPlainText); } if (sResult.DecryptionCertificate != null) { Console.WriteLine(sResult.DecryptionCertificate.Subject); } } catch (MailBeeException ex) { Console.WriteLine(ex.Message); }