william
william

Reputation: 7694

Sending formatted Email Asp.Net

MailAddress to = new MailAddress(emailTo, emailToName);
MailMessage message = new MailMessage(from, to);
message.Subject = dtlSubscribe.Rows[i]["NewsLetter_Title"].ToString().Trim();
message.IsBodyHtml = true;
string msgBody = "<html><body>" + dtlSubscribe.Rows[i]["NewsLetter_Body"].ToString().Trim() + "</body></html>";
Console.WriteLine(msgBody);
message.Body = msgBody;
SmtpClient client = new SmtpClient(SMTPServer, 25);
client.Send(message);
message.Dispose();

That's how I write my email in code.

in msgBody, I put <html><body> then place the record retrieved from database.

This is what I get.

<ol> <li>Number1</li> <li>Number2</li> <li><p>Number3</p> </li> </ol> <p><a title="/NHGD/assets/0/71/90/92/b834e617-473d-49e8-beb1-ecade3afdd2f.jpg" onkeypress="this.onclick();" onclick="try{window.open('/NHGD/assets/0/71/90/92/b834e617-473d-49e8-beb1-ecade3afdd2f.jpg', 'MyImage', 'resizable=yes, scrollbars=yes, width=790, height=580')}catch(e){};return false;" href="#"><img title="DiagnosticsService1" border="0" alt="DiagnosticsService1" src="/NHGD/assets/0/71/90/92/thumb_b834e617-473d-49e8-beb1-ecade3afdd2f.jpg" /></a></p> <p> </p> <p>End<br /></p>

Apart from <html><body> which I put in myself.. my record from database is not formatted.

So, I debug a bit.. and found this..

<html><body>&lt;ol&gt;
&lt;li&gt;Number1&lt;/li&gt;
&lt;li&gt;Number2&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Number3&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;a title=&quot;/NHGD/assets/0/71/90/92/b834e617-473d-49e8-beb1-ecade
3afdd2f.jpg&quot; onkeypress=&quot;this.onclick();&quot; onclick=&quot;try{windo
w.open('/NHGD/assets/0/71/90/92/b834e617-473d-49e8-beb1-ecade3afdd2f.jpg', 'MyIm
age', 'resizable=yes, scrollbars=yes, width=790, height=580')}catch(e){};return
false;&quot; href=&quot;#&quot;&gt;&lt;img title=&quot;DiagnosticsService1&quot;
 border=&quot;0&quot; alt=&quot;DiagnosticsService1&quot; src=&quot;/NHGD/assets
/0/71/90/92/thumb_b834e617-473d-49e8-beb1-ecade3afdd2f.jpg&quot; /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;End&lt;br /&gt;&lt;/p&gt;</body></html>
<html><body>&lt;ol&gt;
&lt;li&gt;Number1&lt;/li&gt;
&lt;li&gt;Number2&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Number3&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;a title=&quot;/NHGD/assets/0/71/90/92/b834e617-473d-49e8-beb1-ecade
3afdd2f.jpg&quot; onkeypress=&quot;this.onclick();&quot; onclick=&quot;try{windo
w.open('/NHGD/assets/0/71/90/92/b834e617-473d-49e8-beb1-ecade3afdd2f.jpg', 'MyIm
age', 'resizable=yes, scrollbars=yes, width=790, height=580')}catch(e){};return
false;&quot; href=&quot;#&quot;&gt;&lt;img title=&quot;DiagnosticsService1&quot;
 border=&quot;0&quot; alt=&quot;DiagnosticsService1&quot; src=&quot;/NHGD/assets
/0/71/90/92/thumb_b834e617-473d-49e8-beb1-ecade3afdd2f.jpg&quot; /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;End&lt;br /&gt;&lt;/p&gt;</body></html>

Found out that when it is stored into database, all the special characters have been changed into that.

Which is why there is no error while saving into database.

Now, my question is how to change them back to their original characters??

So that, it will be formatted properly..

Thanks a lot..

Upvotes: 2

Views: 1438

Answers (3)

s_nair
s_nair

Reputation: 812

You can use HttpUtility HtmlDecode/HtmlEncode method to achive what you needed.

using System.Web;

var encoded = HttpUtility.HtmlEncode(unencoded);

Also, there is an interesting article Here

Alternately

public static string CustomHtmlEncode(string value) 
{    
   char[] chars = HttpUtility.HtmlEncode(YourDbString).ToCharArray(); 
   StringBuilder encodedValue = new StringBuilder(); 
   foreach(char c in chars) 
   { 
      if ((int)c > 127) // above normal ASCII 
         encodedValue.Append("&#" + (int)c + ";"); 
      else 
         encodedValue.Append(c); 
   } 
   return encodedValue.ToString(); 
}

Upvotes: 0

Niranjan Singh
Niranjan Singh

Reputation: 18290

yes this is true you can not save html data to database. you must convert your string to html encoded string using Server.HtmlEncode to avoid page validation error etc..

and if you are accessing your Html Encoded data from datatables then use Server.HtmlDecode().

Follow these for more information:

Server.HtmlEncode: http://msdn.microsoft.com/en-us/library/ms525347%28v=vs.90%29.aspx

Server.HtmlDecode: http://msdn.microsoft.com/en-us/library/hwzhtkke.aspx

String msgBody = "<html><body>" + Server.HtmlDecode(dtlSubscribe.Rows[i]["NewsLetter_Body"].ToString().Trim()) + "</body></html>";

Upvotes: 1

Junaid
Junaid

Reputation: 1755

you can use HttpUtility.HtmlDecode

Upvotes: 2

Related Questions