MailMessage.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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, bodyEncoding;
  52. bool isHtml;
  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 {
  108. // autodetect suitable body encoding (ASCII or UTF-8), if it is not initialized yet.
  109. if (value != null && bodyEncoding == null)
  110. bodyEncoding = GuessEncoding (value);
  111. body = value;
  112. }
  113. }
  114. internal ContentType BodyContentType {
  115. get {
  116. ContentType ct = new ContentType (isHtml ? "text/html" : "text/plain");
  117. ct.CharSet = (BodyEncoding ?? Encoding.ASCII).HeaderName;
  118. return ct;
  119. }
  120. }
  121. internal TransferEncoding ContentTransferEncoding {
  122. get {
  123. Encoding enc = BodyEncoding;
  124. if (Encoding.ASCII.Equals (enc))
  125. return TransferEncoding.SevenBit;
  126. else if (Encoding.UTF8.CodePage == enc.CodePage ||
  127. Encoding.Unicode.CodePage == enc.CodePage ||
  128. Encoding.UTF32.CodePage == enc.CodePage)
  129. return TransferEncoding.Base64;
  130. else
  131. return TransferEncoding.QuotedPrintable;
  132. }
  133. }
  134. public Encoding BodyEncoding {
  135. get { return bodyEncoding; }
  136. set { bodyEncoding = value; }
  137. }
  138. public MailAddressCollection CC {
  139. get { return cc; }
  140. }
  141. public DeliveryNotificationOptions DeliveryNotificationOptions {
  142. get { return deliveryNotificationOptions; }
  143. set { deliveryNotificationOptions = value; }
  144. }
  145. public MailAddress From {
  146. get { return from; }
  147. set { from = value; }
  148. }
  149. public NameValueCollection Headers {
  150. get { return headers; }
  151. }
  152. public bool IsBodyHtml {
  153. get { return isHtml; }
  154. set { isHtml = value; }
  155. }
  156. public MailPriority Priority {
  157. get { return priority; }
  158. set { priority = value; }
  159. }
  160. public MailAddress ReplyTo {
  161. get { return replyTo; }
  162. set { replyTo = value; }
  163. }
  164. public MailAddress Sender {
  165. get { return sender; }
  166. set { sender = value; }
  167. }
  168. public string Subject {
  169. get { return subject; }
  170. set {
  171. if (value != null && subjectEncoding == null)
  172. subjectEncoding = GuessEncoding (value);
  173. subject = value;
  174. }
  175. }
  176. public Encoding SubjectEncoding {
  177. get { return subjectEncoding; }
  178. set { subjectEncoding = value; }
  179. }
  180. public MailAddressCollection To {
  181. get { return to; }
  182. }
  183. #endregion // Properties
  184. #region Methods
  185. public void Dispose ()
  186. {
  187. Dispose (true);
  188. GC.SuppressFinalize (this);
  189. }
  190. protected virtual void Dispose (bool disposing)
  191. {
  192. }
  193. private Encoding GuessEncoding (string s)
  194. {
  195. for (int i = 0; i < s.Length; i++)
  196. if (s [i] >= '\u0080')
  197. return Encoding.UTF8;
  198. return Encoding.ASCII;
  199. }
  200. #endregion // Methods
  201. }
  202. }
  203. #endif // NET_2_0