Would like a few sample coding on how to email a list of recipents with there own unique information using a plaint text or html template.
template Example
<html>
<head>
<title>Welcome to </title>
</head>
<body>
<p>Dear <%Name%>,</p>
<p>An account has been created for you.</p>
<p>Your account is FREE and allows you to perform bla bla features.</p>
<p>To login and complete your profile, please go to:</p>
<p><a href=""><%LogOnUrl%></a></p>
<p>Your User ID is your email address and password is: <%Password%></p>
</body>
</html>
WOULD LIKE TO KNOW HOW TO TAKE THE TEMPLATE AND TRANSFORM THIS INTO A FORMATTED MESSAGE WITH REQUIRED FIELDS FILLED OUT, THEN STORE THAT FORMATTED MESSAGE LIKE IN A REPOSITORY.
I am storing email templates as normal asp webpages. For example, i have an email template
@model EmailTemplate
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Thank you for Registration</title>
</head>
<body>
Hello @Model.FirstName, <br/>
Thank you for registering.
Your username is: @Model.UserName <br/>
Your password is: @Model.Password
</body>
</html>
Then, from code behind, i am getting the page html content.
This is the static method
public static string GetViewPageHtml(Controller controller, object model, string viewName)
{
ViewEngineResult result = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
if (result.View == null)
throw new Exception(string.Format("View Page {0} was not found", viewName));
controller.ViewData.Model = model;
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
{
using (HtmlTextWriter output = new HtmlTextWriter(sw))
{
ViewContext viewContext = new ViewContext(controller.ControllerContext, result.View, controller.ViewData, controller.TempData, output);
result.View.Render(viewContext, output);
}
}
return sb.ToString();
}
And i call it from a Controller, like this:
public ActionResult SendEmail()
{
var viewPageModel = CreateEmailTemplateModel();
string emailTemplate = GetViewPageHtml(this, viewPageModel, "~/Views/EmailTemplates/RegistrationSuccess.cshtml");
// now send the emailTemplate as "body"
}
Is very easy to update the template, and you are working on a real web page
MakeMe
0 Points
1 Post
Emails notification
Apr 12, 2012 09:03 PM|LINK
HEY ALL
Would like a few sample coding on how to email a list of recipents with there own unique information using a plaint text or html template.
template Example
<html>
<head>
<title>Welcome to </title>
</head>
<body>
<p>Dear <%Name%>,</p>
<p>An account has been created for you.</p>
<p>Your account is FREE and allows you to perform bla bla features.</p>
<p>To login and complete your profile, please go to:</p>
<p><a href=""><%LogOnUrl%></a></p>
<p>Your User ID is your email address and password is: <%Password%></p>
</body>
</html>
WOULD LIKE TO KNOW HOW TO TAKE THE TEMPLATE AND TRANSFORM THIS INTO A FORMATTED MESSAGE WITH REQUIRED FIELDS FILLED OUT, THEN STORE THAT FORMATTED MESSAGE LIKE IN A REPOSITORY.
ignatandrei
All-Star
135094 Points
21672 Posts
Moderator
MVP
Re: Emails notification
Apr 13, 2012 03:56 AM|LINK
var text = ////
formattedText = text.Replace("%Name%", yourName).Replace(....)
Store in a database table.
zuperboy90
Participant
977 Points
819 Posts
Re: Emails notification
Apr 13, 2012 05:37 AM|LINK
Hello,
I am storing email templates as normal asp webpages. For example, i have an email template
@model EmailTemplate @{ Layout = null; } <!DOCTYPE html> <html> <head> <title>Thank you for Registration</title> </head> <body> Hello @Model.FirstName, <br/> Thank you for registering. Your username is: @Model.UserName <br/> Your password is: @Model.Password </body> </html>Then, from code behind, i am getting the page html content.
This is the static method
public static string GetViewPageHtml(Controller controller, object model, string viewName) { ViewEngineResult result = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName); if (result.View == null) throw new Exception(string.Format("View Page {0} was not found", viewName)); controller.ViewData.Model = model; StringBuilder sb = new StringBuilder(); using (StringWriter sw = new StringWriter(sb)) { using (HtmlTextWriter output = new HtmlTextWriter(sw)) { ViewContext viewContext = new ViewContext(controller.ControllerContext, result.View, controller.ViewData, controller.TempData, output); result.View.Render(viewContext, output); } } return sb.ToString(); }And i call it from a Controller, like this:
public ActionResult SendEmail() { var viewPageModel = CreateEmailTemplateModel(); string emailTemplate = GetViewPageHtml(this, viewPageModel, "~/Views/EmailTemplates/RegistrationSuccess.cshtml"); // now send the emailTemplate as "body" }Is very easy to update the template, and you are working on a real web page