10 Mar

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

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 asp.net 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.

Implementation

Create an Template folder under App_Data

template

template

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

texttemplate

 

Place the following html code under template file.

<html>
<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>

<br/>
<br/>

<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>
<p>
<a href=””>Login Here</a>

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

 

<br/>
<p>Thank you</p>
</div>
</body>
</html>

Usage

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();
   }

 try
 {
   //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
                };
                MailHelper.Send();

                //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.
    Thanks!

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>