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