URI constructor mistakenly encodes the "?" on mailto request when special characters are present

41 Views Asked by At

I've run into this scenario specifically with the "’" character as part of the body of the e-mail but I think it happens for other characters.

    string mailUrl1 = "mailto:?subject=Testing escape characters&body=It**’**s a good test\n\nFor Sure";

    var shareUri1 = new Uri(
                    mailUrl1,
                    UriKind.Absolute);

shareUri1.ToString(): mailto:%3Fsubject=Testing escape characters&body=It’s a good test%0A%0AFor Sure?subject=Testing escape characters&body=It’s a good test\n\nFor Sure

    string mailUrl2 = "mailto:?subject=Testing escape characters&body=It**'**s a good test\n\nFor Sure";

    var shareUri2 = new Uri(
                    mailUrl2,
                    UriKind.Absolute);

shareUri1.ToString(): mailto:?subject=Testing escape characters&body=It's a good test\n\nFor Sure

For some reason the presence of a "’" character on the first test messes up the URI parameters completely.

I was able to encode the contents of the parameters beforehand but it doesn't help as there's no way to then generate the URI object without automatically encoding again (construction with the bool parameter is deprecated). The only way I could make this work without changing the string is by not using the URI class, and working directly with strings instead.

Another weird behavior: If I fill the "mailto" part of the query, the URI is able to understand it properly. For example the following string works fine: mailto:[email protected]?subject=Testing escape characters&body=It’s a good test\n\nFor Sure

1 - Can someone explain why this is happening? 2 - Is it possible to generate the URI object properly in this case? Maybe by configuring the encoding or something?

Thank you.

0

There are 0 best solutions below