PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/MvcFutures/Mvc/MailToExtensions.cs

https://bitbucket.org/markhneedham/aspnet-mvc
C# | 90 lines | 72 code | 18 blank | 0 comment | 11 complexity | 0872910af39407d5c2e964a137a8216d MD5 | raw file
  1. namespace Microsoft.Web.Mvc {
  2. using System;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5. using System.Web;
  6. using System.Web.Mvc;
  7. using System.Web.Routing;
  8. [AspNetHostingPermission(System.Security.Permissions.SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  9. public static class MailToExtensions {
  10. public static string Mailto(this HtmlHelper helper, string linkText, string emailAddress) {
  11. return Mailto(helper, linkText, emailAddress, null, null, null, null, (IDictionary<string, object>)null);
  12. }
  13. public static string Mailto(this HtmlHelper helper, string linkText, string emailAddress, object htmlAttributes) {
  14. return Mailto(helper, linkText, emailAddress, null, null, null, null, htmlAttributes);
  15. }
  16. public static string Mailto(this HtmlHelper helper, string linkText, string emailAddress, IDictionary<string, object> htmlAttributes) {
  17. return Mailto(helper, linkText, emailAddress, null, null, null, null, htmlAttributes);
  18. }
  19. public static string Mailto(this HtmlHelper helper, string linkText, string emailAddress, string subject) {
  20. return Mailto(helper, linkText, emailAddress, subject, null, null, null, (IDictionary<string, object>)null);
  21. }
  22. public static string Mailto(this HtmlHelper helper, string linkText, string emailAddress, string subject, object htmlAttributes) {
  23. return Mailto(helper, linkText, emailAddress, subject, null, null, null, htmlAttributes);
  24. }
  25. public static string Mailto(this HtmlHelper helper, string linkText, string emailAddress, string subject, IDictionary<string, object> htmlAttributes) {
  26. return Mailto(helper, linkText, emailAddress, subject, null, null, null, htmlAttributes);
  27. }
  28. public static string Mailto(this HtmlHelper helper, string linkText, string emailAddress, string subject, string body, string cc, string bcc, object htmlAttributes) {
  29. return Mailto(helper, linkText, emailAddress, subject, body, cc, bcc, new RouteValueDictionary(htmlAttributes));
  30. }
  31. public static string Mailto(this HtmlHelper helper, string linkText, string emailAddress, string subject,
  32. string body, string cc, string bcc, IDictionary<string, object> htmlAttributes) {
  33. if (emailAddress == null) {
  34. throw new ArgumentNullException("emailAddress"); // TODO: Resource message
  35. }
  36. if (linkText == null) {
  37. throw new ArgumentNullException("linkText"); // TODO: Resource message
  38. }
  39. string mailToUrl = "mailto:" + emailAddress;
  40. List<string> mailQuery = new List<string>();
  41. if (!String.IsNullOrEmpty(subject)) {
  42. mailQuery.Add("subject=" + helper.Encode(subject));
  43. }
  44. if (!String.IsNullOrEmpty(cc)) {
  45. mailQuery.Add("cc=" + helper.Encode(cc));
  46. }
  47. if (!String.IsNullOrEmpty(bcc)) {
  48. mailQuery.Add("bcc=" + helper.Encode(bcc));
  49. }
  50. if (!String.IsNullOrEmpty(body)) {
  51. string encodedBody = helper.Encode(body);
  52. encodedBody = encodedBody.Replace(Environment.NewLine, "%0A");
  53. mailQuery.Add("body=" + encodedBody);
  54. }
  55. string query = string.Empty;
  56. for (int i = 0; i < mailQuery.Count; i++) {
  57. query += mailQuery[i];
  58. if (i < mailQuery.Count - 1) {
  59. query += "&";
  60. }
  61. }
  62. if (query.Length > 0) {
  63. mailToUrl += "?" + query;
  64. }
  65. TagBuilder mailtoAnchor = new TagBuilder("a");
  66. mailtoAnchor.MergeAttribute("href", mailToUrl);
  67. mailtoAnchor.MergeAttributes(htmlAttributes, true);
  68. mailtoAnchor.InnerHtml = linkText;
  69. return mailtoAnchor.ToString();
  70. }
  71. }
  72. }