2010-07-23 Marek Habersack <mhabersack@novell.com>
[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 #if NET_2_0
30
31 using System;
32 using System.IO;
33 using System.Web.Configuration;
34 using System.Net.Configuration;
35 using System.Collections.Generic;
36 using System.Text;
37 using System.ComponentModel;
38 using System.Web.UI;
39 using System.Net.Mail;
40 using System.Collections;
41
42 namespace System.Web.UI.WebControls
43 {
44         [BindableAttribute (false)]
45         [ParseChildren (true)]
46         //[TypeConverter ("System.Web.UI.WebControls.EmptyStringExpandableObjectConverter")]
47         public sealed class MailDefinition : IStateManager
48         {
49                 StateBag _bag = new StateBag ();
50
51                 [Editor ("System.Web.UI.Design.MailDefinitionBodyFileNameEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
52                 [DefaultValue ("")]
53                 [NotifyParentProperty (true)]
54                 [UrlProperty ("*.*")]
55                 public string BodyFileName {
56                         get { return _bag.GetString ("BodyFileName", String.Empty); }
57                         set { _bag ["BodyFileName"] = value; }
58                 }
59
60                 [DefaultValue ("")]
61                 [NotifyParentProperty (true)]
62                 public string CC {
63                         get { return _bag.GetString ("CC", String.Empty); }
64                         set { _bag ["CC"] = value; }
65                 }
66
67                 [PersistenceMode (PersistenceMode.InnerProperty)]
68                 [DefaultValue ("")]
69                 [NotifyParentProperty (true)]
70                 public EmbeddedMailObjectsCollection EmbeddedObjects {
71                         get { throw new NotImplementedException (); }
72                 }
73
74                 [NotifyParentProperty (true)]
75                 [DefaultValue ("")]
76                 public string From {
77                         get { return _bag.GetString ("From", String.Empty); }
78                         set { _bag ["From"] = value; }
79                 }
80
81                 [DefaultValue (false)]
82                 [NotifyParentProperty (true)]
83                 public bool IsBodyHtml {
84                         get { return _bag.GetBool ("IsBodyHtml", false); }
85                         set { _bag ["IsBodyHtml"] = value; }
86                 }
87
88                 [DefaultValue (MailPriority.Normal)]
89                 [NotifyParentProperty (true)]
90                 public MailPriority Priority {
91                         get { return _bag ["Priority"] == null ? MailPriority.Normal : (MailPriority) _bag ["Priority"]; }
92                         set { _bag ["Priority"] = value; }
93                 }
94
95                 [DefaultValue ("")]
96                 [NotifyParentProperty (true)]
97                 public string Subject {
98                         get { return _bag.GetString ("Subject", String.Empty); }
99                         set { _bag ["Subject"] = value; }
100                 }
101
102                 public MailMessage CreateMailMessage (string recipients, IDictionary replacements, Control owner)
103                 {
104                         if (owner == null)
105                                 throw new ArgumentNullException ("owner");
106
107                         string bodyText = null;
108
109                         if (BodyFileName.Length > 0) {
110                                 string filePath = null;
111                                 if (Path.IsPathRooted (BodyFileName))
112                                         filePath = BodyFileName;
113                                 else
114                                         filePath = HttpContext.Current.Request.MapPath (VirtualPathUtility.Combine (owner.TemplateSourceDirectory, BodyFileName));
115
116                                 using (StreamReader sr = new StreamReader (filePath)) {
117                                         bodyText = sr.ReadToEnd ();
118                                 }
119                         }
120                         else
121                                 bodyText = "";
122
123                         return CreateMailMessage (recipients, replacements, bodyText, owner);
124                 }
125
126                 public MailMessage CreateMailMessage (string recipients, IDictionary replacements, string body, Control owner)
127                 {
128                         if (owner == null)
129                                 throw new ArgumentNullException ("owner");
130
131                         MailMessage msg = new MailMessage ();
132
133                         if (CC.Length > 0)
134                                 msg.CC.Add(CC);
135
136                         msg.IsBodyHtml = IsBodyHtml;
137                         msg.Priority = Priority;
138                         msg.Subject = Subject;
139                         msg.Body = body;
140
141                         if (From.Length > 0)
142                                 msg.From = new MailAddress (From);
143                         else {
144                                 SmtpSection smtpSection = (SmtpSection) WebConfigurationManager.GetSection ("system.net/mailSettings/smtp");
145                                 if (smtpSection != null) {
146                                         if (string.IsNullOrEmpty (smtpSection.From))
147                                                 throw new HttpException ("A from e-mail address must be specified in the From property or the system.net/mailSettings/smtp config section");
148
149                                         msg.From = new MailAddress (smtpSection.From);
150                                 }
151                         }
152
153                         string [] recipientsArr = recipients.Split (',');
154                         for (int i = 0; i < recipientsArr.Length; i++)
155                                 msg.To.Add (recipientsArr [i]);
156
157                         foreach (DictionaryEntry d in replacements)
158                                 msg.Body = msg.Body.Replace ((string) d.Key, (string) d.Value);
159
160                         return msg;
161                 }
162
163                 #region IStateManager Members
164
165                 void IStateManager.LoadViewState (object state)
166                 {
167                         _bag.LoadViewState (state);
168                 }
169
170                 object IStateManager.SaveViewState () {
171                         return _bag.SaveViewState ();
172                 }
173
174                 void IStateManager.TrackViewState () {
175                         _bag.TrackViewState ();
176                 }
177
178                 bool IStateManager.IsTrackingViewState {
179                         get { return _bag.IsTrackingViewState; }
180                 }
181
182 #endregion
183         }
184 }
185
186 #endif