Click here to Skip to main content
15,886,258 members
Articles / Web Development / HTML

Send Emails By Using Email Templates in ASP.NET MVC using C#

Rate me:
Please Sign up or sign in to vote.
3.56/5 (6 votes)
9 Feb 2015CPOL 66.8K   12  
In this post we will learn how to send an HTML formatted email templates in ASP.NET MVC.

In the previous post, we learned how to send emails by using mail helper. In this post, we will learn how to send 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.

Image 1

template

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

Image 2

Place the following HTML code under template file.

HTML
<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 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.

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

The post Send Emails by using email templates in ASP.NET MVC using C# appeared first on Venkat Baggu Blog.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) eBiz Solutions http://venkatbaggu.com/
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --