我可以使用ASP.NET设置HTML /电子邮件模板吗? [英] Can I set up HTML/Email Templates with ASP.NET?

查看:154
本文介绍了我可以使用ASP.NET设置HTML /电子邮件模板吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个网站上发送大量的电子邮件。我想设置头文件和页脚文本,甚至可以设置模板,以便用户可以轻松地编辑这些电子邮件,如果他们需要。



如果我将HTML内嵌C#字符串文字,这是丑陋的,他们不得不担心转义。包括页眉和页脚的平面文件可能会起作用,但有些事情只是感觉不到。



使用 .ASPX 页面作为模板,然后只是告诉我的代码来提供该页面,并使用返回的电子邮件的HTML。





更新:

我添加了一个让你使用标准的.aspx页面作为电子邮件模板。只需替换所有的变量,就像你通常会使用数据绑定等等。然后只是捕获页面的输出,瞧瞧!你有你的HTML电子邮件!



更新与CAVEAT !!!
我正在使用一些MailDefinition类aspx页面很好,但是当在运行的服务器进程中尝试使用此类时,它失败。我相信这是因为MailDefinition.CreateMailMessage()方法需要一个有效的控件来引用,即使它并不总是做某事。因此,我会推荐我使用aspx页面的方法,或者使用一个ascx页面的Mun的方法,这似乎更好一点。

解决方案

这里已经有很多的答案,但我偶然发现了一篇关于如何使用Razor和电子邮件模板的伟大文章。 Razor被推荐使用ASP.NET MVC 3,但MVC不需要使用Razor。这是做电子邮件模板的非常光滑的处理



正如文章所指出的那样,剃刀的最好的东西是,与其前身(webforms)不同,它与网络无关环境中,我们可以轻松地将其托管在网络外面,并将其用作各种目的的模板引擎。





利用在ASP.NET之外的剃刀模板:它们不仅仅适用于HTML!

$>>使用RazorEngine的ASP.NET中更智能的电子邮件模板



类似的Stackoverflow QA



Templat使用新的RazorEngine API



使用没有MVC的Razor



是否可以在asp.net之外使用Razor View Engine


I'm working on a site that will send out a significant number of emails. I want to set up both header and footer text, or maybe even templates to allow the users to easily edit these emails if they need to.

If I embed the HTML inside C# string literals, it's ugly and they would have to worry about escaping. Including flat files for the header and footer might work, but something about it just doesn't feel right.

What would be ideal what be to use a .ASPX page as a template somehow, then just tell my code to serve that page, and use the HTML returned for the email.

Is there a nice and easy way to do this? Is there a better way to go about solving this problem?

Updated:
I added an answer that enables you to use a standard .aspx page as the email template. Just replace all the variables like you normally would, use databinding, etc. Then just capture the output of the page, and voila! You have your HTML email!

UPDATED WITH CAVEAT!!!:
I was using the MailDefinition class on some aspx pages just fine, but when trying to use this class during a server process that was running, it failed. I believe it was because the MailDefinition.CreateMailMessage() method requires a valid control to reference, even though it doesn't always do something. Because of this, I would recommend my approach using an aspx page, or Mun's approach using an ascx page, which seems a little better.

解决方案

There's a ton of answers already here, but I stumbled upon a great article about how to use Razor with email templating. Razor was pushed with ASP.NET MVC 3, but MVC is not required to use Razor. This is pretty slick processing of doing email templates

As the article identifies, "The best thing of Razor is that unlike its predecessor(webforms) it is not tied with the web environment, we can easily host it outside the web and use it as template engine for various purpose. "

Generating HTML emails with RazorEngine - Part 01 - Introduction

Leveraging Razor Templates Outside of ASP.NET: They’re Not Just for HTML Anymore!

Smarter email templates in ASP.NET with RazorEngine

Similar Stackoverflow QA

Templating using new RazorEngine API

Using Razor without MVC

Is it possible to use Razor View Engine outside asp.net

这篇关于我可以使用ASP.NET设置HTML /电子邮件模板吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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