2007-01-20 Miguel de Icaza <miguel@novell.com>
[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         [MonoTODO]
40         public class MailMessage : IDisposable
41         {
42                 #region Fields
43
44                 AlternateViewCollection alternateViews;
45                 AttachmentCollection attachments;
46                 MailAddressCollection bcc;
47                 string body;
48                 MailPriority priority;
49                 MailAddress replyTo, sender;
50                 DeliveryNotificationOptions deliveryNotificationOptions;
51                 MailAddressCollection cc;
52                 MailAddress from;
53                 NameValueCollection headers;
54                 MailAddressCollection to;
55                 string subject;
56                 Encoding subjectEncoding;
57                 ContentType bodyContentType;
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                         headers = new NameValueCollection ();
71
72                         headers.Add ("MIME-Version", "1.0");
73                 }
74
75                 // FIXME: should throw a FormatException if the addresses are wrong.
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 { body = value; }
132                 }
133
134                 internal ContentType BodyContentType {
135                         get {
136                                 if (bodyContentType == null)
137                                         bodyContentType = new ContentType ("text/plain; charset=us-ascii");
138                                 return bodyContentType;
139                         }
140                 }
141
142                 public Encoding BodyEncoding {
143                         get { return Encoding.GetEncoding (BodyContentType.CharSet); }
144                         set { BodyContentType.CharSet = value.WebName; }
145                 }
146
147                 public MailAddressCollection CC {
148                         get { return cc; }
149                 }
150
151                 public DeliveryNotificationOptions DeliveryNotificationOptions {
152                         get { return deliveryNotificationOptions; }
153                         set { deliveryNotificationOptions = value; }
154                 }
155
156                 public MailAddress From {
157                         get { return from; }
158                         set { from = value; }
159                 }
160
161                 public NameValueCollection Headers {
162                         get { return headers; }
163                 }
164
165                 public bool IsBodyHtml {
166                         get { return String.Compare (BodyContentType.MediaType, "text/html", true, CultureInfo.InvariantCulture) == 0; }
167                         set {
168                                 if (value)
169                                         BodyContentType.MediaType = "text/html";
170                                 else
171                                         BodyContentType.MediaType = "text/plain";
172                         }
173                 }
174
175                 public MailPriority Priority {
176                         get { return priority; }
177                         set { priority = value; }
178                 }
179
180                 public MailAddress ReplyTo {
181                         get { return replyTo; }
182                         set { replyTo = value; }
183                 }
184
185                 public MailAddress Sender {
186                         get { return sender; }
187                         set { sender = value; }
188                 }
189
190                 public string Subject {
191                         get { return subject; }
192                         set { subject = value; }
193                 }
194
195                 public Encoding SubjectEncoding {
196                         get { return subjectEncoding; }
197                         set { subjectEncoding = value; }
198                 }
199
200                 public MailAddressCollection To {
201                         get { return to; }
202                 }
203
204                 #endregion // Properties
205
206                 #region Methods
207
208                 public void Dispose ()
209                 {
210                         Dispose (true);
211                         GC.SuppressFinalize (this);
212                 }
213
214                 protected virtual void Dispose (bool disposing)
215                 {
216                 }
217
218                 #endregion // Methods
219         }
220 }
221
222 #endif // NET_2_0