0

Web.Config:

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

I'm trying to generate HTML formatted email. Prior to putting data in Web.Config I had both text and HTML emails formatted correctly.

In code-behind:

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

Output in HTML email:

This is <br /> HTML email

Expected output in HTML email:

This is

HTML email

UPDATE - 02-19-2015

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

Community
  • 1
  • 1
IrishChieftain
  • 15,108
  • 7
  • 50
  • 91
  • I have a feeling it's because you are storing HTML in XML format. Do you need to un-XML the value before outputting the HTML? – Jeremy Feb 18 '15 at 22:02
  • Not sure how you mean Jeremy - I tried combinations of </> and </> in conjunction with HtmlEncode() and HtmlDecode() calls and can't get it to play right. My understanding is that they are all strings in config section... – IrishChieftain Feb 18 '15 at 22:05
  • 1
    out of curiosity, can you output the raw html that is generated using both methods? – Jeremy Feb 18 '15 at 22:12
  • 1
    Could it be that you send plain text email instead of HTML ? – Ako Feb 18 '15 at 22:21
  • I think you guys are on to something - will update question shortly and show how I'm attempting to generate the html in email. – IrishChieftain Feb 18 '15 at 22:57
  • `web.config` seems like a bad place to store HTML. Put it in a .html file. Or even better, use [Postal](http://aboutcode.net/postal/) to generate your email bodies using Razor syntax, just like you would with an MVC page. – mason Feb 19 '15 at 02:04
  • Sorry Mason, I refuse point blank to mix code and markup with Razor. – IrishChieftain Feb 19 '15 at 02:46

2 Answers2

0

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.

var message = new MailMessage();
message.IsBodyHtml = true;
Win
  • 61,100
  • 13
  • 102
  • 181
  • Updating question with your suggestion Win; I think problem might be in way I'm generating HTML in email - will update again shortly when I can narrow it down, thx :) – IrishChieftain Feb 18 '15 at 22:56
  • The reason the HTML wasn't working was because I was stupidly trying to display output in a textbox :/ – IrishChieftain Mar 03 '15 at 17:32
0

Hello Brother you can use Server.HtmlDecode(string s)

see below code

StringBuilder emailMessageString = new StringBuilder();
emailMessageString.Append(Server.HtmlDecode(WebConfigurationManager.AppSettings["html1"].ToString()));
User125
  • 108
  • 2
  • 9