MailMessage.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #if NET_2_0
  30. using System.Collections.Specialized;
  31. using System.Globalization;
  32. using System.Net.Mime;
  33. using System.Text;
  34. namespace System.Net.Mail {
  35. [MonoTODO]
  36. public class MailMessage : IDisposable
  37. {
  38. #region Fields
  39. AlternateViewCollection alternateViews;
  40. AttachmentCollection attachments;
  41. MailAddressCollection bcc;
  42. string body;
  43. MailPriority priority;
  44. MailAddress replyTo, sender;
  45. DeliveryNotificationOptions deliveryNotificationOptions;
  46. MailAddressCollection cc;
  47. MailAddress from;
  48. NameValueCollection headers;
  49. MailAddressCollection to;
  50. string subject;
  51. Encoding subjectEncoding;
  52. ContentType bodyContentType;
  53. #endregion // Fields
  54. #region Constructors
  55. public MailMessage () {
  56. this.to = new MailAddressCollection ();
  57. alternateViews = new AlternateViewCollection ();
  58. attachments = new AttachmentCollection ();
  59. bcc = new MailAddressCollection ();
  60. cc = new MailAddressCollection ();
  61. headers = new NameValueCollection ();
  62. headers.Add ("MIME-Version", "1.0");
  63. }
  64. // FIXME: should throw a FormatException if the addresses are wrong.
  65. public MailMessage (MailAddress from, MailAddress to) : this ()
  66. {
  67. if (from == null || to == null)
  68. throw new ArgumentNullException ();
  69. From = from;
  70. this.to.Add (to);
  71. }
  72. public MailMessage (string from, string to) : this ()
  73. {
  74. if (from == null || from == String.Empty)
  75. throw new ArgumentNullException ("from");
  76. if (to == null || to == String.Empty)
  77. throw new ArgumentNullException ("to");
  78. this.from = new MailAddress (from);
  79. foreach (string recipient in to.Split (new char [] {','}))
  80. this.to.Add (new MailAddress (recipient.Trim ()));
  81. }
  82. public MailMessage (string from, string to, string subject, string body) : this ()
  83. {
  84. if (from == null || from == String.Empty)
  85. throw new ArgumentNullException ("from");
  86. if (to == null || to == String.Empty)
  87. throw new ArgumentNullException ("to");
  88. this.from = new MailAddress (from);
  89. foreach (string recipient in to.Split (new char [] {','}))
  90. this.to.Add (new MailAddress (recipient.Trim ()));
  91. Body = body;
  92. Subject = subject;
  93. }
  94. #endregion // Constructors
  95. #region Properties
  96. public AlternateViewCollection AlternateViews {
  97. get { return alternateViews; }
  98. }
  99. public AttachmentCollection Attachments {
  100. get { return attachments; }
  101. }
  102. public MailAddressCollection Bcc {
  103. get { return bcc; }
  104. }
  105. public string Body {
  106. get { return body; }
  107. set { body = value; }
  108. }
  109. internal ContentType BodyContentType {
  110. get {
  111. if (bodyContentType == null)
  112. bodyContentType = new ContentType ("text/plain; charset=us-ascii");
  113. return bodyContentType;
  114. }
  115. }
  116. public Encoding BodyEncoding {
  117. get { return Encoding.GetEncoding (BodyContentType.CharSet); }
  118. set { BodyContentType.CharSet = value.WebName; }
  119. }
  120. public MailAddressCollection CC {
  121. get { return cc; }
  122. }
  123. public DeliveryNotificationOptions DeliveryNotificationOptions {
  124. get { return deliveryNotificationOptions; }
  125. set { deliveryNotificationOptions = value; }
  126. }
  127. public MailAddress From {
  128. get { return from; }
  129. set { from = value; }
  130. }
  131. public NameValueCollection Headers {
  132. get { return headers; }
  133. }
  134. public bool IsBodyHtml {
  135. get { return String.Compare (BodyContentType.MediaType, "text/html", true, CultureInfo.InvariantCulture) == 0; }
  136. set {
  137. if (value)
  138. BodyContentType.MediaType = "text/html";
  139. else
  140. BodyContentType.MediaType = "text/plain";
  141. }
  142. }
  143. public MailPriority Priority {
  144. get { return priority; }
  145. set { priority = value; }
  146. }
  147. public MailAddress ReplyTo {
  148. get { return replyTo; }
  149. set { replyTo = value; }
  150. }
  151. public MailAddress Sender {
  152. get { return sender; }
  153. set { sender = value; }
  154. }
  155. public string Subject {
  156. get { return subject; }
  157. set { subject = value; }
  158. }
  159. public Encoding SubjectEncoding {
  160. get { return subjectEncoding; }
  161. set { subjectEncoding = value; }
  162. }
  163. public MailAddressCollection To {
  164. get { return to; }
  165. }
  166. #endregion // Properties
  167. #region Methods
  168. public void Dispose ()
  169. {
  170. Dispose (true);
  171. GC.SuppressFinalize (this);
  172. }
  173. protected virtual void Dispose (bool disposing)
  174. {
  175. }
  176. #endregion // Methods
  177. }
  178. }
  179. #endif // NET_2_0