2
0

MailMessage.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 ContentType.GuessTransferEncoding (BodyEncoding); }
  124. }
  125. public Encoding BodyEncoding {
  126. get { return bodyEncoding; }
  127. set { bodyEncoding = value; }
  128. }
  129. public MailAddressCollection CC {
  130. get { return cc; }
  131. }
  132. public DeliveryNotificationOptions DeliveryNotificationOptions {
  133. get { return deliveryNotificationOptions; }
  134. set { deliveryNotificationOptions = value; }
  135. }
  136. public MailAddress From {
  137. get { return from; }
  138. set { from = value; }
  139. }
  140. public NameValueCollection Headers {
  141. get { return headers; }
  142. }
  143. public bool IsBodyHtml {
  144. get { return isHtml; }
  145. set { isHtml = value; }
  146. }
  147. public MailPriority Priority {
  148. get { return priority; }
  149. set { priority = value; }
  150. }
  151. #if NET_4_0
  152. public
  153. #else
  154. internal
  155. #endif
  156. Encoding HeadersEncoding {
  157. get { return headersEncoding; }
  158. set { headersEncoding = value; }
  159. }
  160. #if NET_4_0
  161. public
  162. #else
  163. internal
  164. #endif
  165. MailAddressCollection ReplyToList {
  166. get { return replyTo; }
  167. }
  168. #if NET_4_0
  169. [Obsolete ("Use ReplyToList instead")]
  170. #endif
  171. public MailAddress ReplyTo {
  172. get {
  173. if (replyTo.Count == 0)
  174. return null;
  175. return replyTo [0];
  176. }
  177. set {
  178. replyTo.Clear ();
  179. replyTo.Add (value);
  180. }
  181. }
  182. public MailAddress Sender {
  183. get { return sender; }
  184. set { sender = value; }
  185. }
  186. public string Subject {
  187. get { return subject; }
  188. set {
  189. if (value != null && subjectEncoding == null)
  190. subjectEncoding = GuessEncoding (value);
  191. subject = value;
  192. }
  193. }
  194. public Encoding SubjectEncoding {
  195. get { return subjectEncoding; }
  196. set { subjectEncoding = value; }
  197. }
  198. public MailAddressCollection To {
  199. get { return to; }
  200. }
  201. #endregion // Properties
  202. #region Methods
  203. public void Dispose ()
  204. {
  205. Dispose (true);
  206. GC.SuppressFinalize (this);
  207. }
  208. protected virtual void Dispose (bool disposing)
  209. {
  210. }
  211. private Encoding GuessEncoding (string s)
  212. {
  213. return ContentType.GuessEncoding (s);
  214. }
  215. #endregion // Methods
  216. }
  217. }