MailMessage.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. {
  57. }
  58. [MonoTODO ("FormatException")]
  59. public MailMessage (MailAddress from, MailAddress to)
  60. {
  61. if (from == null || to == null)
  62. throw new ArgumentNullException ();
  63. From = from;
  64. this.to = new MailAddressCollection ();
  65. this.to.Add (to);
  66. alternateViews = new AlternateViewCollection ();
  67. attachments = new AttachmentCollection ();
  68. bcc = new MailAddressCollection ();
  69. cc = new MailAddressCollection ();
  70. headers = new NameValueCollection ();
  71. headers.Add ("MIME-Version", "1.0");
  72. }
  73. public MailMessage (string from, string to)
  74. : this (new MailAddress (from), new MailAddress (to))
  75. {
  76. if (from == null || to == null)
  77. throw new ArgumentNullException ();
  78. }
  79. public MailMessage (string from, string to, string subject, string body)
  80. : this (new MailAddress (from), new MailAddress (to))
  81. {
  82. if (from == null || to == null)
  83. throw new ArgumentNullException ();
  84. Body = body;
  85. Subject = subject;
  86. }
  87. #endregion // Constructors
  88. #region Properties
  89. public AlternateViewCollection AlternateViews {
  90. get { return alternateViews; }
  91. }
  92. public AttachmentCollection Attachments {
  93. get { return attachments; }
  94. }
  95. public MailAddressCollection Bcc {
  96. get { return bcc; }
  97. }
  98. public string Body {
  99. get { return body; }
  100. set { body = value; }
  101. }
  102. internal ContentType BodyContentType {
  103. get {
  104. if (bodyContentType == null)
  105. bodyContentType = new ContentType ("text/plain; charset=us-ascii");
  106. return bodyContentType;
  107. }
  108. }
  109. public Encoding BodyEncoding {
  110. get { return Encoding.GetEncoding (BodyContentType.CharSet); }
  111. set { BodyContentType.CharSet = value.WebName; }
  112. }
  113. public MailAddressCollection CC {
  114. get { return cc; }
  115. }
  116. public DeliveryNotificationOptions DeliveryNotificationOptions {
  117. get { return deliveryNotificationOptions; }
  118. set { deliveryNotificationOptions = value; }
  119. }
  120. public MailAddress From {
  121. get { return from; }
  122. set { from = value; }
  123. }
  124. public NameValueCollection Headers {
  125. get { return headers; }
  126. }
  127. public bool IsBodyHtml {
  128. get { return String.Compare (BodyContentType.MediaType, "text/html", true, CultureInfo.InvariantCulture) == 0; }
  129. set {
  130. if (value)
  131. BodyContentType.MediaType = "text/html";
  132. else
  133. BodyContentType.MediaType = "text/plain";
  134. }
  135. }
  136. public MailPriority Priority {
  137. get { return priority; }
  138. set { priority = value; }
  139. }
  140. public MailAddress ReplyTo {
  141. get { return replyTo; }
  142. set { replyTo = value; }
  143. }
  144. public MailAddress Sender {
  145. get { return sender; }
  146. set { sender = value; }
  147. }
  148. public string Subject {
  149. get { return subject; }
  150. set { subject = value; }
  151. }
  152. public Encoding SubjectEncoding {
  153. get { return subjectEncoding; }
  154. set { subjectEncoding = value; }
  155. }
  156. public MailAddressCollection To {
  157. get { return to; }
  158. }
  159. #endregion // Properties
  160. #region Methods
  161. public void Dispose ()
  162. {
  163. Dispose (true);
  164. GC.SuppressFinalize (this);
  165. }
  166. protected virtual void Dispose (bool disposing)
  167. {
  168. }
  169. #endregion // Methods
  170. }
  171. }
  172. #endif // NET_2_0