CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Peter's Gekko

public Blog MyNotepad : Imho { }

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

 



Comments

Peter van Ooijen said:

That doesn't work. HtmlEncode makes proper HTML out of a string. I need a valid URL.
# May 24, 2004 5:43 AM

Scott Galloway said:

Server.UrlEncode(mail.body)
# May 24, 2004 5:46 AM

Chris Slatt said:

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)
# May 24, 2004 6:54 AM

Jon Galloway said:

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).
# May 24, 2004 10:29 AM

Peter van Ooijen said:

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.

# May 24, 2004 11:10 AM

Xdx said:

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
# November 11, 2004 8:33 AM

Peter van Ooijen said:

It's in the system.web dll, part of every web app. Else add it manually to the references of your project.
# November 11, 2004 9:00 AM

Jan said:

Somehow the stuff works as a link on a page.
But not as a redirect on the server.

Anybody any clues.
# June 9, 2005 3:21 AM

pvanooijen said:

It's happening in the browser which jumps to the installed email program. There's nothing happening on the server.
# June 9, 2005 3:34 AM

Szion said:

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" )

# July 19, 2006 1:20 PM

pvanooijen said:

URLencode the body parameter to my%20web%20page.html ?
# July 20, 2006 5:04 AM

Ishai Sagi said:

pvanooijen - it doesnt work.

# August 2, 2007 12:58 AM

louise said:

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?

# August 2, 2007 1:00 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!