Peter's Gekko

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Mailto links

Recently my neighbour Warnar blogged a little on sending mail from an app. He explained how to use the smtpserver on the webserver. Recently I needed some mail functionality on the client machine. A web-page should start an email message to be sent by Outlook (Express) or whatever other client used.

This is a snap using mailto links. Clicking a mailtolink will fire up the emailclient, the browser will take care of the details. The mailto protocol supports several parameters, see here for more details. Take this link:

mailto:Peter@Gekko-Software.nl?subject=From the weblog&body=Your comments here

Click it to mail me (Spam filter is active). The link is typed directly into the navigateUrl property of a hyperlink. To customize it you use databinding.

My app had to generate messages with a body which contained a new link to some web page. At first sight this code would generate that :

result = string.Format("mailto:{0}?subject={1}&body={2}", email, subject, body);

It did not work, as the link in the body often contains an &-character. This character separates parameters of the mailto-link. The result would be that anything after the & will not be in the body. Another thing to watch is that a link cannot return line-feeds. And it does not like “'s either. As I did not find anything in the framework which will creat a valid link out of just any text I solved it by applying several Replace calls to the string.

string quote = new string('"', 1);
body = mail.Body.Replace(System.Environment.NewLine, "%0A").Replace(quote, "%22").Replace("&", "%26");

Have I overlooked something in the framework ? Is this a full clean ?

The result is that my web pages now contain fully functional mailto's. Customer loves it.

Peter

 


Posted 05-24-2004 2:23 PM by pvanooijen
Filed under: ,

[Advertisement]

Comments

William Bartholomew wrote RE: Mailto links
on 05-24-2004 5:19 AM
You would be best to:

body = System.Web.HttpUtility.Server.HtmlEncode( mail.Body )

You may need to include a reference to System.Web if this is a Windows Forms application.
Peter van Ooijen wrote re: Mailto links
on 05-24-2004 5:43 AM
That doesn't work. HtmlEncode makes proper HTML out of a string. I need a valid URL.
Scott Galloway wrote re: Mailto links
on 05-24-2004 5:46 AM
Server.UrlEncode(mail.body)
Chris Slatt wrote re: Mailto links
on 05-24-2004 6:54 AM
Yep, URLEncode is what you need. If this is ASP.NET you can use Server.UrlEncode(mail.body) like Scott said, or if it is Windows Forms, HttpUtility comes to the rescue with System.Web.HttpUtility.UrlEncode(mail.body)
Jon Galloway wrote re: Mailto links
on 05-24-2004 10:29 AM
While you're programatically generating your mailto links, you might as well make them unreadable by robots.

There are some simple ways: http://www.devarticles.com/c/a/Development-Cycles/5-Web-Design-Tips-You-Cant-Live-Without/1/

And you can get a little more sophisticated with a javascript decode function: http://216.239.53.104/search?q=cache:Jj7bOfrL33MJ:www.codeproject.com/aspnet/NoSpamEmailHyperlink.asp+asp.net+mailto+encode+spam&hl=en&start=1 (linking to Google cache because http://www.codeproject.com/aspnet/NoSpamEmailHyperlink.asp is giving internal server error).
Peter van Ooijen wrote re: Mailto links
on 05-24-2004 11:10 AM
Gentlemen,

thanks a lot for all your feedback. I have been toying with URLEncode but it does a little to much. Like replacing all blanks in the body of the email with +'s. Does not read to well and the result is that the URL in the body is no longer recognized as an url. For the moment I will stay with my original code. Untill the user comes up with some new character which will break things.

Jon, encoding the email address would be an idea. I am too generous with mine (it's in this post as well) and I do have a fat spam box. My excuse not doing it for the app I'm generating the links for is that that is an intraweb. If the page escapes to the outer world we are facing larger problems than just an email address.

Xdx wrote re: Mailto links
on 11-11-2004 8:33 AM
Well I wanna use de system.web.HttpUtility but I can't, I don't know Why, Do I need to have installed the asp.net?
Please if someone can help, i'd appreciate to much his help
Peter van Ooijen wrote re: Mailto links
on 11-11-2004 9:00 AM
It's in the system.web dll, part of every web app. Else add it manually to the references of your project.
Jan wrote re: Mailto links
on 06-09-2005 3:21 AM
Somehow the stuff works as a link on a page.
But not as a redirect on the server.

Anybody any clues.
pvanooijen wrote re: Mailto links
on 06-09-2005 3:34 AM
It's happening in the browser which jumps to the installed email program. There's nothing happening on the server.
Szion wrote re: Mailto links
on 07-19-2006 1:20 PM
Anyone know how to get Outlook 2003 to recognize a URL in the body parameter of a mailto link that contains spaces?  (e.g. href="mailto:me@somewhere.com?subject=mail&body=http://myurl.com/my web page.html" )

pvanooijen wrote re: Mailto links
on 07-20-2006 5:04 AM
URLencode the body parameter to my%20web%20page.html ?
Ishai Sagi wrote re: Mailto links
on 08-02-2007 12:58 AM

pvanooijen - it doesnt work.

louise wrote re: Mailto links
on 08-02-2007 1:00 AM

Using an encoded URL and selecting the mailto link results in Outlook converting the %20 back to the space and means that it is not recognised as a full URL link within the email.  Is there a way around that?

Steve wrote re: Mailto links
on 02-17-2009 11:38 AM

URL/URI Encode doesnt work because it will can escape things to 2 or 3 urlcodes. e.g. £ =  %2c%a3

and the email client (well outlook anyway reads that as 2 values.

On the other hand your code can be broken with '#' :)

Add a Comment

(required)  
(optional)
(required)  
Remember Me?