Dart.Mail Namespace > MailMessage Class > SecureEncrypt Method : SecureEncrypt() Method |
Public Overloads Sub SecureEncrypt()
Dim instance As MailMessage instance.SecureEncrypt()
public void SecureEncrypt()
public: void SecureEncrypt();
public: void SecureEncrypt();
The MailMessage is encrypted using EncryptingAlgorithm.TripleDes and the public keys of certificates associated with each recipient (To, Cc and Bcc). These certificates are populated from the "AddressBook" certificate store of the current user. Encryption is performed for each recipient, so the resulting encrypted MailMessage increases in size as the number of recipients increases. Headers are not encrypted.
Use SecureEncrypt(X509Certificate2Collection,EncryptingAlgorithm,Boolean) if additional control is desired.
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