2 // System.Web.UI.WebControls.MailDefinition.cs
5 // Igor Zelmanovich (igorz@mainsoft.com)
7 // (C) 2006 Mainsoft, Inc (http://www.mainsoft.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
33 using System.Web.Configuration;
34 using System.Net.Configuration;
35 using System.Collections.Generic;
37 using System.ComponentModel;
39 using System.Net.Mail;
40 using System.Collections;
42 namespace System.Web.UI.WebControls
44 [BindableAttribute (false)]
45 [ParseChildren (true)]
46 //[TypeConverter ("System.Web.UI.WebControls.EmptyStringExpandableObjectConverter")]
47 public sealed class MailDefinition : IStateManager
49 StateBag _bag = new StateBag ();
51 [Editor ("System.Web.UI.Design.MailDefinitionBodyFileNameEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
53 [NotifyParentProperty (true)]
55 public string BodyFileName {
56 get { return _bag.GetString ("BodyFileName", String.Empty); }
57 set { _bag ["BodyFileName"] = value; }
61 [NotifyParentProperty (true)]
63 get { return _bag.GetString ("CC", String.Empty); }
64 set { _bag ["CC"] = value; }
67 [PersistenceMode (PersistenceMode.InnerProperty)]
69 [NotifyParentProperty (true)]
70 public EmbeddedMailObjectsCollection EmbeddedObjects {
71 get { throw new NotImplementedException (); }
74 [NotifyParentProperty (true)]
77 get { return _bag.GetString ("From", String.Empty); }
78 set { _bag ["From"] = value; }
81 [DefaultValue (false)]
82 [NotifyParentProperty (true)]
83 public bool IsBodyHtml {
84 get { return _bag.GetBool ("IsBodyHtml", false); }
85 set { _bag ["IsBodyHtml"] = value; }
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; }
96 [NotifyParentProperty (true)]
97 public string Subject {
98 get { return _bag.GetString ("Subject", String.Empty); }
99 set { _bag ["Subject"] = value; }
102 public MailMessage CreateMailMessage (string recipients, IDictionary replacements, Control owner)
105 throw new ArgumentNullException ("owner");
107 string bodyText = null;
109 if (BodyFileName.Length > 0) {
110 string filePath = null;
111 if (Path.IsPathRooted (BodyFileName))
112 filePath = BodyFileName;
114 filePath = HttpContext.Current.Request.MapPath (VirtualPathUtility.Combine (owner.TemplateSourceDirectory, BodyFileName));
116 using (StreamReader sr = new StreamReader (filePath)) {
117 bodyText = sr.ReadToEnd ();
123 return CreateMailMessage (recipients, replacements, bodyText, owner);
126 public MailMessage CreateMailMessage (string recipients, IDictionary replacements, string body, Control owner)
129 throw new ArgumentNullException ("owner");
131 MailMessage msg = new MailMessage ();
136 msg.IsBodyHtml = IsBodyHtml;
137 msg.Priority = Priority;
138 msg.Subject = Subject;
142 msg.From = new MailAddress (From);
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");
149 msg.From = new MailAddress (smtpSection.From);
153 string [] recipientsArr = recipients.Split (',');
154 for (int i = 0; i < recipientsArr.Length; i++)
155 msg.To.Add (recipientsArr [i]);
157 foreach (DictionaryEntry d in replacements)
158 msg.Body = msg.Body.Replace ((string) d.Key, (string) d.Value);
163 #region IStateManager Members
165 void IStateManager.LoadViewState (object state)
167 _bag.LoadViewState (state);
170 object IStateManager.SaveViewState () {
171 return _bag.SaveViewState ();
174 void IStateManager.TrackViewState () {
175 _bag.TrackViewState ();
178 bool IStateManager.IsTrackingViewState {
179 get { return _bag.IsTrackingViewState; }