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);
Sweko
source share