tags:

views:

570

answers:

2

I have an ASP.NET/C# application, part of which converts WWW links to mailto links in an HTML email.

For example, if I have a link such as:

www.site.com

It gets rewritten as:

mailto:[email protected]?Subject=www.site.com

This works extremely well, until I run into URLs with ampersands, which then causes the subject to be truncated.

For example the link:

www.site.com?val1=a&val2=b

Shows up as:

mailto:[email protected]?Subject=www.site.com?val1=a&val2=b

Which is exactly what I want, but then when clicked, it creates a message with:

subject=www.site.com?val1=a

Which has dropped the &val2, which makes sense as & is the delimiter in a mailto command.
So, I have tried various other was to work around this with no success.
I have tried implicitly quoting the subject='' part and that did nothing.

I (in C#) replace '&' with & which Live Mail and Thunderbird just turn back into:

www.site.com?val1=a&val2=b

I replaced '&' with '%26' which resulted in:

mailto:[email protected]?Subject=www.site.com?val1=a%26amp;val2=b

In the mail with the subject:

www.site.com?val1=a&val2=b


EDIT:

In response to how URL is being built, this is much trimmed down but is the gist of it. In place of the att.Value.Replace I have tried System.Web.HtmlUtility.URLEncode calls which also results in a failure

HtmlAgilityPack.HtmlNodeCollection nodes =doc.DocumentNode.SelectNodes("//a[@href]");
foreach (HtmlAgilityPack.HtmlNode link in nodes)
{
  HtmlAgilityPack.HtmlAttribute att = link.Attributes["href"];
  att.Value = att.Value.Replace("&", "%26");
}
+8  A: 

Try mailto:[email protected]?Subject=www.site.com?val1=a%26val2=b

& is an HTML escape code, whereas %26 is a URL escape code. Since it's a URL, that's all you need.

EDIT: I figured that's how you were building your URL. Don't build URLs that way! You need to get the %26 in there before you let anything else parse or escape it. If you really must do it this way (which you really should try to avoid), then you should search for "&" instead of just "&" because the string has already been HTML escaped at this point.

So, ideally, you build your URL properly before it's HTML escaped. If you can't do it properly, at least search for the right string instead of the wrong one. "&" is the wrong one.

Welbog
Serapth
@Serapth: That doesn't make any sense. You have a scenario in your question where you tried '%26amp;', which would exhibit the output you're saying, but my URL should not have that problem. How exactly are you building your URL?
Welbog
@Welbog, edited main post to answer your question as code formating is awful in comments. I agree though, the results make no sense, but after adding that last replace call it goes to the format 'www.site.com?val1=a%26amp;val2=b' within my mail client.
Serapth
ARG! I can't believe how long I looked at this without considering that! :( The result of a%26amp; should have been a clear indicator of what was going wrong. Unfortunatley sometimes you get so fixated on a problem you forget to realize that your input may infact be the cause, especially when the debugger shows you everything is A-ok. Thank you so very very much. Working solo means no peer review perhaps the biggest thing I miss. Your insight was a godsend for me. I was going to put a bounty on this problem as I hadn't seen your edit. I still will when I can, to fully credit you. Thanks!
Serapth
+1  A: 

You cant put any character as subject. You could try using System.Web.HttpUtility.URLEncode function on the subject´s value...

Lucas
I tried this with no success, mail client becomes unable to read links after being passed through UrlEncode.
Serapth