BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[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 #if NET_2_0
32
33 using System.Collections.Specialized;
34 using System.Globalization;
35 using System.Net.Mime;
36 using System.Text;
37
38 namespace System.Net.Mail {
39         public class MailMessage : IDisposable
40         {
41                 #region Fields
42
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;
58
59                 #endregion // Fields
60
61                 #region Constructors
62
63                 public MailMessage () {
64                         this.to = new MailAddressCollection ();
65
66                         alternateViews = new AlternateViewCollection ();
67                         attachments = new AttachmentCollection ();
68                         bcc = new MailAddressCollection ();
69                         cc = new MailAddressCollection ();
70                         replyTo = new MailAddressCollection ();
71                         headers = new NameValueCollection ();
72
73                         headers.Add ("MIME-Version", "1.0");
74                 }
75
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 ()
79                 {
80                         if (from == null || to == null)
81                                 throw new ArgumentNullException ();
82
83                         From = from;
84
85                         this.to.Add (to);
86                 }
87
88                 public MailMessage (string from, string to) : this ()
89                 {
90                         if (from == null || from == String.Empty)
91                                 throw new ArgumentNullException ("from");
92                         if (to == null || to == String.Empty)
93                                 throw new ArgumentNullException ("to");
94                         
95                         this.from = new MailAddress (from);
96                         foreach (string recipient in to.Split (new char [] {','}))
97                                 this.to.Add (new MailAddress (recipient.Trim ()));
98                 }
99
100                 public MailMessage (string from, string to, string subject, string body) : this ()
101                 {
102                         if (from == null || from == String.Empty)
103                                 throw new ArgumentNullException ("from");
104                         if (to == null || to == String.Empty)
105                                 throw new ArgumentNullException ("to");
106                         
107                         this.from = new MailAddress (from);
108                         foreach (string recipient in to.Split (new char [] {','}))
109                                 this.to.Add (new MailAddress (recipient.Trim ()));
110
111                         Body = body;
112                         Subject = subject;
113                 }
114
115                 #endregion // Constructors
116
117                 #region Properties
118
119                 public AlternateViewCollection AlternateViews {
120                         get { return alternateViews; }
121                 }
122
123                 public AttachmentCollection Attachments {
124                         get { return attachments; }
125                 }
126
127                 public MailAddressCollection Bcc {
128                         get { return bcc; }
129                 }
130
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;
138                         }
139                 }
140
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;
146                         }
147                 }
148
149                 internal TransferEncoding ContentTransferEncoding {
150                         get { return ContentType.GuessTransferEncoding (BodyEncoding); }
151                 }
152
153                 public Encoding BodyEncoding {
154                         get { return bodyEncoding; }
155                         set { bodyEncoding = value; }
156                 }
157
158                 public MailAddressCollection CC {
159                         get { return cc; }
160                 }
161
162                 public DeliveryNotificationOptions DeliveryNotificationOptions {
163                         get { return deliveryNotificationOptions; }
164                         set { deliveryNotificationOptions = value; }
165                 }
166
167                 public MailAddress From {
168                         get { return from; }
169                         set { from = value; }
170                 }
171
172                 public NameValueCollection Headers {
173                         get { return headers; }
174                 }
175
176                 public bool IsBodyHtml {
177                         get { return isHtml; }
178                         set { isHtml = value; }
179                 }
180
181                 public MailPriority Priority {
182                         get { return priority; }
183                         set { priority = value; }
184                 }
185
186 #if NET_4_0
187                 public
188 #else
189                 internal
190 #endif
191                 Encoding HeadersEncoding {
192                         get { return headersEncoding; }
193                         set { headersEncoding = value; } 
194                 }
195
196 #if NET_4_0
197                 public
198 #else
199                 internal
200 #endif
201                 MailAddressCollection ReplyToList {
202                         get { return replyTo; }
203                 }
204
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];
213                         }
214                         set {
215                                 replyTo.Clear ();
216                                 replyTo.Add (value);
217                         }
218                 }
219
220                 public MailAddress Sender {
221                         get { return sender; }
222                         set { sender = value; }
223                 }
224
225                 public string Subject {
226                         get { return subject; }
227                         set {
228                                 if (value != null && subjectEncoding == null)
229                                         subjectEncoding = GuessEncoding (value);
230                                 subject = value;
231                         }
232                 }
233
234                 public Encoding SubjectEncoding {
235                         get { return subjectEncoding; }
236                         set { subjectEncoding = value; }
237                 }
238
239                 public MailAddressCollection To {
240                         get { return to; }
241                 }
242
243                 #endregion // Properties
244
245                 #region Methods
246
247                 public void Dispose ()
248                 {
249                         Dispose (true);
250                         GC.SuppressFinalize (this);
251                 }
252
253                 protected virtual void Dispose (bool disposing)
254                 {
255                 }
256
257                 private Encoding GuessEncoding (string s)
258                 {
259                         return ContentType.GuessEncoding (s);
260                 }
261
262                 #endregion // Methods
263         }
264 }
265
266 #endif // NET_2_0