剃须刀查看页面作为电子邮件模板 [英] Razor View Page as Email Template

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

问题描述

我设计了一个Razor语法的电子邮件模板。当我使用C#代码和SMTP协议将此模板发送为电子邮件时,我将裸机Razor和HTML标记作为Email Body。这种做法我错了吗?剃须刀页面是否允许作为电子邮件模板?



这是我的页面

  @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 />
感谢您注册到XYZ门户< br />< br />
< a style =color:#22BCE5href ={Url}>点击确认电子邮件< / a>< br />

< br />< br />
谢谢< br />
管理员(XYZ)
< / span>



更新..

  using(StreamReader reader = new StreamReader(HttpContext.Current.Server.MapPath(〜/ ContentPages / EmailConfTemplate.cshtml)))
{
body = reader.ReadToEnd();
//替换BodyName中可用的UserName和其他变量Stream
body = body.Replace({UserName},FirstName);

}

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

  MailMessage message = new MailMessage(
ApplicationWideData.fromEmailId,// From field
ToEmailId,//收件人字段
单击HyperLink以验证电子邮件ID: - AeraEnergy管理员,//电子邮件的主题
body
);


解决方案

电子邮件只能理解两种格式:纯文本和HTML 。由于Razor既不是,它也需要由一些引擎处理,以便它可以让您生成HTML。



这是在ASP中使用Razor时会发生什么.NET MVC,幕后。 Razor文件被编译成一个内部C#类,它被执行,执行结果是发送给客户端的HTML的字符串内容。



您的问题是您需要并且需要处理才能运行,只能将HTML返回为字符串,而不是发送到浏览器。之后,您可以使用HTML字符串进行任何所需的操作,包括以电子邮件的形式发送。



有几个包含此功能的软件包,成功使用了 Westwind.RazorHosting ,但您也可以使用 RazorEngine ,结果相似。我喜欢RazorHosting用于独立的非Web应用程序,RazorEngine用于Web应用程序



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

  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();
等待smtpClient.SendMailAsync(mm);


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?

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

  }

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:- AeraEnergy Admin", // Subject of the email message
    body
   );

解决方案

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.

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.

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.

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

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

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

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