Skip to content

Blog

How to Test the Emails Send by MailKit in .Net 5?

In a .Net Full Framework application we could use the property SpecifiedPickupDirectory in our mailSettings and the emails got stored to disk instead of sending them to the recipient. That allowed us to check them manually or in our test code.

For .Net 5 you should no longer use System.Net.Mail.SmtpClient and instead use MailKit. However, testing is a lot harder then MailKit does not offer such a convenient way to dump your emails to disk. Let us explore what we can do to check that our applications send the right emails.

Sending Emails in .Net 5 With MailKit

SmtpClient from System.Net.Mail was the way to send emails in .Net 4.x. This old class worked for our use cases well enough to not bother finding an alternative. However, as the time progresses, email protocols improved while SmtpClient stayed the same. If you look at the .Net 5 documentation for SmtpClient, you find this important note from Microsoft:

Important We don't recommend that you use the SmtpClient class for new development because SmtpClient doesn't support many modern protocols. Use MailKit or other libraries instead. For more information, see SmtpClient shouldn't be used on GitHub.

Little Git Tricks: Clean-up Your Working Directory

If your code works on the Continuous Integration (CI) server but not on your local machine, you may have a problem in the files not tracked by Git. And since Git does not track them, there is no easy way to get rid of them - at least in most Git clients. In such cases, we can go to the command line and use the full feature set of Git to fix the problem.

Docker & .Net 4.8: An Endless Obstacle Course

The more complex an application becomes, the greater is the benefit of Docker. If everything runs in containers, we can use a tool like docker-compose and start all the parts with one single command. No need to install all the tools, services, and frameworks - just run docker-compose and the magic happens automatically. We can go from nothing to the full running application in a few minutes.

Since I saw the talk "Dev and Test Agility for your Database with Docker" by Julie Lerman at NDC Oslo 2019 I wanted to port her solution for .Net Core to the .Net Full Framework. How hard could it be? As it turns out, it is an awful lot of work and a few weeks ago Microsoft made it even harder.