SmimeVerify2 Method |
Namespace: MailBee.Security
public SmimeResult Verify2( MailMessage message, MessageVerificationFlags flags, X509Certificate2Collection extraCerts )
Exception | Condition |
---|---|
MailBeeInvalidArgumentException | message is a null reference (Nothing in Visual Basic). |
If the message has so-called "attached" signature, you'll need to check DecryptedMessage of the returned value of this method in order to get access to the original (unsigned) message. This is because messages with attached signatures look like encrypted messages so it's required to decode such a message (like in decryption procedure) to get access to the original message. Therefore checking the signature may also extract the original message from it. If DecryptedMessage is not set (null reference) in the returned value of this method, this means the message has so-called "detached" signature which is not encapsulated into the original message contents but simply added as attachment. In this case, message input parameter already refers to the original message and there is no need to extract anything.
extraCerts usually needs to be set if the system default store lacks a certification authority which issued the certificate the message is signed with. This is common case for ASP.NET web applications because ASP.NET user has fewer certification authorities in its system default store than regular Windows users.
To check the message verification result, examine VerificationResult property of the returned SmimeResult object. To access the signature certificate, use SignatureCertificate property of the same object.
To check if the original message had a digital signature, examine IsSigned property value of the original MailMessage object.
To decrypt and verify a message within a single method call, use DecryptAndVerify2(MailMessage, MessageVerificationFlags, X509Certificate2Collection, X509Certificate2Collection) method or its overloads.
Note |
---|
This method can be slow in case if the certificate refers to a non-existing domain name (or local domain name while the current machine is not in that network). |
This method is a version of Verify(MailMessage, MessageVerificationFlags, CertificateStore) which accepts X509Certificate2Collection rather than CertificateStore. Starting from .NET Standard 2.0, you must use this method as CertificateStore functionality has been reduced to avoid dependency on Win32 API.
Note |
---|
This method is not available in .NET Сore 1.0/1.1. On that platform, use Verify(MailMessage, MessageVerificationFlags, CertificateStore) instead. |
// 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\signed_only.eml"); Smime objSmime = new Smime(); try { // Verify the message. SmimeResult smResult = objSmime.Verify2(msg, MessageVerificationFlags.All, null); MessageVerificationFlags resultOptions = smResult.VerificationResult; // Check for the errors. if (resultOptions != MessageVerificationFlags.None) { if ((resultOptions & MessageVerificationFlags.CertificateRevoked) == MessageVerificationFlags.CertificateRevoked) { Console.WriteLine("Error! Certificate revoked..."); } if ((resultOptions & MessageVerificationFlags.MessageTampered) == MessageVerificationFlags.MessageTampered) { Console.WriteLine("Error! Message has been tampered..."); } if ((resultOptions & MessageVerificationFlags.SignatureExpired) == MessageVerificationFlags.SignatureExpired) { Console.WriteLine("Error! Signature expired..."); } if ((resultOptions & MessageVerificationFlags.SignerAndSenderDoNotMatch) == MessageVerificationFlags.SignerAndSenderDoNotMatch) { Console.WriteLine("Error! Signer and sender do not match..."); } if ((resultOptions & MessageVerificationFlags.Untrusted) == MessageVerificationFlags.Untrusted) { Console.WriteLine("Error! Untrusted certificate..."); } } if (smResult.SignatureCertificate != null) { Console.WriteLine(smResult.SignatureCertificate.Subject); } } catch (MailBeeException ex) { Console.WriteLine(ex.Message); }