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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…