AttachmentBase.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // System.Net.Mail.AttachmentBase.cs
  3. //
  4. // Author:
  5. // John Luke ([email protected])
  6. //
  7. // Copyright (C) John Luke, 2005
  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 abstract class AttachmentBase : IDisposable
  35. {
  36. #region Fields
  37. string id;
  38. ContentType contentType = new ContentType ();
  39. Stream contentStream;
  40. TransferEncoding transferEncoding;
  41. #endregion // Fields
  42. #region Constructors
  43. protected AttachmentBase (Stream contentStream)
  44. {
  45. if (contentStream == null)
  46. throw new ArgumentNullException ();
  47. this.contentStream = contentStream;
  48. contentType.MediaType = MediaTypeNames.Application.Octet;
  49. contentType.CharSet = null;
  50. transferEncoding = TransferEncoding.Base64;
  51. }
  52. protected AttachmentBase (Stream contentStream, ContentType contentType)
  53. {
  54. if (contentStream == null || contentType == null)
  55. throw new ArgumentNullException ();
  56. this.contentStream = contentStream;
  57. this.contentType = contentType;
  58. transferEncoding = TransferEncoding.Base64;
  59. }
  60. protected AttachmentBase (Stream contentStream, string mediaType)
  61. {
  62. if (contentStream == null)
  63. throw new ArgumentNullException ();
  64. this.contentStream = contentStream;
  65. contentType.MediaType = mediaType;
  66. transferEncoding = TransferEncoding.Base64;
  67. }
  68. protected AttachmentBase (string fileName)
  69. {
  70. if (fileName == null)
  71. throw new ArgumentNullException ();
  72. contentStream = File.OpenRead (fileName);
  73. contentType.MediaType = MediaTypeNames.Application.Octet;
  74. contentType.CharSet = null;
  75. transferEncoding = TransferEncoding.Base64;
  76. }
  77. protected AttachmentBase (string fileName, ContentType contentType)
  78. {
  79. if (fileName == null)
  80. throw new ArgumentNullException ();
  81. contentStream = File.OpenRead (fileName);
  82. this.contentType = contentType;
  83. transferEncoding = TransferEncoding.Base64;
  84. }
  85. protected AttachmentBase (string fileName, string mediaType)
  86. {
  87. if (fileName == null)
  88. throw new ArgumentNullException ();
  89. contentStream = File.OpenRead (fileName);
  90. contentType.MediaType = mediaType;
  91. transferEncoding = TransferEncoding.Base64;
  92. }
  93. #endregion // Constructors
  94. #region Properties
  95. public string ContentId {
  96. get { return id; }
  97. set {
  98. if (value == null)
  99. throw new ArgumentNullException ();
  100. id = value;
  101. }
  102. }
  103. public Stream ContentStream {
  104. get { return contentStream; }
  105. }
  106. public ContentType ContentType {
  107. get { return contentType; }
  108. set { contentType = value; }
  109. }
  110. public TransferEncoding TransferEncoding {
  111. get { return transferEncoding; }
  112. set { transferEncoding = value; }
  113. }
  114. #endregion // Properties
  115. #region Methods
  116. public void Dispose ()
  117. {
  118. Dispose (true);
  119. //GC.SuppressFinalize (this);
  120. }
  121. [MonoTODO]
  122. protected virtual void Dispose (bool disposing)
  123. {
  124. }
  125. #endregion // Methods
  126. }
  127. }
  128. #endif // NET_2_0