Dart.Mail Namespace > MailMessage Class > SecureEncrypt Method : SecureEncrypt(X509Certificate2Collection,EncryptingAlgorithm,Boolean) Method |
Public Overloads Sub SecureEncrypt( _ ByVal encryptingCertificates As X509Certificate2Collection, _ ByVal encryptingAlgorithm As EncryptingAlgorithm, _ ByVal includeHeaders As Boolean _ )
Dim instance As MailMessage Dim encryptingCertificates As X509Certificate2Collection Dim encryptingAlgorithm As EncryptingAlgorithm Dim includeHeaders As Boolean instance.SecureEncrypt(encryptingCertificates, encryptingAlgorithm, includeHeaders)
public void SecureEncrypt( X509Certificate2Collection encryptingCertificates, EncryptingAlgorithm encryptingAlgorithm, bool includeHeaders )
public: void SecureEncrypt( X509Certificate2Collection* encryptingCertificates, EncryptingAlgorithm encryptingAlgorithm, bool includeHeaders )
public: void SecureEncrypt( X509Certificate2Collection^ encryptingCertificates, EncryptingAlgorithm encryptingAlgorithm, bool includeHeaders )
Exception | Description |
---|---|
System.InvalidOperationException | Certificates matching all recipients not found. |
The MailMessage is encrypted using the public keys of certificates associated with each recipient (To, Cc and Bcc. If encryptingCertificates is empty, it is populated with the certificates used to encrypt the MailMessage (certificates that correspond to each recipient, retrieved from the current user's "AddressBook" certificate store). Encryption is performed for each recipient, so the resulting encrypted MailMessage increases in size as the number of recipients increases.
If includeHeaders is false, the content is encrypted and the message headers are unchanged except for ContentType. If true, the entire message is encrypted and will be restored by the reader during decryption (this was introduced in S/MIME version 3.1 and is not backwards compatible). In this case, sensitive header fields like Subject:, To:, From: and CC: may be removed by the user after encrypting. If the To:, From:, CC:, or BCC: header fields are removed, Smtp.Send or Send(Stream,String,String) must be used.
If successful, the MailMessage is modified and IsSecure returns true. If unsuccessful, the MailMessage is unmodified and an Exception is thrown.
using System.Security.Cryptography.X509Certificates; private MailMessage getEncryptedMessage(MailMessage message) { //Find the encrypting certificate in the CurrentUser/AddressBook store //The following code results in the same encrypted message as "message.SecureEncrypt();" X509Certificate2Collection encryptingCertificates = new X509Certificate2Collection(); X509Store addressBookStore = new X509Store(StoreName.AddressBook, StoreLocation.CurrentUser); addressBookStore.Open(OpenFlags.ReadOnly); foreach (X509Certificate2 certificate in addressBookStore.Certificates) { if (certificate.Subject.Contains("E=" + message.To)) { encryptingCertificates.Add(certificate); //Encrypt the message message.SecureEncrypt(encryptingCertificates, EncryptingAlgorithm.TripleDes, false); return message; } } return null; }
Imports System.Security.Cryptography.X509Certificates Private Function getEncryptedMessage(ByVal message As MailMessage) As MailMessage 'Find the encrypting certificate in the CurrentUser/AddressBook store 'The following code results in the same encrypted message as "message.SecureEncrypt();" Dim encryptingCertificates As New X509Certificate2Collection() Dim addressBookStore As New X509Store(StoreName.AddressBook, StoreLocation.CurrentUser) addressBookStore.Open(OpenFlags.ReadOnly) For Each certificate As X509Certificate2 In addressBookStore.Certificates If certificate.Subject.Contains("E=" & message.To) Then encryptingCertificates.Add(certificate) 'Encrypt the message message.SecureEncrypt(encryptingCertificates, EncryptingAlgorithm.TripleDes, False) Return message End If Next certificate Return Nothing End Function