Sending emails with .Net Core using MailKit with DKIM signature
In this article, I will go through the simplest way to send an email from any .Net Core app, adding a DKIM signature to improve the Spam score.
Adding a DKIM is an important part of sending emails to limit the number of them ending up in the Spam folder. And yet the documentation for that is pretty scarce.
To create a valid DKIM signature you need a domain name where you can add a DNS entry.
If you already have an email sending app, you can jump to step 6.
Here are all the steps:
- Get a .Net Core app
- Add MailKit, the “cross-platform .NET library for IMAP, POP3, and SMTP.”
- Create a Mailtrap account to get a SMTP server for testing
- Configure your API to send emails with MailTrap
- Write your entry point and service
- Add DKIM signature
The .Net Core app
In this exemple, I will be using a Web API made with .Net Core 3.1. If you don’t have one available, just type:
dotnet new webapi
You should now be able to access https://localhost:5001/weatherforecast (with a security warning probably, but that’s ok)
dotnet add package MailKit
Create a MailTrap account
MailTrap is a tool that provide you with a dev SMTP account and shows you what is received.
Go there, create an account and get the SMTP’s credentials
Configure your API
To access the SMTP credentials, you just have to put them in you appsettings.json file.
You probably have 2 appsettings files: appsettings.json (default) and appsettings.Development.json. Be sure to use the right one depending on your environnement.
It should look something like this:
Don’t forget to change Login and Password
Write your entry point and service
Our entry point will be https://localhost:5001/test. On every request, it will send an email to MailTrap’s server.
So first, we need a service method to send an email. Create a file called EMailerService.cs just like this one:
Register it in your app. You do that in Startup.cs:
Then, let’s create a controller named Test and create a Get method to respond to get requests so you have an entry point to call your send email method:
At this point you can launch your app, access https://localhost:5001/test and check MailTrap’s inbox for your first mail sent!
Add DKIM signature
Let’s be honest: this is NOT a fun process!
It goes like this:
- Go to https://dkimcore.org/tools/ and get yourself a key
- Add the public key to be published via DNS TXT record
- Copy the private key in your app
- Import the private key
- Configure your service accordingly
When you go to https://dkimcore.org/tools/, the first tool is to generate a DKIM core key. That’s what we need. Put your domain name in it and Generate.
You should see a page full of info on your newly generated DKIM tokens.
On the Public Key section, follow the instructions to add the public key as a DNS TXT record in your domain name.
On the Private Key section, download the private key, create a folder named data at the root of your app and copy the key into it.
Change your .csproj file to embed the data folder when building:
Now, add the DKIM signature to your message
Change the parameters passed to the DkimSigner constructor to match your DKIM config and sign the whole message with it.
You can now send signed emails!
The next step is to get an SMTP server from any provider (SendGrid, your own …) and test your mails with this https://www.mail-tester.com/ to make sure your DKIM signature is working as expected.
I hope you found this post useful and easy to follow. Please let me know if you have any improvements and/or questions in the comments below.