How to encode MailTo links in ASP.NET MVC?

I have the following mailto link in an ASP.NET MVC 5 application:

<a rel="nofollow" href="@( String.Format("mailto:?subject={0}&amp;body={1}", "The title", "The description" + "%0D%0A" + "http://theurl.xyz")"> share by email </a> 

This does not check the HTML Validator. I get an error message:

Bad mailto value :? subject = Subject & body = This is url:% 0D% 0Ahttp: // localhost: 8580 / home for the href attribute for element a: Space in the request component. Use% 20 instead of spaces.

I tried coding with HttpUtility.UrlEncode , but when I open the letter, I get the "+" signs and others in the subject and body, and I can not solve it.

+7
c # asp.net-mvc mailto url-encoding
source share
3 answers

I know this is a little old, but I came across this when I was trying to find a better way to encode mail links. I found a better way to use Uri.EscapeDataString for each parameter, and then encode the entire attribute thing using HttpUtility.HtmlAttributeEncode:

 HttpUtility.HtmlAttributeEncode( String.Format("mailto:?subject={0}&body={1}", Uri.EscapeDataString(subject), Uri.EscapeDataString(body))) 

HttpUtility.UrlEncode and HttpUtility.UrlEncodeUnicode incorrectly encode spaces - they become plus signs ("+"), which are then displayed as plus signs in the subject line / body / etc. HttpUtility.UrlPathEncode seems to fix this problem, but it does not correctly encode other characters such as ?, # and /. Uri.EscapedDataString seems to be the only method that correctly encodes all of these characters. I believe Uri.HexEscape will work the same, but it seems like this might be redundant.

Caution: I have not tested this even with a remote set of browsers and email clients

+10
source share

You need to use HttpUtility.UrlPathEncode instead of HttpUtility.UrlEncode :

 <a rel="nofollow" href="@( (String.Format("mailto:?subject={0}&body={1}", HttpUtility.UrlPathEncode("The subject line"), HttpUtility.UrlPathEncode("The body") + "%0D%0A" + "http://theurl.xyz"))))"> share by email </a> 

Note: you need the HttpUtility.UrlPathEncode parts separately, and you cannot put the HttpUtility.UrlPathEncode around the entire String.Format because HttpUtility.UrlPathEncode processes ? specially and only encodes the text before ? .

From MSDN :

You can encode the URL using the UrlEncode method or the UrlPathEncode method. However, methods return different results. The UrlEncode method converts each space into a plus sign (+). The UrlPathEncode method converts each whitespace character to the string "% 20", which is a space in the hexadecimal system.

+4
source share

This is because the razor does not know when to start processing C # code. You must explicitly specify a razor when interpreting C # ...

 <a rel="nofollow" href=" @(string.Format("mailto:?subject={0}&body={1}", ViewBag.Title, ViewBag.Description + "%0D%0A" + ViewBag.Url))>Share by Email </a> 

Edit

Answer

I got carried away with the sitanks mistakes you had in your razor. However, even after you edited your question, I still see some problems. The first problem is that you open two brackets, but close only one. The second problem is that you specify an empty email address, well, you should at least add a space (not html encoded). And the last question is that you do not actually separate the subject and body because you use ? instead of & . If you fix these problems, you should be good to go. Here is an example based on your question ...

 <a rel="nofollow" href="@(String.Format("mailto: ?subject={0}&body={1}" , "The title" , "The description" + "%0D%0A" + "http://theurl.xyz"))"> share by email </a> 

This should work as it is. But if you want to do more funky things, please read this RFC

0
source share

All Articles