Razor browse page as email template

I developed an email template from Razor syntax. When I send this template as email using C # code and SMTP protocol, I get Razor and HTML nude markups as the body of the email. Am I mistaken in this approach? Are Razor pages allowed as an email template?

Here is my page

@inherits ViewPage @{ Layout = "_Layout"; ViewBag.Title = ""; } <div class="container w-420 p-15 bg-white mt-40"> <div style="border-top:3px solid #22BCE5">&nbsp;</div> <span style="font-family:Arial;font-size:10pt"> Hello <b>{UserName}</b>,<br /><br /> Thanks for Registering to XYZ Portal<br /><br /> <a style="color:#22BCE5" href="{Url}">Click to Confirm Email</a><br /> <br /><br /> Thanks<br /> Admin (XYZ) </span> 

Update ..

  using (StreamReader reader = new StreamReader(HttpContext.Current.Server.MapPath("~/ContentPages/EmailConfTemplate.cshtml"))) { body = reader.ReadToEnd(); //Replace UserName and Other variables available in body Stream body = body.Replace("{UserName}", FirstName); } 

Subsequently, I replace the SMTP code with ..

  MailMessage message = new MailMessage( ApplicationWideData.fromEmailId, // From field ToEmailId, // Recipient field "Click On HyperLink To Verify Email Id", // Subject of the email message body ); 
+8
html c # email razor
source share
4 answers

Email messages understand only two formats: plain text and HTML. Since Razor is neither one, it needs to be handled by some kind of engine, so it will return the generated HTML to you.

This is what happens when you use Razor in ASP.NET MVC, backstage. The Razor file is compiled into the C # inner class, which runs, and the result of the execution is the HTML string content that is sent to the client.

Your problem is what you want and need this processing to run, only to return the HTML as a string instead of sending to the browser. After that, you can do whatever you want with an HTML string, including sending it as email.

There are several packages that include this power, and I have successfully used Westwind.RazorHosting , but you can also use RazorEngine with similar results. I would prefer RazorHosting for stand-alone non-web applications, and RazorEngine for web applications

Here is a (sanitized) version of some of my code - I use Westwind.RazorHosting to send razor-formatted emails from a Windows service using a strongly typed representation.

 RazorFolderHostContainer host = = new RazorFolderHostContainer(); host.ReferencedAssemblies.Add("NotificationsManagement.dll"); host.TemplatePath = templatePath; host.Start(); string output = host.RenderTemplate(template.Filename, model); MailMessage mm = new MailMessage { Subject = subject, IsBodyHtml = true }; mm.Body = output; mm.To.Add(email); var smtpClient = new SmtpClient(); await smtpClient.SendMailAsync(mm); 
+10
source share

Have you looked at the MVC Mailer ?

This is a free package available from GitHub ( https://github.com/smsohan/MvcMailer )

There is a walkthrough for this https://github.com/smsohan/MvcMailer/wiki/MvcMailer-Step-by-Step-Guide

It is also on Nugget too. https://www.nuget.org/packages/MvcMailer

Essentially, it will parse your razor look in html.

+3
source share

Take a look at a razor processor such as RazorEngine ( https://razorengine.codeplex.com/ ), which is available on NuGet. It processes the razor to create an outlet that you would then use as the body of your email.

+1
source share

The Mailzory project is a valuable and convenient choice for sending letters with Razor templates.

 // template path var viewPath = Path.Combine("Views/Emails", "hello.cshtml"); // read the content of template and pass it to the Email constructor var template = File.ReadAllText(viewPath); var email = new Email(template); // set ViewBag properties email.ViewBag.Name = "Johnny"; email.ViewBag.Content = "Mailzory Is Funny"; // send email var task = email.SendAsync("mailzory@outlook.com", "subject"); task.Wait() 

This project is hosted on Github. In addition, there is a nuget package for Mailzory .

0
source share

All Articles