7

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!

5 Answers 5

21

I believe System.Net.WebUtility.HtmlEncode will provide this functionality. MSDN reference for WebUtility.HTMLEncode

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for being first but that's .NET 4 only so I can't use unfortunately.
11

You can import the System.Web namespace and use the HttpUtility class. Altohugh it's a Web assembly I don't think it's neccesary to be running under a Web environment for it to work.

2 Comments

Thanks, not sure why I didn't find that!
Adding a dependency on System.Web is not a great idea for a class library. Use System.Net.WebUtility.HtmlEncode() instead.
2

You can use the HttpUtility class from System.Web.

Comments

2

You should really look into using the AntiXSS library of the WPL.

  1. You don't need System.Web!

  2. It uses a white-list inclusion principle (System.Web encoding uses black-listing). Only characters that are known to be safe will be trusted and all others will be properly encoded

Comments

0

How about HttpUtility.HtmlEncode?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.