Site icon Ryadel

ASP.NET Core C# - Send email messages via SMTP with MailKit

In this article I will briefly explain how to implement the IEMailSender interface using MailKit, which will allow any ASP.NET Web Application to send e-mail messages using any SMTP server.

For those who don't know what the IEmailSender interface and MailKit are, here's a brief explanation to better understand what are we talking about:

In a nutshell, what we're about to do is to implement the IEmailSender interface using MailKit, so that we can use the native infrastructure to seamlessly send e-mail messages through the SMTP server of our choice. Are we ready? Let's start!

DISCLAIMER: This website is not affiliated with MailKit; this article represents the free opinion of the author and has not been commissioned or sponsored in any way.

1. Installing MailKit

The first thing we need to do is to install the MailKit NuGet package, which can be done in the following ways:

Once MailKit has been installed, we can move on to the next step.

2. Creating a TOptions class

If we don't want to "hardcode" the various required parameters to connect to the SMTP server (server name, protocol, credentials, and so on), we need to find a way to provide them to our IMailSender interface implementation: the most effective way  to do that is by leveraging the ASP.NET Core options pattern with a custom options class specifically made to host MailKit required variables, which we'll call MailKitEmailSenderOptions.

Here's how we can do that:

Needless to say, this is only a small example including the required stuff: we're free to add further properties if we need to configure additional parameters of our SMTP and/or e-mail message.

3. Implementing the MailKitEmailSender

Now that we can rely upon a configurable set of options we can implement the MailKitEmailSender class, which is the most important part of our work:

As we can see by looking at the above code, the class makes an extensive use of the MailKit NuGet Package to perform the SMTP connection and to actually send the e-mail message, taking all the required option from the MailKitEmailSenderOptions class and from the SendEmailAsync() method parameters.

4. Adding the MailKitEmailSender to the pipeline

Now that we have our very own MailKitEmailSender class, we need to add it to the web application's pipeline. This can be done by editing the Startup.cs file and adding the following statement block to the ConfigureServices() method:

This will make our web application aware of our custom EmailSender class during the startup phase and allow us to instantiate it in our Controllers using Dependency Injection.

If you need a SMTP service with a free plan you can visit SendInBlue.com and create a free account there, which allows to send up to 300 e-mail messages per day: should you choose do that, we strongly suggest to also read our tutorial on send email messages with SendInBlue and MailKit, which explains how to properly retrieve the required SMTP settings to feed the MailKitEmailSenderOptions class with.

Configure the SMTP settings from appsettings.json

In case you don't want to hardcode such values in the Startup.cs class - which would be quite undesiderable for a number of good reasons - you can define the SMTP settings in the appsettings.json file instead:

And then reference them to the Startup.cs class in the following way:

5. Sending the E-Mail message

Now that we've done all the required stuff, we just need to actually send a email message through our application. This can be done from any controller in the following way:

As we can see, we're accessing a IEmailSender instance through Dependency Injection in the SampleController'sconstructor, and then use it in the SendMail() action method below: such instance will be instantiated using the MailKitEmailSender implementation which we've added to the pipeline.

Conclusion

That's it, at least for the time being: I hope that this small tutorial will help other ASP.NET Core developers to implement their own MailKit-based EmailSender and send e-mail messages with ease!

UPDATE: We've recently written a follow-up post with an alternative implementation that takes advantage of the NETCore.MailKit NuGet package to achieve the same results in a faster way and with less custom code. You can find it here.
This article is part of the ASP.NET Core 5 and Angular book, available as paperback and e-book.

 

 

*/ ?>
Exit mobile version