简体   繁体   English

在Code-Behind中从Web.Config中检索HTML

[英]Retrieve HTML from Web.Config in Code-Behind

Web.Config: Web.Config中:

<appSettings>
    <add key="html1" value="This is &lt;br /&gt; HTML email"/>
</appSettings>

I'm trying to generate HTML formatted email. 我正在尝试生成HTML格式的电子邮件。 Prior to putting data in Web.Config I had both text and HTML emails formatted correctly. 在将数据放入Web.Config之前,我已正确格式化了文本和HTML电子邮件。

In code-behind: 在代码隐藏中:

StringBuilder emailMessageString = new StringBuilder();
emailMessageString.Append(WebConfigurationManager.AppSettings["html1"].ToString());

Output in HTML email: HTML电子邮件中的输出:

This is <br /> HTML email 这是HTML电子邮件

Expected output in HTML email: HTML电子邮件中的预期输出:

This is 这是

HTML email HTML电子邮件

UPDATE - 02-19-2015 更新 - 2015年2月19日

It looks like code I wrote to generate HTML email is not working correctly and had previously posted the code in this question (in my own answer). 看起来我编写的用于生成HTML电子邮件的代码无法正常工作,并且此前已在此问题中发布了代码(在我自己的答案中)。 Can someone spot what I'm doing wrong as it looks like only the text emails are working: 有人可以发现我做错了,因为它看起来只有文本电子邮件正常工作:

Correct Syntax for Generating HTML Email using AlternateView 使用AlternateView生成HTML电子邮件的正确语法

You do not need to decode again. 您无需再次解码。 This should solve it - 这应该解决它 -

var body = new StringBuilder();
body.Append(WebConfigurationManager.AppSettings["html1"]);

Also, ensure that you set IsBodyHtml to true. 另外,请确保将IsBodyHtml设置为true。

var message = new MailMessage();
message.IsBodyHtml = true;

Hello Brother you can use Server.HtmlDecode(string s) 你好兄弟你可以使用Server.HtmlDecode(string s)

see below code 见下面的代码

StringBuilder emailMessageString = new StringBuilder();
emailMessageString.Append(Server.HtmlDecode(WebConfigurationManager.AppSettings["html1"].ToString()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM