24

I have written an program in C# which sends out emails. Now I have a requirement to sign outbound emails using Dominkeys/DKIM, but I'm not sure how to do it.

I have set up all keys, but I don't know how to get those and how to include them in the email header.

1
  • Did you find a solution using Windows SMTP Server?
    – Josh
    Jul 19, 2010 at 14:30

6 Answers 6

19

There is a fundamental problem with trying to do DKIM signatures with System.Net.Mail.MailMessage and System.Net.Mail.SmtpClient which is that in order to sign the message, you need to poke the internals of SmtpClient in order to hash the message body as one of the steps in generating the DKIM-Signature header. The problem comes in when you have alternative views or attachments because SmtpClient will generate new multipart boundaries each time it writes out the message which breaks the body hash and thus the DKIM-Signature validity.

To work around this, you can use the MimeKit and MailKit open source libraries for .NET as an alternative framework to using System.Net.Mail.

To add a DKIM signature to a message in MimeKit, you would do something like this:

MimeMessage message = MimeMessage.CreateFromMailMessage(mailMessage);
HeaderId[] headersToSign =  new HeaderId[] { HeaderId.From, HeaderId.Subject, HeaderId.Date };

string domain = "example.net";
string selector = "brisbane";

DkimSigner signer = new DkimSigner ("C:\my-dkim-key.pem", domain, selector) 
{
   SignatureAlgorithm = DkimSignatureAlgorithm.RsaSha1,
   AgentOrUserIdentifier = "@eng.example.com",
   QueryMethod = "dns/txt",      
};

// Prepare the message body to be sent over a 7bit transport (such as 
// older versions of SMTP). This is VERY important because the message
// cannot be modified once we DKIM-sign our message!
//
// Note: If the SMTP server you will be sending the message over 
// supports the 8BITMIME extension, then you can use
// `EncodingConstraint.EightBit` instead.
message.Prepare (EncodingConstraint.SevenBit);

message.Sign (signer, headersToSign, 
    DkimCanonicalizationAlgorithm.Relaxed, 
    DkimCanonicalizationAlgorithm.Simple);

To send the message using MailKit, you would do something like this:

using (var client = new MailKit.Net.Smtp.SmtpClient ()) {
    client.Connect ("smtp.gmail.com", 465, true);
    client.Authenticate ("username", "password");
    client.Send (message);
    client.Disconnect (true);
}

Hope that helps.

3
  • Once I create a MimeKit.MimeMessage how can I send it? There are undreads of lines of code about creating the message, but not a single Send() nad not a single line example of sending it. Can MimeKit be used (to send the message) without MailKit ?
    – Alex 75
    Mar 3, 2021 at 23:58
  • My answer includes code for sending. Not sure how you missed it...
    – jstedfast
    Mar 4, 2021 at 15:01
  • I didn't missed it. I referenced MimeKit because I need to DKIM sign my emails. MimeKit does the job but MimeKit alone is not enough to send emails or to be used with the .net SmtpClient. You necessarily need another library (MailKit) to send the MimeMessage. It was not so obvious to me. The 2 libraries are used togheter in 99% of the case (maybe 100% because I don't think there is any alternative to MailKit) that I just liked it clearly stated in the MimeKit README or its "Get Start" page instead of figure out myself after a long search and you confirming this. Thank you.
    – Alex 75
    Mar 4, 2021 at 15:39
6

see https://github.com/dmcgiv/DKIM.Net it's a DomainKeys Identified Mail (DKIM) implementation for .Net written in C# - it enables you to sign MailMessage objects.

2
  • This currently throws exceptions out of the box - trying to debug it but am thinking of finding another library in the mean-time
    – Doug
    Nov 14, 2011 at 3:09
  • @Doug readme has been updated. If you are still having issues create an issue on the github site please with more details. Nov 15, 2011 at 0:56
2

Use http://www.mimekit.org

Not only does it allow to use DKIM for signing, also you can include S/MIME certificates, PGP certificates and more. Also, its a very mature lib - the only one i've found that handles foreign languages (apart from english) correctly, since its completely and thoroughly coded with unicode in mind.

Its free and opensource.

2

This solved it for me when using Mailenable as SMTP relay server.

http://www.mailenable.com/kb/content/article.asp?ID=ME020700

When creating the DKIM TXT record on the domain name don't forget to use the active selector as prefix => yourselector._domainkey.yourdomainname.be

1

If you are looking to DKIM-sign the body of the MailMessage then DKIM.NET is great. If you are looking to have alternative views in your message then I wasnt able to find a solution and wrote my own (open-source with the usual disclaimers) that can be found at https://github.com/yannispsarras/DKIM-AlternativeViews

I understand this is a pretty old thread but I thought it may help someone.

2
  • I dont understand the downvote. I have even provided an open source project that does all that...
    – Yannis
    Jun 26, 2013 at 10:54
  • Not Work when attachment in mailmessage thats why i am using Mailkit.
    – Naveen
    Jun 8, 2018 at 12:37
-1

i didnt find much help on this issue, but my problem got solve by configuring smtp server. i cant post those steps as i am using 3rd party smtp server and every server has their own configuration. after proper configuration my smtp automatically adds DM/DKIM signature.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.