I have a utility class for sending emails. The emails are generated as html.
I'm having problems with encoding special characters which aren't getting displayed correctly.
However, I can't work out how to use HtmlEncode
as I don't have a current HttpContext
. Is there a .Net
class which will translate special characters to html codes without running under Asp.Net
?
I hope that makes sense, I'm a bit flaky about how all this stuff works. Thanks!
HtmlEncode(Object)Converts an object's string representation into an HTML-encoded string, and returns the encoded string. public: static System::String ^ HtmlEncode(System::Object ^ value); C# Copy.
HTML encoding ensures that text will be correctly displayed in the browser, not interpreted by the browser as HTML. For example, if a text string contains a less than sign (<) or greater than sign (>), the browser would interpret these characters as an opening or closing bracket of an HTML tag.
HTMLEncoding turns this character into "<" which is the encoded representation of the less-than sign. URLEncoding does the same, but for URLs, for which the special characters are different, although there is some overlap. Save this answer. Show activity on this post.
Any time you are trying to output data that could include untrusted html, you should use HTMLENCODE . Encodes text and merge field values for use in HTML by replacing characters that are reserved in HTML, such as the greater-than sign ( > ), with HTML entity equivalents, such as > .
I believe System.Net.WebUtility.HtmlEncode
will provide this functionality. MSDN reference for WebUtility.HTMLEncode
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With