PowerTCP Mail for .NET
SecureSign() Method
Example 




Sign the MailMessage using S/MIME encoding default values.
Syntax
Public Overloads Function SecureSign() As X509Certificate2Collection
Dim instance As MailMessage
Dim value As X509Certificate2Collection
 
value = instance.SecureSign()
public X509Certificate2Collection SecureSign()
public: X509Certificate2Collection* SecureSign(); 
public:
X509Certificate2Collection^ SecureSign(); 

Return Value

System.Security.Cryptography.X509Certificates.X509Certificate2Collection containing the certificates included within the signed message.
Remarks

From is used to locate a signing certificate in the "MY" certificate store and X509IncludeOption.ExcludeRoot used. DigestAlgorithm.Sha256 is used for creating the message hash. A "detached" digital signature is created and included as an attachment.

Use SecureSign(X509Certificate2,X509IncludeOption,DigestAlgorithm,Boolean,Boolean) if additional control is desired.

Example
This example demonstrates signing a message with the sender's digitial signature.
using System.Security.Cryptography.X509Certificates;

private MailMessage getSignedMessage(MailMessage message)
{
    //Find the signing certificate in the "CurrentUser/My" certificate store
    //The following code results in the same signed message as "message.SecureSign();"
    X509Store myPersonalStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
    myPersonalStore.Open(OpenFlags.ReadOnly);
    foreach (X509Certificate2 certificate in myPersonalStore.Certificates)
    {
        if (certificate.Subject.Contains("E=" + message.From.ToString()))
        {
            //Sign the message
            message.SecureSign(certificate, X509IncludeOption.ExcludeRoot, DigestAlgorithm.Sha1, true, false);
            return message;
        }
    }
    return null;
}
Imports System.Security.Cryptography.X509Certificates

Private Function getSignedMessage(ByVal message As MailMessage) As MailMessage
    'Find the signing certificate in the "CurrentUser/My" certificate store
    'The following code results in the same signed message as "message.SecureSign();"
    Dim myPersonalStore As New X509Store(StoreName.My, StoreLocation.CurrentUser)
    myPersonalStore.Open(OpenFlags.ReadOnly)
    For Each certificate As X509Certificate2 In myPersonalStore.Certificates
        If certificate.Subject.Contains("E=" & message.From.ToString()) Then
            'Sign the message
            message.SecureSign(certificate, X509IncludeOption.ExcludeRoot, DigestAlgorithm.Sha1, True, 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