Send Emails by using email templates in ASP.NET MVC using C#

10 Mar

In the previous post we learned how to send emails by using mail helper. In this post we will learn how to send an html formatted email templates in mvc.

For example, if user registered in site then we may want to send an email HTML format. Rather than building the html code in String Builder we can use separate Html file and send email using it.


Create an Template folder under App_Data



Now create an text file/ html file  that contains the email template



Place the following html code under template file.

<body style=”color:grey; font-size:15px;”>
<font face=”Helvetica, Arial, sans-serif”>

<div style=”position:absolute; height:100px; width:600px; background-color:0d1d36; padding:30px;”>
<img src=”logo” />


<div style=”background-color: #ece8d4; width:600px; height:200px; padding:30px; margin-top:30px;”>

<p>Dear {0},<p>

<p>Please click the link below to login and get started.</p>
<a href=””>Login Here</a>

Username: {1}<br>
Password: {2}<br>


<p>Thank you</p>


Now we can use the Mail helper that we created previously created for sending emails. You can check the mail helper here

For the above template you need to send {0} represents username , Username: {1} represents username  and Password: {2} as parameters

string body;
//Read template file from the App_Data folder
 using (var sr = new StreamReader(Server.MapPath("\\App_Data\\Templates\") + "WelcomeEmail.txt"))
     body = sr.ReadToEnd();

   //add email logic here to email the customer that their invoice has been voided
//Username: {1}
   string username = HttpUtility.UrlEncode(user);
   string password = HttpUtility.UrlEncode(password);
   string sender = ConfigurationManager.AppSettings["EmailFromAddress"];
   string emailSubject = @"Welcome Email;

   string messageBody = string.Format(body, username, username, password);

                var MailHelper = new MailHelper
                    Sender = sender, //email.Sender,
                    Recipient = useremail,
                    RecipientCC = null,
                    Subject = emailSubject,
                    Body = messageBody

                //return RedirectToAction("EmailConfirm");
            catch (Exception e)
                ModelState.AddModelError("", e.Message);


One thought on “Send Emails by using email templates in ASP.NET MVC using C#

  1. Hello, I found very useful your idea.
    It’s needed to put the template inside App_Data folder?
    Of course I know it’s under MVC pattern desing.

Leave a Reply

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