2
0

AlternateView.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // System.Net.Mail.AlternateView.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. using System.IO;
  30. using System.Net.Mime;
  31. using System.Text;
  32. namespace System.Net.Mail {
  33. public class AlternateView : AttachmentBase
  34. {
  35. #region Fields
  36. Uri baseUri;
  37. LinkedResourceCollection linkedResources = new LinkedResourceCollection ();
  38. #endregion // Fields
  39. #region Constructors
  40. public AlternateView (string fileName) : base (fileName)
  41. {
  42. if (fileName == null)
  43. throw new ArgumentNullException ();
  44. }
  45. public AlternateView (string fileName, ContentType contentType) : base (fileName, contentType)
  46. {
  47. if (fileName == null)
  48. throw new ArgumentNullException ();
  49. }
  50. public AlternateView (string fileName, string mediaType) : base (fileName, mediaType)
  51. {
  52. if (fileName == null)
  53. throw new ArgumentNullException ();
  54. }
  55. public AlternateView (Stream contentStream) : base (contentStream)
  56. {
  57. }
  58. public AlternateView (Stream contentStream, string mediaType) : base (contentStream, mediaType)
  59. {
  60. }
  61. public AlternateView (Stream contentStream, ContentType contentType) : base (contentStream, contentType)
  62. {
  63. }
  64. #endregion // Constructors
  65. #region Properties
  66. #endregion // Properties
  67. public Uri BaseUri {
  68. get { return baseUri; }
  69. set { baseUri = value; }
  70. }
  71. public LinkedResourceCollection LinkedResources {
  72. get { return linkedResources; }
  73. }
  74. #region Methods
  75. public static AlternateView CreateAlternateViewFromString (string content)
  76. {
  77. if (content == null)
  78. throw new ArgumentNullException ();
  79. MemoryStream ms = new MemoryStream (Encoding.UTF8.GetBytes (content));
  80. AlternateView av = new AlternateView (ms);
  81. av.TransferEncoding = TransferEncoding.QuotedPrintable;
  82. return av;
  83. }
  84. public static AlternateView CreateAlternateViewFromString (string content, ContentType contentType)
  85. {
  86. if (content == null)
  87. throw new ArgumentNullException ("content");
  88. Encoding enc = contentType.CharSet != null ? Encoding.GetEncoding (contentType.CharSet) : Encoding.UTF8;
  89. MemoryStream ms = new MemoryStream (enc.GetBytes (content));
  90. AlternateView av = new AlternateView (ms, contentType);
  91. av.TransferEncoding = TransferEncoding.QuotedPrintable;
  92. return av;
  93. }
  94. public static AlternateView CreateAlternateViewFromString (string content, Encoding encoding, string mediaType)
  95. {
  96. if (content == null)
  97. throw new ArgumentNullException ("content");
  98. if (encoding == null)
  99. encoding = Encoding.UTF8;
  100. MemoryStream ms = new MemoryStream (encoding.GetBytes (content));
  101. ContentType ct = new ContentType ();
  102. ct.MediaType = mediaType;
  103. ct.CharSet = encoding.HeaderName;
  104. AlternateView av = new AlternateView (ms, ct);
  105. av.TransferEncoding = TransferEncoding.QuotedPrintable;
  106. return av;
  107. }
  108. protected override void Dispose (bool disposing)
  109. {
  110. if (disposing)
  111. foreach (LinkedResource lr in linkedResources)
  112. lr.Dispose ();
  113. base.Dispose (disposing);
  114. }
  115. #endregion // Methods
  116. }
  117. }