How to use email template in ASP.NET Core

| Dua Ali

Environment-Variables-in-Canvas-Apps

In this blog, we will discuss that how we can send emails using an email template in ASP.NET core application. It is almost similar to sending emails using plain text. The only difference being that the first step is to read the template file. Below are the steps we will take to add an email template in our ASP.NET application

Step 1:

For this, we will create a folder in wwwroot folder named email template. This is important to create template file in wwwroot folder because we will read the source location of the template file and wwwroot folder’s path would remain same after deploying into the server.

Step 2:

Add a new html file into the template folder name emailtemplate.html

Step 3:

Add html code snippet for template into the file.

Environment-Variables-in-Canvas-Apps

This is an example of email template. In this template we have used placeholders which we can dynamically change in our code logic.

Step 4:

Now we will read the path of the location of the email template

Environment-Variables-in-Canvas-Apps

  1. A string variable is declared containing the main body of the email.
  2. A variable is declared to initialize IWebHostEnvironment variable.
  3. A variable is declared that contains the original path of the email template in our project.
  4. StreamReader class is used to read the email template file and store into an htmlBody variable.
  5. The placeholders present in the template are dynamically filled with the desired values.

Step 5:

Environment-Variables-in-Canvas-Apps

  1. A model is declared containing all the parameters required to send an email.
  2. An http POST request is made with the defined model to the endpoint that is responsible to send an email.

Step 6:

This is the endpoint where a function “SendContactUsEmail” is holding the whole logic to send an email is called with all the required parameters.

Environment-Variables-in-Canvas-Apps

Step 7:

In this function, email configuration is done which is required to send a proper email. We have passed here the email template body from the previous steps. After all the configuration, SendEmail function is called with the desired configuration.

Environment-Variables-in-Canvas-Apps

Join us next time, as we continue our journey of learning canvas apps.Click here to learn more about Imperium's Power Apps Services. We hope this information was useful, and we look forward to sharing more insights into the Power Platform world.


M
Chief Architect, Founder, and CEO - a Microsoft recognized Power Platform solution architect.

About The Blog

Stay updated with what is happening in the Microsoft Business Applications world and initiatives Imperium is taking to ease digital transformation for customers.


More About Us

We provide guidance and strategic oversight to C-Suite and IT Directors for on-going implementations. Feel free to give us a call.

1 331 250 27 17
Send A Message

Ready to Start?

Get a personalized consultation for your project.

Book a Meeting