1 // Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
2 
3 using System;
4 using System.Collections.Generic;
5 using System.Diagnostics.CodeAnalysis;
6 using System.Web.Mvc;
7 
8 namespace Microsoft.Web.Mvc
9 {
10     [SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "MailTo", Justification = "This is correctly cased.")]
11     public static class MailToExtensions
12     {
Mailto(this HtmlHelper helper, string linkText, string emailAddress)13         public static MvcHtmlString Mailto(this HtmlHelper helper, string linkText, string emailAddress)
14         {
15             return Mailto(helper, linkText, emailAddress, null, null, null, null, null);
16         }
17 
Mailto(this HtmlHelper helper, string linkText, string emailAddress, object htmlAttributes)18         public static MvcHtmlString Mailto(this HtmlHelper helper, string linkText, string emailAddress, object htmlAttributes)
19         {
20             return Mailto(helper, linkText, emailAddress, null, null, null, null, htmlAttributes);
21         }
22 
Mailto(this HtmlHelper helper, string linkText, string emailAddress, IDictionary<string, object> htmlAttributes)23         public static MvcHtmlString Mailto(this HtmlHelper helper, string linkText, string emailAddress, IDictionary<string, object> htmlAttributes)
24         {
25             return Mailto(helper, linkText, emailAddress, null, null, null, null, htmlAttributes);
26         }
27 
Mailto(this HtmlHelper helper, string linkText, string emailAddress, string subject)28         public static MvcHtmlString Mailto(this HtmlHelper helper, string linkText, string emailAddress, string subject)
29         {
30             return Mailto(helper, linkText, emailAddress, subject, null, null, null, null);
31         }
32 
Mailto(this HtmlHelper helper, string linkText, string emailAddress, string subject, object htmlAttributes)33         public static MvcHtmlString Mailto(this HtmlHelper helper, string linkText, string emailAddress, string subject, object htmlAttributes)
34         {
35             return Mailto(helper, linkText, emailAddress, subject, null, null, null, htmlAttributes);
36         }
37 
Mailto(this HtmlHelper helper, string linkText, string emailAddress, string subject, IDictionary<string, object> htmlAttributes)38         public static MvcHtmlString Mailto(this HtmlHelper helper, string linkText, string emailAddress, string subject, IDictionary<string, object> htmlAttributes)
39         {
40             return Mailto(helper, linkText, emailAddress, subject, null, null, null, htmlAttributes);
41         }
42 
Mailto(this HtmlHelper helper, string linkText, string emailAddress, string subject, string body, string cc, string bcc, object htmlAttributes)43         public static MvcHtmlString Mailto(this HtmlHelper helper, string linkText, string emailAddress, string subject, string body, string cc, string bcc, object htmlAttributes)
44         {
45             return Mailto(helper, linkText, emailAddress, subject, body, cc, bcc, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
46         }
47 
Mailto(this HtmlHelper helper, string linkText, string emailAddress, string subject, string body, string cc, string bcc, IDictionary<string, object> htmlAttributes)48         public static MvcHtmlString Mailto(this HtmlHelper helper, string linkText, string emailAddress, string subject,
49                                            string body, string cc, string bcc, IDictionary<string, object> htmlAttributes)
50         {
51             if (emailAddress == null)
52             {
53                 throw new ArgumentNullException("emailAddress"); // TODO: Resource message
54             }
55             if (linkText == null)
56             {
57                 throw new ArgumentNullException("linkText"); // TODO: Resource message
58             }
59 
60             string mailToUrl = "mailto:" + emailAddress;
61 
62             List<string> mailQuery = new List<string>();
63             if (!String.IsNullOrEmpty(subject))
64             {
65                 mailQuery.Add("subject=" + helper.Encode(subject));
66             }
67 
68             if (!String.IsNullOrEmpty(cc))
69             {
70                 mailQuery.Add("cc=" + helper.Encode(cc));
71             }
72 
73             if (!String.IsNullOrEmpty(bcc))
74             {
75                 mailQuery.Add("bcc=" + helper.Encode(bcc));
76             }
77 
78             if (!String.IsNullOrEmpty(body))
79             {
80                 string encodedBody = helper.Encode(body);
81                 encodedBody = encodedBody.Replace(Environment.NewLine, "%0A");
82                 mailQuery.Add("body=" + encodedBody);
83             }
84 
85             string query = String.Empty;
86             for (int i = 0; i < mailQuery.Count; i++)
87             {
88                 query += mailQuery[i];
89                 if (i < mailQuery.Count - 1)
90                 {
91                     query += "&";
92                 }
93             }
94             if (query.Length > 0)
95             {
96                 mailToUrl += "?" + query;
97             }
98 
99             TagBuilder mailtoAnchor = new TagBuilder("a");
100             mailtoAnchor.MergeAttribute("href", mailToUrl);
101             mailtoAnchor.MergeAttributes(htmlAttributes, true);
102             mailtoAnchor.InnerHtml = linkText;
103             return MvcHtmlString.Create(mailtoAnchor.ToString());
104         }
105     }
106 }
107