Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

c# - 我可以使用ASP.NET设置HTML /电子邮件模板吗?(Can I set up HTML/Email Templates with ASP.NET?)

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.

(如果我将HTML嵌入C#字符串文字中,则很丑陋,他们将不得不担心转义。)

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.

(理想的情况是以某种方式使用.ASPX页面作为模板,然后告诉我的代码为该页面提供服务,并使用为电子邮件返回的HTML。)

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.

(我添加了一个答案,使您可以使用标准的.aspx页面作为电子邮件模板。)

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!

(您有HTML电子邮件!)

UPDATED WITH CAVEAT!!!:

(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.

(我在一些aspx页面上使用MailDefinition类就很好了,但是当尝试在运行的服务器进程中使用此类时,它失败了。)

I believe it was because the MailDefinition.CreateMailMessage() method requires a valid control to reference, even though it doesn't always do something.

(我相信这是因为MailDefinition.CreateMailMessage()方法需要一个有效的控件来引用,即使它并不总是做某事。)

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.

(因此,我建议使用aspx页面的方法,或Mun使用ascx页面的方法,这似乎要好一些。)

  ask by John Bubriski translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There's a ton of answers already here, but I stumbled upon a great article about how to use Razor with email templating.

(这里已经有很多答案,但是我偶然发现了一篇很棒的文章,关于如何将Razor与电子邮件模板一起使用。)

Razor was pushed with ASP.NET MVC 3, but MVC is not required to use Razor.

(Razor已随ASP.NET MVC 3一起推出,但使用Razor不需要MVC。)

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. "

(正如文章所指出的那样,“ Razor的最好之处在于,与它的前身(webforms)不同,它不与Web环境捆绑在一起,我们可以轻松地将其托管在Web外部并将其用作各种目的的模板引擎。”)

Generating HTML emails with RazorEngine - Part 01 - Introduction

(使用RazorEngine生成HTML电子邮件-第01部分-简介)

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

(在ASP.NET之外利用Razor模板:它们不再仅用于HTML!)

Smarter email templates in ASP.NET with RazorEngine

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

Similar Stackoverflow QA

(类似的Stackoverflow质量检查)

Templating using new RazorEngine API

(使用新的RazorEngine API进行模板)

Using Razor without MVC

(在没有MVC的情况下使用Razor)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...