Sending Confirmation Email in ASP.NET Core Identity

In this article let us learn 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 at three places.

  1. After user registration, to confirm the registered Email
  2. Resending the Email Confirmation Link
  3. During the Forgot password option

If we do not configure the Email services, then Identity API will not be able to send the Emails. In such a scenario, the identity API will generate the confirm link in the web page itself which is not safe.

Create a New Web APP

Create a new ASP.NET Core Web APP with .NET 5.0 & Individual Accounts Enabled.

Update the Connection String in appsettings.json.

Run EF Core Migrations to create the database

Run the app check everything is ok

Email Service

Now, let us create an Email Service to use with Identity API.

Create a new class EmailSender under the folder Services. This class must inherit from the IEmailSender interface, which is defined in the Microsoft.AspNetCore.Identity.UI.Services namespace.

The IEmailSender has only one method SendEmailAsync, which we need to implement in our concrete class. The Signature of the method is as below.

There are many ways in which you can send emails from your ASP.NET Core. The simplest of them is to use the Gmail & SmtpClient from the System.Net.Mail namespace.

Gmail settings

You can use any Gmail account to send emails. But to do that you need to set the App Password. Follow these steps

  1. Log in to your Google Account
  2. Go to My Account > Security > Signing into Google -> App Passwords
  3. Enter your password
  4. Select the App & Devices from the Dropdown
  5. Click Generate
  6. Copy the Password & Use it along with your email account.

Step 2 My Account > Security > Signing into Google -> App Passwords

Create App Password in google

Step 4. Select the App & Devices from the Dropdown

App Passwords

Code to Send the Email

Here, we are using the SmtpClient to send the Email

We need to register this service in the startup class in theConfigureServices method.

Now, we are ready. Let us test our app.

Registering a User

Now try to register a new user. On successful registration, you will get a Registration Confirmation message. It will also send an email with a confirmation link.

The following image shows the format of the confirmation mail. Confirm your account, by clicking on the link.

Email Confirmation in ASP.NET Core Identity

Resend Email Confirmation

If you try to log in without a confirmed account, will result in a Invalid login attempt message.

Invalid Login Attempt

You will also have an option to resend to email confirmation, which will ask for your email id.

Resend Email Confirmation

Forgot Password

Now, check the Forgot password Option from the login form. It will ask for your registered email id. On clicking Reset Password, option you will get the reset password link in your mail box

Forgot your password

The Reset Password mail format. Click on the link, which will take you to the ResetPassword form, where you can reset your password.

Reset password form

That’s it.

3 thoughts on “Sending Confirmation Email in ASP.NET Core Identity”

  1. great tutorial. It seems like there is 1 step missing – in order for the line in startup.cs to work (services.AddTransient();) it requires two references:
    1. using Microsoft.AspNetCore.Identity.UI.Services for IEmailSender
    and
    2. using .Services; to access EmailSender

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top