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