add ISafeSerializationData
[mcs.git] / class / System / System.Net.Mail / MailMessage.cs
blob98d3244e330dabdf2323270e63ea09fc415b475c
1 //
2 // System.Net.Mail.MailMessage.cs
3 //
4 // Author:
5 // Tim Coleman (tim@timcoleman.com)
6 //
7 // Copyright (C) Tim Coleman, 2004
8 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 #if NET_2_0
33 using System.Collections.Specialized;
34 using System.Globalization;
35 using System.Net.Mime;
36 using System.Text;
38 namespace System.Net.Mail {
39 public class MailMessage : IDisposable
41 #region Fields
43 AlternateViewCollection alternateViews;
44 AttachmentCollection attachments;
45 MailAddressCollection bcc;
46 MailAddressCollection replyTo;
47 string body;
48 MailPriority priority;
49 MailAddress sender;
50 DeliveryNotificationOptions deliveryNotificationOptions;
51 MailAddressCollection cc;
52 MailAddress from;
53 NameValueCollection headers;
54 MailAddressCollection to;
55 string subject;
56 Encoding subjectEncoding, bodyEncoding, headersEncoding = Encoding.UTF8;
57 bool isHtml;
59 #endregion // Fields
61 #region Constructors
63 public MailMessage () {
64 this.to = new MailAddressCollection ();
66 alternateViews = new AlternateViewCollection ();
67 attachments = new AttachmentCollection ();
68 bcc = new MailAddressCollection ();
69 cc = new MailAddressCollection ();
70 replyTo = new MailAddressCollection ();
71 headers = new NameValueCollection ();
73 headers.Add ("MIME-Version", "1.0");
76 // FIXME: should it throw a FormatException if the addresses are wrong?
77 // (How is it possible to instantiate such a malformed MailAddress?)
78 public MailMessage (MailAddress from, MailAddress to) : this ()
80 if (from == null || to == null)
81 throw new ArgumentNullException ();
83 From = from;
85 this.to.Add (to);
88 public MailMessage (string from, string to) : this ()
90 if (from == null || from == String.Empty)
91 throw new ArgumentNullException ("from");
92 if (to == null || to == String.Empty)
93 throw new ArgumentNullException ("to");
95 this.from = new MailAddress (from);
96 foreach (string recipient in to.Split (new char [] {','}))
97 this.to.Add (new MailAddress (recipient.Trim ()));
100 public MailMessage (string from, string to, string subject, string body) : this ()
102 if (from == null || from == String.Empty)
103 throw new ArgumentNullException ("from");
104 if (to == null || to == String.Empty)
105 throw new ArgumentNullException ("to");
107 this.from = new MailAddress (from);
108 foreach (string recipient in to.Split (new char [] {','}))
109 this.to.Add (new MailAddress (recipient.Trim ()));
111 Body = body;
112 Subject = subject;
115 #endregion // Constructors
117 #region Properties
119 public AlternateViewCollection AlternateViews {
120 get { return alternateViews; }
123 public AttachmentCollection Attachments {
124 get { return attachments; }
127 public MailAddressCollection Bcc {
128 get { return bcc; }
131 public string Body {
132 get { return body; }
133 set {
134 // autodetect suitable body encoding (ASCII or UTF-8), if it is not initialized yet.
135 if (value != null && bodyEncoding == null)
136 bodyEncoding = GuessEncoding (value) ?? Encoding.ASCII;
137 body = value;
141 internal ContentType BodyContentType {
142 get {
143 ContentType ct = new ContentType (isHtml ? "text/html" : "text/plain");
144 ct.CharSet = (BodyEncoding ?? Encoding.ASCII).HeaderName;
145 return ct;
149 internal TransferEncoding ContentTransferEncoding {
150 get { return ContentType.GuessTransferEncoding (BodyEncoding); }
153 public Encoding BodyEncoding {
154 get { return bodyEncoding; }
155 set { bodyEncoding = value; }
158 public MailAddressCollection CC {
159 get { return cc; }
162 public DeliveryNotificationOptions DeliveryNotificationOptions {
163 get { return deliveryNotificationOptions; }
164 set { deliveryNotificationOptions = value; }
167 public MailAddress From {
168 get { return from; }
169 set { from = value; }
172 public NameValueCollection Headers {
173 get { return headers; }
176 public bool IsBodyHtml {
177 get { return isHtml; }
178 set { isHtml = value; }
181 public MailPriority Priority {
182 get { return priority; }
183 set { priority = value; }
186 #if NET_4_0
187 public
188 #else
189 internal
190 #endif
191 Encoding HeadersEncoding {
192 get { return headersEncoding; }
193 set { headersEncoding = value; }
196 #if NET_4_0
197 public
198 #else
199 internal
200 #endif
201 MailAddressCollection ReplyToList {
202 get { return replyTo; }
205 #if NET_4_0
206 [Obsolete ("Use ReplyToList instead")]
207 #endif
208 public MailAddress ReplyTo {
209 get {
210 if (replyTo.Count == 0)
211 return null;
212 return replyTo [0];
214 set {
215 replyTo.Clear ();
216 replyTo.Add (value);
220 public MailAddress Sender {
221 get { return sender; }
222 set { sender = value; }
225 public string Subject {
226 get { return subject; }
227 set {
228 if (value != null && subjectEncoding == null)
229 subjectEncoding = GuessEncoding (value);
230 subject = value;
234 public Encoding SubjectEncoding {
235 get { return subjectEncoding; }
236 set { subjectEncoding = value; }
239 public MailAddressCollection To {
240 get { return to; }
243 #endregion // Properties
245 #region Methods
247 public void Dispose ()
249 Dispose (true);
250 GC.SuppressFinalize (this);
253 protected virtual void Dispose (bool disposing)
257 private Encoding GuessEncoding (string s)
259 return ContentType.GuessEncoding (s);
262 #endregion // Methods
266 #endif // NET_2_0