PowerTCP Mail for .NET
SecureEncrypt(X509Certificate2Collection,EncryptingAlgorithm,Boolean) Method
Example 




In/Out. Optional System.Security.Cryptography.X509Certificates.X509Certificate2Collection used to override use of certificates from the user's "AddressBook" certificate store. If empty, populated with the certificates used. Can be null.
The encryption algorithm to use.
If true, message header fields are moved into the encrypted payload.
Encrypt the MailMessage using S/MIME encoding.
Syntax
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)

Parameters

encryptingCertificates
In/Out. Optional System.Security.Cryptography.X509Certificates.X509Certificate2Collection used to override use of certificates from the user's "AddressBook" certificate store. If empty, populated with the certificates used. Can be null.
encryptingAlgorithm
The encryption algorithm to use.
includeHeaders
If true, message header fields are moved into the encrypted payload.
Exceptions
ExceptionDescription
System.InvalidOperationExceptionCertificates matching all recipients not found.
Remarks

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.

Example
This example demonstrates encrypting a message using the recipient's certificate.
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
See Also

Reference

MailMessage Class
MailMessage Members
Overload List


PowerTCP Mail for .NET Documentation Version 4.3
© 2018 Dart Communications. All Rights Reserved.
Send comments on this topic