Configuring Email Service in ASP.NET Core Identity

Configuring Email Service in ASP.NET Core Identity

In this article, I will discuss Configuring Email Service in ASP.NET Core Identity, i.e., How to Send Email in ASP.NET Core Application. Please read our previous article discussing ASP.NET Core Secret Manager.

Email Service in ASP.NET Core Identity

In this article, we will discuss how to Configure the Email Service in ASP.NET Core Identity. This will enable us to confirm the Registered email account by sending a confirmation link to their mailbox. The ASP.NET Core identity needs to send the email in three places.

  • After user registration, to confirm the registered email
  • Resending the Email Confirmation Link
  • During the Forgot Password Option
Configure Gmail SMTP Settings

In our application, we will use the Gmail SMTP server to send the Email. But you need to remember that when using our Gmail Password, we cannot send emails through our ASP.NET Core Application. For this, we need to create an App Password. So, let us first see how we can create the App Password. Follow these steps.

Log in to your Google Account. Go to My Account > Security > as shown in the below image:

Configure Gmail SMTP Settings

Once you click on the Security tab, it will open the following page. In order to generate an App Password, you must have two-factor authentication enabled. In my case, I have already enabled the two-factor authentication, as shown in the below image.

Configuring Email Service in ASP.NET Core

If not enabled, please follow the on-screen steps to enable it. Once enabled, we can generate the App password. Once enabled, the green color tick mark will be there, and then again, click on the arrow symbol as shown in the below image.

Configuring Email Service in ASP.NET Core

It will open the following page, and once you scroll down, you will see the following App passwords section and click on the arrow button, as shown in the image below.

How to Send Email in ASP.NET Core Application

Then it will open the following page asking you to enter the App Name. Here, you can give any name to your app. I am giving the name Dot Net Tutorials and click on the Create button as shown in the below image:

How to Send Email in ASP.NET Core Application

Once you click the Create button, it will open the Generated app password popup, as shown in the image below. Just save the app the app password, which we will use in our application.

Configuring Email Service in ASP.NET Core Identity

Set Up SMTP Configuration:

Add the following configuration to your appsettings.json file:

"EmailSettings": {
    "MailServer": "smtp.gmail.com",
    "MailPort": 587,
    "SenderName": "Your Application Name",
    "FromEmail": "yourgmail@gmail.com",
    "Password": "your App Password"
}

Replace yourgmail@gmail.com and your App Password with your actual Gmail ID and app password.

Note: For security reasons, consider using a secure way to store your credentials, like Azure Key Vault or environment variables, especially if you’re deploying your application to production.

Create an Email Service

Create a service in your application to send emails. So, create a class file named EmailSender.cs and then copy and paste the following code.

using System.Net.Mail;
using System.Net;

namespace ASPNETCoreIdentityDemo.Models
{
    public interface ISenderEmail
    {
        Task SendEmailAsync(string ToEmail, string Subject, string Body, bool IsBodyHtml = false);
    }

    public class EmailSender : ISenderEmail
    {
        private readonly IConfiguration _configuration;
        public EmailSender(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        public Task SendEmailAsync(string ToEmail, string Subject, string Body, bool IsBodyHtml = false)
        {
            string MailServer = _configuration["EmailSettings:MailServer"];
            string FromEmail = _configuration["EmailSettings:FromEmail"];
            string Password = _configuration["EmailSettings:Password"];
            int Port = int.Parse(_configuration["EmailSettings:MailPort"]);

            var client = new SmtpClient(MailServer, Port)
            {
                Credentials = new NetworkCredential(FromEmail, Password),
                EnableSsl = true,
            };

            MailMessage mailMessage = new MailMessage(FromEmail, ToEmail, Subject, Body)
            {
                IsBodyHtml = IsBodyHtml
            };

            return client.SendMailAsync(mailMessage);
        }
    }
}

You can send emails from your ASP.NET Core in many ways. The simplest is to use the Gmail and SmtpClient from the System.Net.Mail namespace.

Register Email Service:

In Program.cs, register your email service as follows:

builder.Services.AddTransient<ISenderEmail, EmailSender>();

In the next article, I will discuss Email Confirmation in ASP.NET Core Identity. In this article, I explain How to Configure Email Service in ASP.NET Core. I hope you enjoy this article, Configuring Email Service in ASP.NET Core.

Leave a Reply

Your email address will not be published. Required fields are marked *