Fixed trigger happy commit
[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 || to == null)
89                                 throw new ArgumentNullException ();
90
91                         this.from = new MailAddress (from);
92                         this.to.Add (new MailAddress (to));
93                 }
94
95                 public MailMessage (string from, string to, string subject, string body) : this ()
96                 {
97                         if (from == null || to == null)
98                                 throw new ArgumentNullException ();
99                         this.from = new MailAddress (from);
100                         this.to.Add (new MailAddress (to));
101                         Body = body;
102                         Subject = subject;
103                 }
104
105                 #endregion // Constructors
106
107                 #region Properties
108
109                 public AlternateViewCollection AlternateViews {
110                         get { return alternateViews; }
111                 }
112
113                 public AttachmentCollection Attachments {
114                         get { return attachments; }
115                 }
116
117                 public MailAddressCollection Bcc {
118                         get { return bcc; }
119                 }
120
121                 public string Body {
122                         get { return body; }
123                         set { body = value; }
124                 }
125
126                 internal ContentType BodyContentType {
127                         get {
128                                 if (bodyContentType == null)
129                                         bodyContentType = new ContentType ("text/plain; charset=us-ascii");
130                                 return bodyContentType;
131                         }
132                 }
133
134                 public Encoding BodyEncoding {
135                         get { return Encoding.GetEncoding (BodyContentType.CharSet); }
136                         set { BodyContentType.CharSet = value.WebName; }
137                 }
138
139                 public MailAddressCollection CC {
140                         get { return cc; }
141                 }
142
143                 public DeliveryNotificationOptions DeliveryNotificationOptions {
144                         get { return deliveryNotificationOptions; }
145                         set { deliveryNotificationOptions = value; }
146                 }
147
148                 public MailAddress From {
149                         get { return from; }
150                         set { from = value; }
151                 }
152
153                 public NameValueCollection Headers {
154                         get { return headers; }
155                 }
156
157                 public bool IsBodyHtml {
158                         get { return String.Compare (BodyContentType.MediaType, "text/html", true, CultureInfo.InvariantCulture) == 0; }
159                         set {
160                                 if (value)
161                                         BodyContentType.MediaType = "text/html";
162                                 else
163                                         BodyContentType.MediaType = "text/plain";
164                         }
165                 }
166
167                 public MailPriority Priority {
168                         get { return priority; }
169                         set { priority = value; }
170                 }
171
172                 public MailAddress ReplyTo {
173                         get { return replyTo; }
174                         set { replyTo = value; }
175                 }
176
177                 public MailAddress Sender {
178                         get { return sender; }
179                         set { sender = value; }
180                 }
181
182                 public string Subject {
183                         get { return subject; }
184                         set { subject = value; }
185                 }
186
187                 public Encoding SubjectEncoding {
188                         get { return subjectEncoding; }
189                         set { subjectEncoding = value; }
190                 }
191
192                 public MailAddressCollection To {
193                         get { return to; }
194                 }
195
196                 #endregion // Properties
197
198                 #region Methods
199
200                 public void Dispose ()
201                 {
202                         Dispose (true);
203                         GC.SuppressFinalize (this);
204                 }
205
206                 protected virtual void Dispose (bool disposing)
207                 {
208                 }
209
210                 #endregion // Methods
211         }
212 }
213
214 #endif // NET_2_0