Attachment.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // System.Net.Mail.Attachment.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.IO;
  31. using System.Net.Mime;
  32. using System.Text;
  33. namespace System.Net.Mail {
  34. public class Attachment : IDisposable
  35. {
  36. #region Fields
  37. ContentType contentType = new ContentType ();
  38. Encoding encoding;
  39. ContentDisposition contentDisposition;
  40. Stream contentStream;
  41. string contentString;
  42. string name;
  43. TransferEncoding transferEncoding;
  44. #endregion // Fields
  45. #region Constructors
  46. public Attachment ()
  47. {
  48. contentDisposition = new ContentDisposition ("attachment");
  49. }
  50. public Attachment (string contentString)
  51. {
  52. SetContent (contentString);
  53. }
  54. public Attachment (Stream contentStream, ContentType contentType)
  55. {
  56. SetContent (contentStream);
  57. this.contentType = contentType;
  58. }
  59. public Attachment (Stream contentStream, string name)
  60. {
  61. SetContent (contentStream, name);
  62. }
  63. public Attachment (string contentString, string mediaType)
  64. {
  65. SetContent (contentString, mediaType);
  66. }
  67. public Attachment (Stream contentStream, string name, string mediaType)
  68. {
  69. SetContent (contentStream, name, mediaType);
  70. }
  71. public Attachment (string contentString, string mediaType, Encoding encoding)
  72. {
  73. SetContent (contentString, mediaType, encoding);
  74. }
  75. #endregion // Constructors
  76. #region Properties
  77. [MonoTODO]
  78. public ContentDisposition ContentDisposition {
  79. get { return contentDisposition; }
  80. }
  81. public Stream ContentStream {
  82. get { return contentStream; }
  83. }
  84. public string ContentString {
  85. get { return contentString; }
  86. }
  87. public Encoding ContentStringEncoding {
  88. get { return encoding; }
  89. }
  90. public ContentType ContentType {
  91. get { return contentType; }
  92. }
  93. [MonoTODO]
  94. public string FileName {
  95. get { throw new NotImplementedException (); }
  96. }
  97. public string Name {
  98. get { return contentType.Name; }
  99. set {
  100. if (value == null)
  101. throw new ArgumentNullException ();
  102. if (value.Equals (""))
  103. throw new ArgumentException ();
  104. contentType.Name = value;
  105. }
  106. }
  107. public TransferEncoding TransferEncoding {
  108. get { return transferEncoding; }
  109. set { transferEncoding = value; }
  110. }
  111. #endregion // Properties
  112. #region Methods
  113. [MonoTODO]
  114. public void Dispose ()
  115. {
  116. }
  117. public void SetContent (Stream contentStream)
  118. {
  119. this.contentStream = contentStream;
  120. contentType.MediaType = MediaTypeNames.Application.Octet;
  121. contentType.CharSet = null;
  122. transferEncoding = TransferEncoding.Base64;
  123. }
  124. public void SetContent (string contentString)
  125. {
  126. SetContent (contentString, MediaTypeNames.Text.Plain, Encoding.Default);
  127. }
  128. public void SetContent (Stream contentStream, string name)
  129. {
  130. SetContent (contentStream);
  131. contentType.Name = name;
  132. }
  133. public void SetContent (string contentString, string mediaType)
  134. {
  135. SetContent (contentString, mediaType, Encoding.Default);
  136. }
  137. public void SetContent (Stream contentStream, string name, string mediaType)
  138. {
  139. SetContent (contentStream, name);
  140. contentType.MediaType = mediaType;
  141. }
  142. public void SetContent (string contentString, string mediaType, Encoding encoding)
  143. {
  144. contentType.MediaType = mediaType;
  145. contentType.CharSet = encoding.WebName;
  146. transferEncoding = TransferEncoding.QuotedPrintable;
  147. contentStream = new MemoryStream ();
  148. StreamWriter sw = new StreamWriter (contentStream);
  149. sw.Write (contentString);
  150. sw.Flush ();
  151. contentStream.Position = 0;
  152. this.contentString = contentString;
  153. }
  154. public void SetContent (string contentString, string mediaType, Encoding encoding, TransferEncoding transferEncoding)
  155. {
  156. }
  157. public void SetContentFromFile (string fileName)
  158. {
  159. SetContentFromFile (fileName, null, null);
  160. }
  161. public void SetContentFromFile (string fileName, string name)
  162. {
  163. SetContentFromFile (fileName, name, null);
  164. }
  165. public void SetContentFromFile (string fileName, string name, string mediaType)
  166. {
  167. if (name == null) {
  168. char[] match = new char [2] {';','\\'};
  169. if (fileName.IndexOfAny (match) > 0)
  170. name = fileName.Substring (fileName.LastIndexOfAny (match));
  171. }
  172. Stream s = new FileStream (fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
  173. SetContent (s, name, mediaType);
  174. }
  175. #endregion // Methods
  176. }
  177. }
  178. #endif // NET_2_0