MailMessage.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. //
  2. // System.Net.Mail.MailMessage.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. //
  7. // Copyright (C) Tim Coleman, 2004
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Collections.Specialized;
  30. using System.Globalization;
  31. using System.Net.Mime;
  32. using System.Text;
  33. namespace System.Net.Mail {
  34. public class MailMessage : IDisposable
  35. {
  36. #region Fields
  37. AlternateViewCollection alternateViews;
  38. AttachmentCollection attachments;
  39. MailAddressCollection bcc;
  40. MailAddressCollection replyTo;
  41. string body;
  42. MailPriority priority;
  43. MailAddress sender;
  44. DeliveryNotificationOptions deliveryNotificationOptions;
  45. MailAddressCollection cc;
  46. MailAddress from;
  47. NameValueCollection headers;
  48. MailAddressCollection to;
  49. string subject;
  50. Encoding subjectEncoding, bodyEncoding, headersEncoding = Encoding.UTF8;
  51. bool isHtml;
  52. #endregion // Fields
  53. #region Constructors
  54. public MailMessage () {
  55. this.to = new MailAddressCollection ();
  56. alternateViews = new AlternateViewCollection ();
  57. attachments = new AttachmentCollection ();
  58. bcc = new MailAddressCollection ();
  59. cc = new MailAddressCollection ();
  60. replyTo = new MailAddressCollection ();
  61. headers = new NameValueCollection ();
  62. headers.Add ("MIME-Version", "1.0");
  63. }
  64. // FIXME: should it throw a FormatException if the addresses are wrong?
  65. // (How is it possible to instantiate such a malformed MailAddress?)
  66. public MailMessage (MailAddress from, MailAddress to) : this ()
  67. {
  68. if (from == null || to == null)
  69. throw new ArgumentNullException ();
  70. From = from;
  71. this.to.Add (to);
  72. }
  73. public MailMessage (string from, string to) : this ()
  74. {
  75. if (from == null || from == String.Empty)
  76. throw new ArgumentNullException ("from");
  77. if (to == null || to == String.Empty)
  78. throw new ArgumentNullException ("to");
  79. this.from = new MailAddress (from);
  80. foreach (string recipient in to.Split (new char [] {','}))
  81. this.to.Add (new MailAddress (recipient.Trim ()));
  82. }
  83. public MailMessage (string from, string to, string subject, string body) : this ()
  84. {
  85. if (from == null || from == String.Empty)
  86. throw new ArgumentNullException ("from");
  87. if (to == null || to == String.Empty)
  88. throw new ArgumentNullException ("to");
  89. this.from = new MailAddress (from);
  90. foreach (string recipient in to.Split (new char [] {','}))
  91. this.to.Add (new MailAddress (recipient.Trim ()));
  92. Body = body;
  93. Subject = subject;
  94. }
  95. #endregion // Constructors
  96. #region Properties
  97. public AlternateViewCollection AlternateViews {
  98. get { return alternateViews; }
  99. }
  100. public AttachmentCollection Attachments {
  101. get { return attachments; }
  102. }
  103. public MailAddressCollection Bcc {
  104. get { return bcc; }
  105. }
  106. public string Body {
  107. get { return body; }
  108. set {
  109. // autodetect suitable body encoding (ASCII or UTF-8), if it is not initialized yet.
  110. if (value != null && bodyEncoding == null)
  111. bodyEncoding = GuessEncoding (value) ?? Encoding.ASCII;
  112. body = value;
  113. }
  114. }
  115. internal ContentType BodyContentType {
  116. get {
  117. ContentType ct = new ContentType (isHtml ? "text/html" : "text/plain");
  118. ct.CharSet = (BodyEncoding ?? Encoding.ASCII).HeaderName;
  119. return ct;
  120. }
  121. }
  122. internal TransferEncoding ContentTransferEncoding {
  123. get { return GuessTransferEncoding (BodyEncoding); }
  124. }
  125. public Encoding BodyEncoding {
  126. get { return bodyEncoding; }
  127. set { bodyEncoding = value; }
  128. }
  129. public TransferEncoding BodyTransferEncoding {
  130. get { return GuessTransferEncoding (BodyEncoding); }
  131. set { throw new NotImplementedException (); }
  132. }
  133. public MailAddressCollection CC {
  134. get { return cc; }
  135. }
  136. public DeliveryNotificationOptions DeliveryNotificationOptions {
  137. get { return deliveryNotificationOptions; }
  138. set { deliveryNotificationOptions = value; }
  139. }
  140. public MailAddress From {
  141. get { return from; }
  142. set { from = value; }
  143. }
  144. public NameValueCollection Headers {
  145. get { return headers; }
  146. }
  147. public bool IsBodyHtml {
  148. get { return isHtml; }
  149. set { isHtml = value; }
  150. }
  151. public MailPriority Priority {
  152. get { return priority; }
  153. set { priority = value; }
  154. }
  155. public
  156. Encoding HeadersEncoding {
  157. get { return headersEncoding; }
  158. set { headersEncoding = value; }
  159. }
  160. public
  161. MailAddressCollection ReplyToList {
  162. get { return replyTo; }
  163. }
  164. [Obsolete ("Use ReplyToList instead")]
  165. public MailAddress ReplyTo {
  166. get {
  167. if (replyTo.Count == 0)
  168. return null;
  169. return replyTo [0];
  170. }
  171. set {
  172. replyTo.Clear ();
  173. replyTo.Add (value);
  174. }
  175. }
  176. public MailAddress Sender {
  177. get { return sender; }
  178. set { sender = value; }
  179. }
  180. public string Subject {
  181. get { return subject; }
  182. set {
  183. if (value != null && subjectEncoding == null)
  184. subjectEncoding = GuessEncoding (value);
  185. subject = value;
  186. }
  187. }
  188. public Encoding SubjectEncoding {
  189. get { return subjectEncoding; }
  190. set { subjectEncoding = value; }
  191. }
  192. public MailAddressCollection To {
  193. get { return to; }
  194. }
  195. #endregion // Properties
  196. #region Methods
  197. public void Dispose ()
  198. {
  199. Dispose (true);
  200. GC.SuppressFinalize (this);
  201. }
  202. protected virtual void Dispose (bool disposing)
  203. {
  204. }
  205. private Encoding GuessEncoding (string s)
  206. {
  207. for (int i = 0; i < s.Length; i++)
  208. if (s [i] >= '\u0080')
  209. return UTF8Unmarked;
  210. return null;
  211. }
  212. internal static TransferEncoding GuessTransferEncoding (Encoding enc)
  213. {
  214. if (Encoding.ASCII.Equals (enc))
  215. return TransferEncoding.SevenBit;
  216. else if (Encoding.UTF8.CodePage == enc.CodePage ||
  217. #if !MOBILE
  218. Encoding.Unicode.CodePage == enc.CodePage || Encoding.UTF32.CodePage == enc.CodePage
  219. #else
  220. Encoding.Unicode.CodePage == enc.CodePage
  221. #endif
  222. )
  223. return TransferEncoding.Base64;
  224. else
  225. return TransferEncoding.QuotedPrintable;
  226. }
  227. static char [] hex = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  228. internal static string To2047(byte [] bytes)
  229. {
  230. StringBuilder sb = new StringBuilder ();
  231. foreach (byte i in bytes) {
  232. if (i < 0x21 || i > 0x7E || i == '?' || i == '=' || i == '_') {
  233. sb.Append ('=');
  234. sb.Append (hex [(i >> 4) & 0x0f]);
  235. sb.Append (hex [i & 0x0f]);
  236. } else
  237. sb.Append ((char) i);
  238. }
  239. return sb.ToString ();
  240. }
  241. internal static string EncodeSubjectRFC2047 (string s, Encoding enc)
  242. {
  243. if (s == null || Encoding.ASCII.Equals (enc))
  244. return s;
  245. for (int i = 0; i < s.Length; i++)
  246. if (s [i] >= '\u0080') {
  247. string quoted = To2047(enc.GetBytes (s));
  248. return String.Concat ("=?", enc.HeaderName, "?Q?", quoted, "?=");
  249. }
  250. return s;
  251. }
  252. static Encoding utf8unmarked;
  253. static Encoding UTF8Unmarked {
  254. get {
  255. if (utf8unmarked == null)
  256. utf8unmarked = new UTF8Encoding (false);
  257. return utf8unmarked;
  258. }
  259. }
  260. #endregion // Methods
  261. }
  262. }