Encode email address to HTML entities in C#

Here is a code snippet to convert your email address to HTML entities that will keep your email safe from email scraper programs:
Using LINQ

public string ConvertToHTMLEntities(string inputStr)
{
 return string.Join("", inputStr.ToCharArray().Select(c =>string.Format("&#{0};",(int)c)).ToArray());
}

Traditional way

public string ConvertToHTMLEntities(string inputStr)
{
 StringBuilder sb = new StringBuilder();
 char[] inputChars = inputStr.ToCharArray();
 foreach (char c in inputChars)
 {
   sb.AppendFormat("&#{0};", (int)c);
 }
 return sb.ToString();
}

The above code will convert an email address to its character entity representation.
For example, an email me@domain.com will look like me@domain.com in HTML source.

You may also like...