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 public sealed class MailDefinition : IStateManager
47 StateBag _bag = new StateBag ();
50 [NotifyParentProperty (true)]
51 public string BodyFileName {
52 get { return _bag.GetString ("BodyFileName", String.Empty); }
53 set { _bag ["BodyFileName"] = value; }
57 [NotifyParentProperty (true)]
59 get { return _bag.GetString ("CC", String.Empty); }
60 set { _bag ["CC"] = value; }
63 [PersistenceMode (PersistenceMode.InnerProperty)]
65 [NotifyParentProperty (true)]
66 public EmbeddedMailObjectsCollection EmbeddedObjects {
67 get { throw new NotImplementedException (); }
70 [NotifyParentProperty (true)]
73 get { return _bag.GetString ("From", String.Empty); }
74 set { _bag ["From"] = value; }
77 [DefaultValue (false)]
78 [NotifyParentProperty (true)]
79 public bool IsBodyHtml {
80 get { return _bag.GetBool ("IsBodyHtml", false); }
81 set { _bag ["IsBodyHtml"] = value; }
84 [NotifyParentProperty (true)]
85 public MailPriority Priority {
86 get { return _bag ["Priority"] == null ? MailPriority.Normal : (MailPriority) _bag ["Priority"]; }
87 set { _bag ["Priority"] = value; }
91 [NotifyParentProperty (true)]
92 public string Subject {
93 get { return _bag.GetString ("Subject", String.Empty); }
94 set { _bag ["Subject"] = value; }
97 public MailMessage CreateMailMessage (string recipients, IDictionary replacements, Control owner)
100 throw new ArgumentNullException ("owner");
102 string bodyText = null;
104 if (BodyFileName.Length > 0) {
105 string filePath = null;
106 if (Path.IsPathRooted (BodyFileName))
107 filePath = BodyFileName;
109 filePath = HttpContext.Current.Request.MapPath (VirtualPathUtility.Combine (owner.TemplateSourceDirectory, BodyFileName));
111 using (StreamReader sr = new StreamReader (filePath)) {
112 bodyText = sr.ReadToEnd ();
118 return CreateMailMessage (recipients, replacements, bodyText, owner);
121 public MailMessage CreateMailMessage (string recipients, IDictionary replacements, string body, Control owner)
124 throw new ArgumentNullException ("owner");
126 MailMessage msg = new MailMessage ();
131 msg.IsBodyHtml = IsBodyHtml;
132 msg.Priority = Priority;
133 msg.Subject = Subject;
137 msg.From = new MailAddress (From);
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");
144 msg.From = new MailAddress (smtpSection.From);
148 string [] recipientsArr = recipients.Split (',');
149 for (int i = 0; i < recipientsArr.Length; i++)
150 msg.To.Add (recipientsArr [i]);
152 foreach (DictionaryEntry d in replacements)
153 msg.Body = msg.Body.Replace ((string) d.Key, (string) d.Value);
158 #region IStateManager Members
160 void IStateManager.LoadViewState (object state)
162 _bag.LoadViewState (state);
165 object IStateManager.SaveViewState () {
166 return _bag.SaveViewState ();
169 void IStateManager.TrackViewState () {
170 _bag.TrackViewState ();
173 bool IStateManager.IsTrackingViewState {
174 get { return _bag.IsTrackingViewState; }