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.
31 using System.Collections.Specialized;
32 using System.Globalization;
33 using System.Net.Mime;
36 namespace System.Net.Mail {
37 public class MailMessage : IDisposable
41 AlternateViewCollection alternateViews;
42 AttachmentCollection attachments;
43 MailAddressCollection bcc;
44 MailAddressCollection replyTo;
46 MailPriority priority;
48 DeliveryNotificationOptions deliveryNotificationOptions;
49 MailAddressCollection cc;
51 NameValueCollection headers;
52 MailAddressCollection to;
54 Encoding subjectEncoding, bodyEncoding, headersEncoding = Encoding.UTF8;
61 public MailMessage () {
62 this.to = new MailAddressCollection ();
64 alternateViews = new AlternateViewCollection ();
65 attachments = new AttachmentCollection ();
66 bcc = new MailAddressCollection ();
67 cc = new MailAddressCollection ();
68 replyTo = new MailAddressCollection ();
69 headers = new NameValueCollection ();
71 headers.Add ("MIME-Version", "1.0");
74 // FIXME: should it throw a FormatException if the addresses are wrong?
75 // (How is it possible to instantiate such a malformed MailAddress?)
76 public MailMessage (MailAddress from, MailAddress to) : this ()
78 if (from == null || to == null)
79 throw new ArgumentNullException ();
86 public MailMessage (string from, string to) : this ()
88 if (from == null || from == String.Empty)
89 throw new ArgumentNullException ("from");
90 if (to == null || to == String.Empty)
91 throw new ArgumentNullException ("to");
93 this.from = new MailAddress (from);
94 foreach (string recipient in to.Split (new char [] {','}))
95 this.to.Add (new MailAddress (recipient.Trim ()));
98 public MailMessage (string from, string to, string subject, string body) : this ()
100 if (from == null || from == String.Empty)
101 throw new ArgumentNullException ("from");
102 if (to == null || to == String.Empty)
103 throw new ArgumentNullException ("to");
105 this.from = new MailAddress (from);
106 foreach (string recipient in to.Split (new char [] {','}))
107 this.to.Add (new MailAddress (recipient.Trim ()));
113 #endregion // Constructors
117 public AlternateViewCollection AlternateViews {
118 get { return alternateViews; }
121 public AttachmentCollection Attachments {
122 get { return attachments; }
125 public MailAddressCollection Bcc {
132 // autodetect suitable body encoding (ASCII or UTF-8), if it is not initialized yet.
133 if (value != null && bodyEncoding == null)
134 bodyEncoding = GuessEncoding (value) ?? Encoding.ASCII;
139 internal ContentType BodyContentType {
141 ContentType ct = new ContentType (isHtml ? "text/html" : "text/plain");
142 ct.CharSet = (BodyEncoding ?? Encoding.ASCII).HeaderName;
147 internal TransferEncoding ContentTransferEncoding {
148 get { return GuessTransferEncoding (BodyEncoding); }
151 public Encoding BodyEncoding {
152 get { return bodyEncoding; }
153 set { bodyEncoding = value; }
156 public MailAddressCollection CC {
160 public DeliveryNotificationOptions DeliveryNotificationOptions {
161 get { return deliveryNotificationOptions; }
162 set { deliveryNotificationOptions = value; }
165 public MailAddress From {
167 set { from = value; }
170 public NameValueCollection Headers {
171 get { return headers; }
174 public bool IsBodyHtml {
175 get { return isHtml; }
176 set { isHtml = value; }
179 public MailPriority Priority {
180 get { return priority; }
181 set { priority = value; }
185 Encoding HeadersEncoding {
186 get { return headersEncoding; }
187 set { headersEncoding = value; }
191 MailAddressCollection ReplyToList {
192 get { return replyTo; }
195 [Obsolete ("Use ReplyToList instead")]
196 public MailAddress ReplyTo {
198 if (replyTo.Count == 0)
208 public MailAddress Sender {
209 get { return sender; }
210 set { sender = value; }
213 public string Subject {
214 get { return subject; }
216 if (value != null && subjectEncoding == null)
217 subjectEncoding = GuessEncoding (value);
222 public Encoding SubjectEncoding {
223 get { return subjectEncoding; }
224 set { subjectEncoding = value; }
227 public MailAddressCollection To {
231 #endregion // Properties
235 public void Dispose ()
238 GC.SuppressFinalize (this);
241 protected virtual void Dispose (bool disposing)
245 private Encoding GuessEncoding (string s)
247 for (int i = 0; i < s.Length; i++)
248 if (s [i] >= '\u0080')
253 internal static TransferEncoding GuessTransferEncoding (Encoding enc)
255 if (Encoding.ASCII.Equals (enc))
256 return TransferEncoding.SevenBit;
257 else if (Encoding.UTF8.CodePage == enc.CodePage ||
259 Encoding.Unicode.CodePage == enc.CodePage || Encoding.UTF32.CodePage == enc.CodePage
261 Encoding.Unicode.CodePage == enc.CodePage
264 return TransferEncoding.Base64;
266 return TransferEncoding.QuotedPrintable;
269 static char [] hex = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
270 internal static string To2047(byte [] bytes)
272 StringBuilder sb = new StringBuilder ();
273 foreach (byte i in bytes) {
274 if (i < 0x21 || i > 0x7E || i == '?' || i == '=' || i == '_') {
276 sb.Append (hex [(i >> 4) & 0x0f]);
277 sb.Append (hex [i & 0x0f]);
279 sb.Append ((char) i);
281 return sb.ToString ();
284 internal static string EncodeSubjectRFC2047 (string s, Encoding enc)
286 if (s == null || Encoding.ASCII.Equals (enc))
288 for (int i = 0; i < s.Length; i++)
289 if (s [i] >= '\u0080') {
290 string quoted = To2047(enc.GetBytes (s));
291 return String.Concat ("=?", enc.HeaderName, "?Q?", quoted, "?=");
296 static Encoding utf8unmarked;
297 static Encoding UTF8Unmarked {
299 if (utf8unmarked == null)
300 utf8unmarked = new UTF8Encoding (false);
304 #endregion // Methods