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.
32 using System.Web.Configuration;
33 using System.Net.Configuration;
34 using System.Collections.Generic;
36 using System.ComponentModel;
38 using System.Net.Mail;
39 using System.Collections;
41 namespace System.Web.UI.WebControls
43 [BindableAttribute (false)]
44 [ParseChildren (true)]
45 //[TypeConverter ("System.Web.UI.WebControls.EmptyStringExpandableObjectConverter")]
46 public sealed class MailDefinition : IStateManager
48 StateBag _bag = new StateBag ();
50 [Editor ("System.Web.UI.Design.MailDefinitionBodyFileNameEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
52 [NotifyParentProperty (true)]
54 public string BodyFileName {
55 get { return _bag.GetString ("BodyFileName", String.Empty); }
56 set { _bag ["BodyFileName"] = value; }
60 [NotifyParentProperty (true)]
62 get { return _bag.GetString ("CC", String.Empty); }
63 set { _bag ["CC"] = value; }
66 [PersistenceMode (PersistenceMode.InnerProperty)]
68 [NotifyParentProperty (true)]
69 public EmbeddedMailObjectsCollection EmbeddedObjects {
70 get { throw new NotImplementedException (); }
73 [NotifyParentProperty (true)]
76 get { return _bag.GetString ("From", String.Empty); }
77 set { _bag ["From"] = value; }
80 [DefaultValue (false)]
81 [NotifyParentProperty (true)]
82 public bool IsBodyHtml {
83 get { return _bag.GetBool ("IsBodyHtml", false); }
84 set { _bag ["IsBodyHtml"] = value; }
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; }
95 [NotifyParentProperty (true)]
96 public string Subject {
97 get { return _bag.GetString ("Subject", String.Empty); }
98 set { _bag ["Subject"] = value; }
101 public MailMessage CreateMailMessage (string recipients, IDictionary replacements, Control owner)
104 throw new ArgumentNullException ("owner");
106 string bodyText = null;
108 if (BodyFileName.Length > 0) {
109 string filePath = null;
110 if (Path.IsPathRooted (BodyFileName))
111 filePath = BodyFileName;
113 filePath = HttpContext.Current.Request.MapPath (VirtualPathUtility.Combine (owner.TemplateSourceDirectory, BodyFileName));
115 using (StreamReader sr = new StreamReader (filePath)) {
116 bodyText = sr.ReadToEnd ();
122 return CreateMailMessage (recipients, replacements, bodyText, owner);
125 public MailMessage CreateMailMessage (string recipients, IDictionary replacements, string body, Control owner)
128 throw new ArgumentNullException ("owner");
130 MailMessage msg = new MailMessage ();
135 msg.IsBodyHtml = IsBodyHtml;
136 msg.Priority = Priority;
137 msg.Subject = Subject;
141 msg.From = new MailAddress (From);
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");
148 msg.From = new MailAddress (smtpSection.From);
152 string [] recipientsArr = recipients.Split (',');
153 for (int i = 0; i < recipientsArr.Length; i++)
154 msg.To.Add (recipientsArr [i]);
156 foreach (DictionaryEntry d in replacements)
157 msg.Body = msg.Body.Replace ((string) d.Key, (string) d.Value);
162 #region IStateManager Members
164 void IStateManager.LoadViewState (object state)
166 _bag.LoadViewState (state);
169 object IStateManager.SaveViewState () {
170 return _bag.SaveViewState ();
173 void IStateManager.TrackViewState () {
174 _bag.TrackViewState ();
177 bool IStateManager.IsTrackingViewState {
178 get { return _bag.IsTrackingViewState; }