In my last article, we discussed SendGrid, why it was good (mainly ease of use), and why you shouldn’t probably use it anymore. Today, I want to discuss an alternative to SendGrid - SmtpServer. Let’s look at the description:
SmtpServer is a simple, but highly functional SMTP server implementation. Written entirely in C# it takes full advantage of the .NET TPL to achieve maximum performance.
So, it’s an SMTP server implemented as a library and not as a standalone application. You can find the list of supported features on the official GitHub page. Personally, I played only with basic ones (smtp server on port 25 without any protection/encryption).
To setup a server, you just need several lines of code:
SmtpServer injects IMessageStore, IMailboxFilter, IUserAuthenticator implementation through ServiceProvider.
IMessageStore allows you to implement the store for messages
IMailboxFilter allows you to implement some logic to filter out messages
IUserAuthenticator allows to implement a custom auth
Default implementations do nothing, IMessageStore discards any message, IMailboxFilter accepts any message without filtering, IUserAuthenticator authenticates any user. And obviosly in the real world scenarios, you need to provide custom implementations.
For my needs, I decided to implement IMessageStore to save messages to the collection and access them later. For example:
I use this code in my integration tests when I need to wait for an email message, for example, for a new user registration message to get a token and activate the user later:
With this library, you don’t need to setup any additional software, service, docker container, etc. It just works.
Conclusion
The SmtpServer provides a simple and extendible interface for starting a SMTP server. And even though, I’ve tested only the basic subset of features, I’ve shown how can you extend this library and use it for your needs. I’m not sure whether this library is the production ready or not. But for integration tests (the example provided in this article), this library is a great choice.