2 // System.Net.Mail.MailMessage.cs
5 // Tim Coleman (tim@timcoleman.com)
7 // Copyright (C) Tim Coleman, 2004
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
33 using System
.Collections
.Specialized
;
34 using System
.Globalization
;
35 using System
.Net
.Mime
;
38 namespace System
.Net
.Mail
{
39 public class MailMessage
: IDisposable
43 AlternateViewCollection alternateViews
;
44 AttachmentCollection attachments
;
45 MailAddressCollection bcc
;
46 MailAddressCollection replyTo
;
48 MailPriority priority
;
50 DeliveryNotificationOptions deliveryNotificationOptions
;
51 MailAddressCollection cc
;
53 NameValueCollection headers
;
54 MailAddressCollection to
;
56 Encoding subjectEncoding
, bodyEncoding
, headersEncoding
= Encoding
.UTF8
;
63 public MailMessage () {
64 this.to
= new MailAddressCollection ();
66 alternateViews
= new AlternateViewCollection ();
67 attachments
= new AttachmentCollection ();
68 bcc
= new MailAddressCollection ();
69 cc
= new MailAddressCollection ();
70 replyTo
= new MailAddressCollection ();
71 headers
= new NameValueCollection ();
73 headers
.Add ("MIME-Version", "1.0");
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 ()
80 if (from == null || to
== null)
81 throw new ArgumentNullException ();
88 public MailMessage (string from, string to
) : this ()
90 if (from == null || from == String
.Empty
)
91 throw new ArgumentNullException ("from");
92 if (to
== null || to
== String
.Empty
)
93 throw new ArgumentNullException ("to");
95 this.from = new MailAddress (from);
96 foreach (string recipient
in to
.Split (new char [] {','}
))
97 this.to
.Add (new MailAddress (recipient
.Trim ()));
100 public MailMessage (string from, string to
, string subject
, string body
) : this ()
102 if (from == null || from == String
.Empty
)
103 throw new ArgumentNullException ("from");
104 if (to
== null || to
== String
.Empty
)
105 throw new ArgumentNullException ("to");
107 this.from = new MailAddress (from);
108 foreach (string recipient
in to
.Split (new char [] {','}
))
109 this.to
.Add (new MailAddress (recipient
.Trim ()));
115 #endregion // Constructors
119 public AlternateViewCollection AlternateViews
{
120 get { return alternateViews; }
123 public AttachmentCollection Attachments
{
124 get { return attachments; }
127 public MailAddressCollection Bcc
{
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
;
141 internal ContentType BodyContentType
{
143 ContentType ct
= new ContentType (isHtml
? "text/html" : "text/plain");
144 ct
.CharSet
= (BodyEncoding
?? Encoding
.ASCII
).HeaderName
;
149 internal TransferEncoding ContentTransferEncoding
{
150 get { return ContentType.GuessTransferEncoding (BodyEncoding); }
153 public Encoding BodyEncoding
{
154 get { return bodyEncoding; }
155 set { bodyEncoding = value; }
158 public MailAddressCollection CC
{
162 public DeliveryNotificationOptions DeliveryNotificationOptions
{
163 get { return deliveryNotificationOptions; }
164 set { deliveryNotificationOptions = value; }
167 public MailAddress From
{
169 set { from = value; }
172 public NameValueCollection Headers
{
173 get { return headers; }
176 public bool IsBodyHtml
{
177 get { return isHtml; }
178 set { isHtml = value; }
181 public MailPriority Priority
{
182 get { return priority; }
183 set { priority = value; }
191 Encoding HeadersEncoding
{
192 get { return headersEncoding; }
193 set { headersEncoding = value; }
201 MailAddressCollection ReplyToList
{
202 get { return replyTo; }
206 [Obsolete ("Use ReplyToList instead")]
208 public MailAddress ReplyTo
{
210 if (replyTo
.Count
== 0)
220 public MailAddress Sender
{
221 get { return sender; }
222 set { sender = value; }
225 public string Subject
{
226 get { return subject; }
228 if (value != null && subjectEncoding
== null)
229 subjectEncoding
= GuessEncoding (value);
234 public Encoding SubjectEncoding
{
235 get { return subjectEncoding; }
236 set { subjectEncoding = value; }
239 public MailAddressCollection To
{
243 #endregion // Properties
247 public void Dispose ()
250 GC
.SuppressFinalize (this);
253 protected virtual void Dispose (bool disposing
)
257 private Encoding
GuessEncoding (string s
)
259 return ContentType
.GuessEncoding (s
);
262 #endregion // Methods