In this article, we are going to learn how to send email in ASP.NET Core. We are going to start with the simple project creation and creating a basic email configuration. Once we are finished with that, we are going to install the required Mailkit library and create the logic to send email messages from ASP.NET Core.

We are going to show you sync and async ways to send email messages in ASP.NET Core and how to change the email body from the plain text to the pure HTML.


VIDEO: How to Send an Email with Attachments in ASP.NET Core.


Finally, we are going to learn how to include attachments in the email message.

The source code is available at our GitHub repo.

So, let’s see the basic navigation for this article:

Creating a Project and a Basic Configuration

We are going to start with a new ASP.NET Core 3.1 Web API project. After the project creation, we are going to add the .NET Core Class Library project with the name EmailService. Of course, we have to add a reference to the main project:

Project structure

Let’s add the EmailConfiguration class inside that project:

This class contains properties needed to configure sending email messages from our application. We are going to use the appsettings.json file to populate these properties, and in order to do that, we have to modify that file:

To finish with the configuration process, we are going to modify the ConfigureServices method in the Startup.cs class:

So, we extract configuration values from the appsettings file and register EmailConfiguration as a singleton. And that’s all we need to configure our email service.

How to Send Email in Synchronous Way?

Before starting any other operation in our project, we have to add the NETCore.MailKit library to the EmailService project:

MailKit

This library is going to help us with the email sending process.

Next, in the same project, we are going to create a Message class:

We are going to use this class to set the data related to our email recipients, subject and content.

Then, let’s create a new interface:

And a class that implements this interface:

As you can see, we inject email configuration into EmailSender class and then we call two different methods to create an email message and to send the email respectively. Now, let’s implement those two missing methods:

We use the first method to create an object of type MimeMessage and to configure the required properties. Then, we pass that object to the second method and use the SmtpClient class to connect to the email server, authenticate and send the email.

Just pay attention that the SmtpClient class comes from the MailKit.Net.Smtp namespace. So, you should use that one instead of System.Net.Mail.

Now, we have to register this service in the Startup.cs class:

services.AddScoped<IEmailSender, EmailSender>();

We have one more step to do. Let’s modify the Get action in the WeatherForecastController:

That’s it. Let’s test this now:

Postman request sync

We get a 200 OK response. So, let’s check the source email server:

Email server source

And let’s check the destination server:

Email server destination - Send Email

Excellent. Everything works as expected.

Just one note here. If you are using the Gmail server as a source server and you get an error about less secure apps, all you have to do is to sign in to your Gmail account, follow this link and turn on allow option.

Using HTML in the Email Message Body

In the previous example, we have been using the Plain Text as a body format. But we can use HTML as well with a few modifications to our code.

So, all we have to do is to modify the CreateEmailMessage method:

And that’s all it takes. A simple change of the body text format and the content itself.

We can try sending the same request one more time from Postman:

Html body for the email message

It doesn’t get any easier than that.

Asynchronous Way of Sending Email Messages in ASP.NET Core

If we want to send email messages asynchronously, we have to make some changes to our project. If you are not familiar with the asynchronous programming in ASP.NET Core Web API, we strongly recommend reading our article about Asynchronous Repository Pattern in ASP.NET Core Web API.

Let’s start with the interface modification:

Next, let’s modify the EmalSender class:

As you can see, we use the same method to create an email message but a different method to send that email. So, we have to implement that one:

Finally, we have to modify the Get action:

Awesome. Now let’s try this out with the same request from Postman:

Async email message

Adding Attachments to the Email Message

In order to include attachments to our email messages, we have to provide the way for our app to process the attached files.

So, let’s start by adding a new POST action to our controller:

This action is almost the same as the previous one, but it has some important modifications. The first thing to notice is that this is a POST action. The second change is the part where we extract files from the request, if any, or return an empty file collection. The third change is related to the Message constructor, it now accepts an additional parameter.

Of course, we have to modify the Message class:

As you can see, we’ve added the additional IFormCollection parameter. Now, let’s move on to the EmailSender class:

We now use the BodyBuilder class to create a body for the email message. Additionally, we check for the attachment files and if they exist, we convert each of them to the byte array and add it to the Attachments part from the bodyBuilder object. Finally, we convert the bodyBuilder object to the message body and return that message.

Now, before we use Postman to send a request, we are going to add the FormOptions configuration to the ConfigureServices method:

With the FormOptions configuration, we set the limit for it’s different properties to the maximum value.

One more thing. We have to modify the call to the Message constructor in the Get action because now it accepts four parameters.

To prepare a request in Postman, we have to choose the POST method option and to use the form-data options to send files with the request:

Send Email with attachment Postman

And let’s inspect our email:

Attachments email

Excellent work.

Conclusion

We did a great job here. Now we have a fully functional Web API service that we can use in our projects to send email messages.

So to sum up, we have learned:

  • How to create a basic configuration
  • The way to send email messages with the plain text or html body
  • How to send email messages asynchronously
  • And how to include attachments in an email message

If you have enjoyed reading this article and if you would like to receive the notifications about the freshly published .NET Core content we encourage you to subscribe to our blog.