Merge pull request #3625 from slide/master
[mono.git] / mcs / class / System / System.Net.Mail / MailMessage.cs
1 //
2 // System.Net.Mail.MailMessage.cs
3 //
4 // Author:
5 //      Tim Coleman (tim@timcoleman.com)
6 //
7 // Copyright (C) Tim Coleman, 2004
8 //
9
10 //
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.
29 //
30
31 using System.Collections.Specialized;
32 using System.Globalization;
33 using System.Net.Mime;
34 using System.Text;
35
36 namespace System.Net.Mail {
37         public class MailMessage : IDisposable
38         {
39                 #region Fields
40
41                 AlternateViewCollection alternateViews;
42                 AttachmentCollection attachments;
43                 MailAddressCollection bcc;
44                 MailAddressCollection replyTo;          
45                 string body;
46                 MailPriority priority;
47                 MailAddress sender;
48                 DeliveryNotificationOptions deliveryNotificationOptions;
49                 MailAddressCollection cc;
50                 MailAddress from;
51                 NameValueCollection headers;
52                 MailAddressCollection to;
53                 string subject;
54                 Encoding subjectEncoding, bodyEncoding, headersEncoding = Encoding.UTF8;
55                 bool isHtml;
56
57                 #endregion // Fields
58
59                 #region Constructors
60
61                 public MailMessage () {
62                         this.to = new MailAddressCollection ();
63
64                         alternateViews = new AlternateViewCollection ();
65                         attachments = new AttachmentCollection ();
66                         bcc = new MailAddressCollection ();
67                         cc = new MailAddressCollection ();
68                         replyTo = new MailAddressCollection ();
69                         headers = new NameValueCollection ();
70
71                         headers.Add ("MIME-Version", "1.0");
72                 }
73
74                 // FIXME: should it throw a FormatException if the addresses are wrong? 
75                 // (How is it possible to instantiate such a malformed MailAddress?)
76                 public MailMessage (MailAddress from, MailAddress to) : this ()
77                 {
78                         if (from == null || to == null)
79                                 throw new ArgumentNullException ();
80
81                         From = from;
82
83                         this.to.Add (to);
84                 }
85
86                 public MailMessage (string from, string to) : this ()
87                 {
88                         if (from == null || from == String.Empty)
89                                 throw new ArgumentNullException ("from");
90                         if (to == null || to == String.Empty)
91                                 throw new ArgumentNullException ("to");
92                         
93                         this.from = new MailAddress (from);
94                         foreach (string recipient in to.Split (new char [] {','}))
95                                 this.to.Add (new MailAddress (recipient.Trim ()));
96                 }
97
98                 public MailMessage (string from, string to, string subject, string body) : this ()
99                 {
100                         if (from == null || from == String.Empty)
101                                 throw new ArgumentNullException ("from");
102                         if (to == null || to == String.Empty)
103                                 throw new ArgumentNullException ("to");
104                         
105                         this.from = new MailAddress (from);
106                         foreach (string recipient in to.Split (new char [] {','}))
107                                 this.to.Add (new MailAddress (recipient.Trim ()));
108
109                         Body = body;
110                         Subject = subject;
111                 }
112
113                 #endregion // Constructors
114
115                 #region Properties
116
117                 public AlternateViewCollection AlternateViews {
118                         get { return alternateViews; }
119                 }
120
121                 public AttachmentCollection Attachments {
122                         get { return attachments; }
123                 }
124
125                 public MailAddressCollection Bcc {
126                         get { return bcc; }
127                 }
128
129                 public string Body {
130                         get { return body; }
131                         set {
132                                 // autodetect suitable body encoding (ASCII or UTF-8), if it is not initialized yet.
133                                 if (value != null && bodyEncoding == null)
134                                         bodyEncoding = GuessEncoding (value) ?? Encoding.ASCII;
135                                 body = value;
136                         }
137                 }
138
139                 internal ContentType BodyContentType {
140                         get {
141                                 ContentType ct = new ContentType (isHtml ? "text/html" : "text/plain");
142                                 ct.CharSet = (BodyEncoding ?? Encoding.ASCII).HeaderName;
143                                 return ct;
144                         }
145                 }
146
147                 internal TransferEncoding ContentTransferEncoding {
148                         get { return GuessTransferEncoding (BodyEncoding); }
149                 }
150
151                 public Encoding BodyEncoding {
152                         get { return bodyEncoding; }
153                         set { bodyEncoding = value; }
154                 }
155
156                 public TransferEncoding BodyTransferEncoding {
157                         get { return GuessTransferEncoding (BodyEncoding); }
158                         set { throw new NotImplementedException (); }
159                 }
160
161                 public MailAddressCollection CC {
162                         get { return cc; }
163                 }
164
165                 public DeliveryNotificationOptions DeliveryNotificationOptions {
166                         get { return deliveryNotificationOptions; }
167                         set { deliveryNotificationOptions = value; }
168                 }
169
170                 public MailAddress From {
171                         get { return from; }
172                         set { from = value; }
173                 }
174
175                 public NameValueCollection Headers {
176                         get { return headers; }
177                 }
178
179                 public bool IsBodyHtml {
180                         get { return isHtml; }
181                         set { isHtml = value; }
182                 }
183
184                 public MailPriority Priority {
185                         get { return priority; }
186                         set { priority = value; }
187                 }
188
189                 public
190                 Encoding HeadersEncoding {
191                         get { return headersEncoding; }
192                         set { headersEncoding = value; } 
193                 }
194
195                 public
196                 MailAddressCollection ReplyToList {
197                         get { return replyTo; }
198                 }
199
200                 [Obsolete ("Use ReplyToList instead")]
201                 public MailAddress ReplyTo {
202                         get {
203                                 if (replyTo.Count == 0)
204                                         return null;
205                                 return replyTo [0];
206                         }
207                         set {
208                                 replyTo.Clear ();
209                                 replyTo.Add (value);
210                         }
211                 }
212
213                 public MailAddress Sender {
214                         get { return sender; }
215                         set { sender = value; }
216                 }
217
218                 public string Subject {
219                         get { return subject; }
220                         set {
221                                 if (value != null && subjectEncoding == null)
222                                         subjectEncoding = GuessEncoding (value);
223                                 subject = value;
224                         }
225                 }
226
227                 public Encoding SubjectEncoding {
228                         get { return subjectEncoding; }
229                         set { subjectEncoding = value; }
230                 }
231
232                 public MailAddressCollection To {
233                         get { return to; }
234                 }
235
236                 #endregion // Properties
237
238                 #region Methods
239
240                 public void Dispose ()
241                 {
242                         Dispose (true);
243                         GC.SuppressFinalize (this);
244                 }
245
246                 protected virtual void Dispose (bool disposing)
247                 {
248                 }
249
250                 private Encoding GuessEncoding (string s)
251                 {
252                         for (int i = 0; i < s.Length; i++)
253                                 if (s [i] >= '\u0080')
254                                         return UTF8Unmarked;
255                         return null;
256                 }
257
258                 internal static TransferEncoding GuessTransferEncoding (Encoding enc)
259                 {
260                         if (Encoding.ASCII.Equals (enc))
261                                 return TransferEncoding.SevenBit;
262                         else if (Encoding.UTF8.CodePage == enc.CodePage ||
263 #if !MOBILE
264                             Encoding.Unicode.CodePage == enc.CodePage || Encoding.UTF32.CodePage == enc.CodePage
265 #else
266                             Encoding.Unicode.CodePage == enc.CodePage
267 #endif
268                                          )
269                                 return TransferEncoding.Base64;
270                         else
271                                 return TransferEncoding.QuotedPrintable;
272                 }
273
274                 static char [] hex = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
275                 internal static string To2047(byte [] bytes)
276                 {
277                         StringBuilder sb = new StringBuilder ();
278                         foreach (byte i in bytes) {
279                                 if (i < 0x21 || i > 0x7E || i == '?' || i == '=' || i == '_') {
280                                         sb.Append ('=');
281                                         sb.Append (hex [(i >> 4) & 0x0f]);
282                                         sb.Append (hex [i & 0x0f]);
283                                 } else
284                                         sb.Append ((char) i);
285                         }
286                         return sb.ToString ();
287                 }
288
289                 internal static string EncodeSubjectRFC2047 (string s, Encoding enc)
290                 {
291                         if (s == null || Encoding.ASCII.Equals (enc))
292                                 return s;
293                         for (int i = 0; i < s.Length; i++)
294                                 if (s [i] >= '\u0080') {
295                                         string quoted = To2047(enc.GetBytes (s));
296                                         return String.Concat ("=?", enc.HeaderName, "?Q?", quoted, "?=");
297                                 }
298                         return s;
299                 }
300
301                 static Encoding utf8unmarked;
302                 static Encoding UTF8Unmarked {
303                         get {
304                                 if (utf8unmarked == null)
305                                         utf8unmarked = new UTF8Encoding (false);
306                                 return utf8unmarked;
307                         }
308                 }
309                 #endregion // Methods
310         }
311 }
312