Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
9 Feb 2015 1  
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.

template

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

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

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here