PowerTCP Mail for .NET
SecureEncrypt() Method
Example 




Encrypt the MailMessage using S/MIME encoding default values.
Syntax
Public Overloads Sub SecureEncrypt() 
Dim instance As MailMessage
 
instance.SecureEncrypt()
public void SecureEncrypt()
public: void SecureEncrypt(); 
public:
void SecureEncrypt(); 
Remarks

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.

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