Data Browser - Viewing Site  Sector 23 Code Bank Logged in as:  Guest  


Encode Mailto Body Text to Open in Email Client
In .NET, passing text unencoded to the body query parameter causes issues when clicking the mailto link for Outlook.
However, HtmlEncode,UrlEncode,PathEncode... none of these seem to generate valid text that Outlook can process.
So, here is a custom version that simply throws out the most problematic characters... you can use this as a starting point.

This clips the text at around 1000 chars on the assumption that your subject/url is less than 1000 chars (total URL cannot exceed 2000 chars or it doesn't work).

/// <summary>
/// Clean up text to use as body for a mailto: link that outlook can read without complaining. Strips out or reformats common problem characters.
/// </summary>
/// <param name="body"></param>
/// <param name="asReply">true to leave space above text for your reply</param>
/// <returns></returns>
public static string EncodeMailtoBody(string body, bool asReply)
// url encode, path encode, html encode... none seem to work for outlook. Just strip out most common troublesome chars.
body = body.Replace("#", " %23");
body = body.Replace("\"", "'");
body = body.Replace("\r", "%0D%0A");
body = body.Replace("&", " ");
body = body.Replace("<", " ");
body = body.Replace(">", " ");
body = "%0D%0A%0D%0A%0D%0A> >" + body; // prepend some whitespace
if (body.Length > 1010)
body = body.Substring(0, 1000) + "... (message clipped)";// url max length check
return body;

Created By: amos 4/8/2014 5:03:31 PM
Updated: 4/8/2014 5:05:55 PM