Merge pull request #2916 from ludovic-henry/fix-40306
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / MailDefinition.cs
1 //
2 // System.Web.UI.WebControls.MailDefinition.cs
3 //
4 // Authors:
5 //      Igor Zelmanovich (igorz@mainsoft.com)
6 //
7 // (C) 2006 Mainsoft, Inc (http://www.mainsoft.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29
30 using System;
31 using System.IO;
32 using System.Web.Configuration;
33 using System.Net.Configuration;
34 using System.Collections.Generic;
35 using System.Text;
36 using System.ComponentModel;
37 using System.Web.UI;
38 using System.Net.Mail;
39 using System.Collections;
40
41 namespace System.Web.UI.WebControls
42 {
43         [BindableAttribute (false)]
44         [ParseChildren (true)]
45         //[TypeConverter ("System.Web.UI.WebControls.EmptyStringExpandableObjectConverter")]
46         public sealed class MailDefinition : IStateManager
47         {
48                 StateBag _bag = new StateBag ();
49
50                 [Editor ("System.Web.UI.Design.MailDefinitionBodyFileNameEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
51                 [DefaultValue ("")]
52                 [NotifyParentProperty (true)]
53                 [UrlProperty ("*.*")]
54                 public string BodyFileName {
55                         get { return _bag.GetString ("BodyFileName", String.Empty); }
56                         set { _bag ["BodyFileName"] = value; }
57                 }
58
59                 [DefaultValue ("")]
60                 [NotifyParentProperty (true)]
61                 public string CC {
62                         get { return _bag.GetString ("CC", String.Empty); }
63                         set { _bag ["CC"] = value; }
64                 }
65
66                 [PersistenceMode (PersistenceMode.InnerProperty)]
67                 [DefaultValue ("")]
68                 [NotifyParentProperty (true)]
69                 public EmbeddedMailObjectsCollection EmbeddedObjects {
70                         get { throw new NotImplementedException (); }
71                 }
72
73                 [NotifyParentProperty (true)]
74                 [DefaultValue ("")]
75                 public string From {
76                         get { return _bag.GetString ("From", String.Empty); }
77                         set { _bag ["From"] = value; }
78                 }
79
80                 [DefaultValue (false)]
81                 [NotifyParentProperty (true)]
82                 public bool IsBodyHtml {
83                         get { return _bag.GetBool ("IsBodyHtml", false); }
84                         set { _bag ["IsBodyHtml"] = value; }
85                 }
86
87                 [DefaultValue (MailPriority.Normal)]
88                 [NotifyParentProperty (true)]
89                 public MailPriority Priority {
90                         get { return _bag ["Priority"] == null ? MailPriority.Normal : (MailPriority) _bag ["Priority"]; }
91                         set { _bag ["Priority"] = value; }
92                 }
93
94                 [DefaultValue ("")]
95                 [NotifyParentProperty (true)]
96                 public string Subject {
97                         get { return _bag.GetString ("Subject", String.Empty); }
98                         set { _bag ["Subject"] = value; }
99                 }
100
101                 public MailMessage CreateMailMessage (string recipients, IDictionary replacements, Control owner)
102                 {
103                         if (owner == null)
104                                 throw new ArgumentNullException ("owner");
105
106                         string bodyText = null;
107
108                         if (BodyFileName.Length > 0) {
109                                 string filePath = null;
110                                 if (Path.IsPathRooted (BodyFileName))
111                                         filePath = BodyFileName;
112                                 else
113                                         filePath = HttpContext.Current.Request.MapPath (VirtualPathUtility.Combine (owner.TemplateSourceDirectory, BodyFileName));
114
115                                 using (StreamReader sr = new StreamReader (filePath)) {
116                                         bodyText = sr.ReadToEnd ();
117                                 }
118                         }
119                         else
120                                 bodyText = "";
121
122                         return CreateMailMessage (recipients, replacements, bodyText, owner);
123                 }
124
125                 public MailMessage CreateMailMessage (string recipients, IDictionary replacements, string body, Control owner)
126                 {
127                         if (owner == null)
128                                 throw new ArgumentNullException ("owner");
129
130                         MailMessage msg = new MailMessage ();
131
132                         if (CC.Length > 0)
133                                 msg.CC.Add(CC);
134
135                         msg.IsBodyHtml = IsBodyHtml;
136                         msg.Priority = Priority;
137                         msg.Subject = Subject;
138                         msg.Body = body;
139
140                         if (From.Length > 0)
141                                 msg.From = new MailAddress (From);
142                         else {
143                                 SmtpSection smtpSection = (SmtpSection) WebConfigurationManager.GetSection ("system.net/mailSettings/smtp");
144                                 if (smtpSection != null) {
145                                         if (string.IsNullOrEmpty (smtpSection.From))
146                                                 throw new HttpException ("A from e-mail address must be specified in the From property or the system.net/mailSettings/smtp config section");
147
148                                         msg.From = new MailAddress (smtpSection.From);
149                                 }
150                         }
151
152                         string [] recipientsArr = recipients.Split (',');
153                         for (int i = 0; i < recipientsArr.Length; i++)
154                                 msg.To.Add (recipientsArr [i]);
155
156                         foreach (DictionaryEntry d in replacements)
157                                 msg.Body = msg.Body.Replace ((string) d.Key, (string) d.Value);
158
159                         return msg;
160                 }
161
162                 #region IStateManager Members
163
164                 void IStateManager.LoadViewState (object state)
165                 {
166                         _bag.LoadViewState (state);
167                 }
168
169                 object IStateManager.SaveViewState () {
170                         return _bag.SaveViewState ();
171                 }
172
173                 void IStateManager.TrackViewState () {
174                         _bag.TrackViewState ();
175                 }
176
177                 bool IStateManager.IsTrackingViewState {
178                         get { return _bag.IsTrackingViewState; }
179                 }
180
181 #endregion
182         }
183 }
184