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