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

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

问题描述

我正在开发一个会发送大量电子邮件的网站.我想设置页眉和页脚文本,或者甚至是模板,以允许用户在需要时轻松编辑这些电子邮件.

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.

如果我将 HTML 嵌入到 C# 字符串文字中,这很丑陋,他们将不得不担心转义.为页眉和页脚包含平面文件可能会起作用,但感觉有些不对劲.

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.

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

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?

更新:
我添加了一个答案,使您能够使用标准的 .aspx 页面作为电子邮件模板.只需像往常一样替换所有变量,使用数据绑定等.然后捕获页面的输出,瞧!你有你的 HTML 电子邮件!

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!

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

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.

推荐答案

这里已经有大量答案,但我偶然发现了一篇关于如何将 Razor 与电子邮件模板结合使用的精彩文章.Razor 是用 ASP.NET MVC 3 推送的,但使用 Razor 不需要 MVC.这是制作电子邮件模板的非常巧妙的处理

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

正如文章指出的那样,Razor 最好的地方在于,与它的前身(网络表单)不同,它不与网络环境绑定,我们可以轻松地将其托管在网络之外,并将其用作各种用途的模板引擎."

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

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

利用 ASP.NET 之外的 Razor 模板:它们不再仅适用于 HTML!

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

类似的 Stackoverflow QA

使用新的 RazorEngine API 进行模板

在没有 MVC 的情况下使用 Razor

是否可以使用 Razor Viewasp.net 外的引擎

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

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