Razor 视图页面作为电子邮件模板 [英] Razor View Page as Email Template

查看:20
本文介绍了Razor 视图页面作为电子邮件模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 Razor 语法设计了一个电子邮件模板.当我使用 C# 代码和 SMTP 协议将此模板作为电子邮件发送时,我将裸露的 Razor 和 HTML 标记作为电子邮件正文.这种方法我错了吗?是否允许 Razor 页面作为电子邮件模板?

I have designed an Email Template from Razor Syntax. When I send this template as Email using C# code and SMTP protocol, I get bare Razor and HTML markups as Email Body. Am I wrong in this approach? Are Razor Pages allowed as Email Template?

这是我的主页

@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>

更新..

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

  }

稍后我将 SMTP 代码替换为 ..

Later On I am replacing the SMTP Code as ..

  MailMessage message = new MailMessage(
    ApplicationWideData.fromEmailId, // From field
    ToEmailId, // Recipient field
    "Click On HyperLink To Verify Email Id", // Subject of the email message
    body
   );

推荐答案

电子邮件只能理解两种格式:纯文本和 HTML.由于 Razor 两者都不是,因此需要由某些引擎处理它,以便将生成的 HTML 返回给您.

Email messages only understand two formats: plain text and HTML. Since Razor is neither, it will need to be processed by some engine, so that it gives you back the generated HTML.

这正是您在 ASP.NET MVC 中使用 Razor 时在幕后发生的情况.Razor 文件被编译成一个内部 C# 类,该类被执行,执行的结果是发送到客户端的 HTML 字符串内容.

That's exactly what happens when you use Razor in ASP.NET MVC, behind the scenes. The Razor file is compiled into a internal C# class, that gets executed, and the result of the execution is the string content of the HTML, that gets sent to the client.

您的问题是您想要并且需要运行该处理,只是为了将 HTML 作为字符串返回,而不是发送到浏览器.之后,您可以对 HTML 字符串执行任何操作,包括将其作为电子邮件发送.

Your problem is that you want and need that processing to run, only to get the HTML back as a string, instead of being sent to the browser. After that you can do whatever you want with the HTML string, including sending it as an e-mail.

有几个包含此功能的软件包,我已成功使用 Westwind.RazorHosting,但您也可以使用 RazorEngine 获得类似的结果.我更喜欢 RazorHosting 用于独立的非网络应用程序,而 RazorEngine 用于网络应用程序

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

这是我的一些代码的(净化)版本 - 我使用 Westwind.RazorHosting 从 Windows 服务发送剃刀格式的电子邮件,使用强类型视图.

Here is a (sanitized) version of some of my code - I'm using Westwind.RazorHosting to send razor-formatted emails from a windows service, using a strongly typed view.

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

这篇关于Razor 视图页面作为电子邮件模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆