broguyman
broguyman

Reputation: 1426

Ampersand in query string messing up simple mailto of a link in the body

I'm trying to do a simple mailto inside my C# ASP.Net web app.

string url = HttpContext.Current.Request.Url.AbsoluteUri;
System.Diagnostics.Process.Start("mailto:?subject=View Rig Map&body=" + url);

However if the url has a query string with an ampersand (&) separating the name-value pairs like so "http://localhost:51771/MuseumViewer.aspx?MuseumIDs=3301&CountryIDs=1" the link it cut off in the body of the email at "http://localhost:51771/MuseumViewer.aspx?MuseumIDs=3301."

I don't really want to do anything fancy because all I need to do is have the link in the body of the email. Can anyone help me with this? Would it work if I put the mailto on the client side?

UPDATE with SOLUTION I'm having a tough time deciding on who to pick as the answer but here is the solution I used:

string url = HttpContext.Current.Request.Url.AbsoluteUri;
string link = Server.UrlEncode(url);
System.Diagnostics.Process.Start("mailto:?subject=View Rig Map&body=" + link);

Upvotes: 2

Views: 3155

Answers (2)

Cinchoo
Cinchoo

Reputation: 6332

Add reference to System.Web to your project.

Use the below lines in your app

string url = "http://localhost:51771/MuseumViewer.aspx?MuseumIDs=3301&CountryIDs=1";
System.Diagnostics.Process.Start("mailto:?subject=View Rig Map&body=" + System.Web.HttpUtility.UrlEncodeUnicode(url));

Upvotes: 2

mgiuffrida
mgiuffrida

Reputation: 3579

%26 is the URL escape code for an ampersand. Try running UrlEncode() on the url.

Upvotes: 5

Related Questions