Search code examples
c#serializationmailto

Trying to use "mailto:" URL schema to allow users to send emails w/ errors in them


In C# I have the following code:

body = body + ":::::" + Events.JSONToString();
body = System.Web.HttpUtility.HtmlEncode(body).Replace('\n', '^');
System.Diagnostics.Process.Start(
    String.Format(
        "mailto:foo@bar.com?Subject={0}&Body={1}", subject, body
    )
);

This works, but there are too many characters that will throw it off and terminate the body text prematurely.

Is there a formatting/escape routine that could help me preserve the data (and ideally the formatting)?


Solution

  • Because you are making this part of a query string you need to use Uri.EscapeDataString(body) instead of System.Web.HttpUtility.HtmlEncode(body).