Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I deal with ampersands in a mail client's mailto links?

Tags:

c#

email

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");
}
like image 371
Serapth Avatar asked Jun 08 '09 15:06

Serapth


People also ask

How do I pass HTML formatted body in mailto?

The Mailto format does not support HTML code emails. Outlook was used at 2003, but to become compliant with the mailto: standard they removed that functionality. But you can Use %0D%0A for a line break in HTML body.

How do I put subject and body in mailto link?

subject=<subject> to the mailto tag. For example, the complete tag would look similar to the example below. You can also add body text by adding &body=body to the end of the tag, as shown in the example below. You can also include &cc= or &bcc= to fill out the CC and BCC fields.


2 Answers

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

&amp; 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 "&amp;" 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.

like image 111
Welbog Avatar answered Oct 10 '22 04:10

Welbog


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

like image 1
Lucas Avatar answered Oct 10 '22 05:10

Lucas