Using MailKit to Send Email Notifications in .Net Core

April 19, 2023

These days, most applications come with the ability to send email notifications. Sending an email in Dot Net is quite easy and can be accomplished using Simple Mail Transfer Protocol (SMTP). I have used SMTP in many of my web applications, and it is still available in .Net Core.

So, why should you use MailKit? While implementing the email function in one of my .Net Core projects, I found out that Microsoft had deemed SMTP to be outdated. Instead, they recommended using the MailKit library. For more information, please check out the link below.
https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?WT.mc_id=AZ-MVP-5002809&view=netcore-2.2

SMTP Client Class

SMTP Client Class 1

I have used MailKit library in my .Net Core project.

Here I will share how to use simple email sending function using MailKit library in .Net Core projects.

Implementation:

Mailkit

After a successful installation, we can proceed further with the code.

1. Create a configuration class.

Configuration Class

2. Add the mail server settings in appsettings.json file, like shown below.

Mail Server Settings

3. Create one email message class.

Email Message Class

4. To encapsulate email sending functionality and to use it anywhere from the application we should create one interface and class like given below.

Email Sending Functionality

5. Create a new class and inherit interface.

Class and Inherit Interface

6. The code below is an implementation in class file EmailSender. Please don’t get confused with Smtp class used below in the code; this class belongs to MailKit library class. CreateEmailMessage method is a simple message creation method which we need to use to create a message using MimeMessage class.

Class File EmailSender

7. In the .Net Core project, we need to add dependency injection in Startup.cs file.

Dot Net Core Project

8. Now we are ready to use the send method and can call this method from anywhere in the application.

Send Method

The implementation provided above is for the synchronous method. Similarly, we need to implement an asynchronous method for the send function. We can also add code to be able to send emails with attachments.

When calling the send method, we can use a string for the message body or even a dynamic template file. The BodyBuilder class can be used to build the body or content of the email. It can also be used to add data from the template file and then change the dynamic content.

I developed this in a .Net Core project, but the recommendation is to use the MailKit library in all new developments, even if the project is in the .Net framework. In older implementations, we can replace existing SMTP code with MailKit.

Partner with MetaSys Software, the top DotNet development company, to build powerful and reliable software solutions that will drive your success. Contact us now to schedule a consultation and start your journey towards unparalleled growth!

Tags :

Category :