DomainKeysSign Method (MailMessage, String, String, Boolean, String) |
Namespace: MailBee.Security
public MailMessage Sign( MailMessage msg, string[] headersToSign, string privateKeyStr, bool isFilename, string selector )
Exception | Condition |
---|---|
MailBeeInvalidArgumentException | msg 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. |
MailBeeException | An error occurred and ThrowExceptions is true. |
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).
If you want to sign empty headers as well (to avoid tampering them), set KeepEmptyHeaders property of Builder object to true.
To make MailBee automatically DK/DKIM sign all outgoing e-mail messages created from certain MailMessage instance, call SetDomainKeysSignOnSend(Boolean, Boolean, String, String, Boolean, String, DomainKeysTypes) method of Builder object.
Note |
---|
Be sure to enable FipsMode if the current system is FIPS-compliant. |
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(); } }