MailBee.NET Objects 7.2

MailMessage.DomainKeysSign Method 

Signs the current e-mail message with DKIM and classic DomainKeys signatures.

public void DomainKeysSign(
   bool isWebApp,
   string[] headersToSign,
   string privateKeyStr,
   bool isFilename,
   string selector,
   DomainKeysTypes dkTypes
);

Parameters

isWebApp
Must be true for ASP.NET applications, false otherwise.
headersToSign
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
The contents or the filename of the DomainKeys/DKIM private key.
isFilename
If true, privateKeyStr denotes the private key filename; otherwise, the contents.
selector
The prefix of the sub-domain serving DomainKeys/DKIM for the sender domain.
dkTypes
Specifies which signatures to create (classic DK, newer DKIM, or both).

Remarks

If you need more advanced level of DomainKeys/DKIM signing or additional information on private keys, certificates and so on, refer to DomainKeys class.

Moreover, in ideal case it should be your your mail server's job to sign outgoing e-mails with DomainKeys/DKIM signatures. Use MailBee only if you cannot enable this feature on your mail server.

Exceptions

Exception TypeCondition
MailBeeInvalidArgumentExceptionprivateKeyStr 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.

Example

This sample composes a simple e-mail, signs it with DomainKeys and DKIM signatures and sends it out. We assume the private certificate is in "C:\Temp\rsa512.private" file and the domain selector is "dk".

[C#]
using System;
using MailBee;
using MailBee.Mime;
using MailBee.SmtpMail;
using MailBee.Security;

class Sample
{
    static void Main(string[] args)
    {
        // Initialize mailer.
        Smtp mailer = new Smtp();

        // Set the message properties.
        mailer.Message.From.Email = "john.doe@company.com";
        mailer.Message.To.Add("jane.doe@example.com");
        mailer.Message.Subject = "Hello";
        mailer.Message.BodyPlainText = "Hello, Jane, can we meet today?";

        // Sign the message with DomainKeys and DKIM.
        mailer.Message.DomainKeysSign(false, null, @"C:\Temp\rsa512.private", true, "dk", DomainKeysTypes.Both);

        // Send the message via SMTP server (authentication is used in this sample).
        mailer.SmtpServers.Add("mail.company.com", "john.doe@company.com", "secret");
        mailer.Send();
    }
}
[Visual Basic]
Imports System
Imports MailBee
Imports MailBee.Mime
Imports MailBee.SmtpMail
Imports MailBee.Security

Class Sample
    Shared Sub Main(ByVal args() As String)
        ' Initialize mailer.
        Dim mailer As Smtp = New Smtp() 

        ' Set the message properties.
        mailer.Message.From.Email = "john.doe@company.com"
        mailer.Message.To.Add("jane.doe@example.com"")
        mailer.Message.Subject = "Hello"
        mailer.Message.BodyPlainText = "Hello, Jane, can we meet today?"

        ' Sign the message with DomainKeys and DKIM.
        mailer.Message.DomainKeysSign(False, Nothing, "C:\Temp\rsa512.private", True, "dk", DomainKeysTypes.Both)

        ' Send the message via SMTP server (authentication is used in this sample).
        mailer.SmtpServers.Add("mail.company.com", "john.doe@company.com", "secret")
        mailer.Send()
    End Sub
End Class

See Also

MailMessage Class | MailBee.Mime Namespace | DomainKeys