DomainKeysSign Method (MailMessage, String, String, Boolean, String)
Signs a mail message with DomainKeys and DKIM signatures.

Namespace: MailBee.Security
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 11.2.0 build 590 for .NET 4.5
Syntax
public MailMessage Sign(
	MailMessage msg,
	string[] headersToSign,
	string privateKeyStr,
	bool isFilename,
	string selector
)

Parameters

msg
Type: MailBee.MimeMailMessage
The mail message to be signed.
headersToSign
Type: SystemString
The array of the names of the headers to be included in the signature, or a null reference (Nothing in Visual Basic) if all the message headers should be included in the signature.
privateKeyStr
Type: SystemString
The contents or the filename of the DomainKeys/DKIM private key.
isFilename
Type: SystemBoolean
If true, privateKeyStr denotes the private key filename; otherwise, the contents.
selector
Type: SystemString
The prefix of the sub-domain serving DomainKeys/DKIM for the sender domain.

Return Value

Type: MailMessage
The signed mail message.
Exceptions
ExceptionCondition
MailBeeInvalidArgumentExceptionmsg or privateKeyStr or selector is a null reference (Nothing in Visual Basic) or the message to be signed does not include the header specifying the sender (Sender or From header) or headersToSign array (if not null) does not list the name of that header or the sender's e-mail address domain is empty.
MailBeeExceptionAn error occurred and ThrowExceptions is true.
Remarks

To sign a message with DomainKeys/DKIM signatures, you must have a private key (for instance, you can create it with OpenSSL utility).

The private key string (of file contents) looks like: -----BEGIN RSA PRIVATE KEY-----Base64-encoded key-----END RSA PRIVATE KEY-----. At least 1024-bit key length is recommended.

The message to be signed must have Sender or From header and the e-mail address in that header must contain non-empty domain part. You can include all the headers into the signature (passing a null reference as headersToSign value) so that any change in the headers the message originally had will make the signature invalid. Alternatively, you may include only certain headers into the signature (at least Sender or From) so that other headers can be changed. This is useful if the signature should not become invalid if you change some headers after the message has been signed. Typical case is when the mail server alters the headers of the signed message (changes Date, Message-ID or other headers).

Note Note
Be sure to enable FipsMode if the current system is FIPS-compliant.
Examples
This console sample composes a new mail message, signs it with DomainKeys/DKIM signatures (DomainKeys private key is taken from a file) and sends it out.
Note Note
See Sign(MailMessage, String, String, Boolean, String) example if you need to sign a message which may already contain Date or Message-ID headers which can be modified after the message gets sent (that sample contains alternative approach to the one listed in Remarks section).
using System;
using MailBee;
using MailBee.Mime;
using MailBee.Security;
using MailBee.SmtpMail;

class Sample
{
    static void Main(string[] args)
    {
        // Compose a message.
        MailMessage msg = new MailMessage();
        msg.From.Email = "from@domain1.com";
        msg.To.Add("to@domain2.com");
        msg.Subject = "Hello";
        msg.BodyPlainText = "Hello, World!";

        // We will send the message with this object.
        Smtp mailer = new Smtp();

        // Sign the message and assign the signed message for sending.
        DomainKeys dk = new DomainKeys();
        mailer.Message = dk.Sign(msg, null, @"C:\Temp\rsa512.private", true, "dk");

        // Send the message via SMTP server (authentication is used in this sample).
        mailer.SmtpServers.Add("mail.domain1.com", "from@domain1.com", "secret");
        mailer.Send();
    }
}
See Also