Skip to content

How to Send Emails in ASP.Net Web Applications

How to Send Emails in ASP.Net Web Applications

How to Send Emails in ASP.Net Web Applications

Sending emails is a common task in web applications and is required in many scenarios, such as sending registration confirmations, password reset links, and other notifications. In this article, we will learn how to send emails using ASP.NET.

Prerequisites

To send emails in C#, you will need the following:

  • An SMTP server: This is the server that will be used to send emails. You can use a free service like Gmail or Microsoft Outlook or set up your own SMTP server.
  • Credentials: You will need a username and password to connect to the SMTP server. These credentials are usually provided by your email service provider.
  • Email message information: you will need to know the recipient’s email address, subject, and message body of the email you want to send.

Sending emails in ASP.Net

Let’s see how to add email-sending functionality to our web project step by step. By the way, did you know that we offer a unique and powerful online course that boosts your C# career? Check it out here!

Step 1: Adding EmailSender interface

Let’s start by creating the IEmailSender interface.

The interface contains a method signature that receives an email address, a subject, and a message to be sent to the address.

Step 2: Configuring the Services

In this step, we will configure the services for sending emails in the Startup.cs file. The first thing we need to inject it into the services object at the application’s startup:

This line of code adds the IEmailSender interface to the service container and maps it to the EmailSender class. You can learn more about dependency injection in C# from my dependency injection article.

Step 3: Creating the EmailSender Class

In this step, we will create a new class that implements the IEmailSender interface. This class will be responsible for sending emails using the SmtpClient class.

As you can see in the above code snippet, the EmailSender class has a SendEmailAsync method which takes three parameters: the recipient’s email address, the subject of the email, and the message body. The method creates an instance of the SmtpClient class and sets its properties, such as the host, port, and credentials. The credentials are the username and password of your email account. Finally, the method calls the SendMailAsync method of the SmtpClient class to send the email.

It’s important to note that you should replace the host, port, and credentials in the above example with the appropriate values for your SMTP server. Also, you should replace the sender’s email address and name with the appropriate values for your application.

Step 4: Sending the Email

Now that we have configured the services and created the EmailSender class, we can send emails from anywhere in our application by injecting the IEmailSender interface and calling the SendEmailAsync method.

For example, in a controller action, you can send an email like this:

In the above example, the SendEmail action takes three parameters: the recipient’s email address, the subject of the email, and the message body. The action injects the IEmailSender interface and calls the SendEmailAsync method to send the email.

Application Configuration

In real-world scenarios, we need to read the configuration options for sending emails from the appsettings.json and environment variables rather than hard-coding them.  We can do this by creating a class named AuthMessageSenderOptions that contains the configuration properties and then injecting it into the services object at the application’s startup like the following:

If you want to skyrocket your C# career, check out our powerful ASP.NET full-stack web development course that also covers test-driven development and C# software architecture.

You can find the project for this article in this Github repository.

Security Warning

It’s important to note that sending email is a sensitive operation and it’s important to keep security in mind when sending emails. You should avoid hardcoding sensitive information like credentials and use a configuration manager like the appsettings.json file to store them. You can read my secret management post to learn more about storing secrets in dotnet.

Conclusion

In this article, we have learned how to send emails in ASP.NET Core using the built-in “Send Email” functionality provided by the framework. We have seen how to install the required package, configure the services, create the EmailSender class, and send emails from a controller action. With this knowledge, you should be able to implement email functionality in your own web applications.

It’s important to note that it’s not recommended to use your personal email account for sending emails from an application in production; you should consider using transactional email services such as SendGrid or Mailgun.

Enter your email and we will send you the PDF guide:
Enter your email and we will send you the PDF guide