- First you need to obtain private and public key for DKIM purposes.
- Then you need to create DNS record for DKIM.
- Final step is sending signed message.
For details on 1 and 2 please follow the steps described in sign emails with DKIM.
Step 3 is very simple.
Note the usage of PemReader class that reads "dkim_private.key" file obtained from OpenSSL's key generation. Also note that attachment is added to the email (*AddAttachment*):
MailBuilder builder = new MailBuilder();
builder.Subject = "Hello";
builder.Text = "Hello";
builder.From.Add(new MailBox("alice@example.com"));
builder.To.Add(new MailBox("bob@mail.com"));
builder.AddAttachment(@"c:\report.csv");
RSACryptoServiceProvider rsa =
new PemReader().ReadPrivateKeyFromFile(@"d:\dkim_private.key");
builder.DKIMSign(rsa, "alpha", "example.com");
IMail email = builder.Create();
using(Smtp smtp = new Smtp())
{
smtp.Connect("smtp.example.com"); // or ConnectSSL for SSL
smtp.UseBestLogin("alice@example.com", "password");
smtp.SendMessage(email);
smtp.Close();
}
There are several ways of adding attachment to the DKIM signed email (byte[] array, filename).