Reputation: 7694
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><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-ecade
3afdd2f.jpg" onkeypress="this.onclick();" onclick="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;" 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></body></html>
<html><body><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-ecade
3afdd2f.jpg" onkeypress="this.onclick();" onclick="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;" 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></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
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
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