add ISafeSerializationData
[mcs.git] / class / System / System.Net.Mail / Attachment.cs
blob2ce8c91ca76284deda3397e2e2cfbcde9a14f699
1 //
2 // System.Net.Mail.Attachment.cs
3 //
4 // Author:
5 // Tim Coleman (tim@timcoleman.com)
6 //
7 // Copyright (C) Tim Coleman, 2004
8 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 #if NET_2_0
33 using System.IO;
34 using System.Net.Mime;
35 using System.Text;
37 namespace System.Net.Mail {
38 public class Attachment : AttachmentBase
40 #region Fields
42 ContentDisposition contentDisposition = new ContentDisposition ();
43 Encoding nameEncoding;
45 #endregion // Fields
47 #region Constructors
49 public Attachment (string fileName)
50 : base (fileName) {
51 InitName (fileName);
54 public Attachment (string fileName, string mediaType)
55 : base (fileName, mediaType) {
56 InitName (fileName);
59 public Attachment (string fileName, ContentType contentType)
60 : base (fileName, contentType) {
61 InitName (fileName);
64 public Attachment (Stream contentStream, ContentType contentType)
65 : base (contentStream, contentType) {
68 public Attachment (Stream contentStream, string name)
69 : base (contentStream) {
70 Name = name;
73 public Attachment (Stream contentStream, string name, string mediaType)
74 : base (contentStream, mediaType) {
75 Name = name;
80 #endregion // Constructors
82 #region Properties
84 public ContentDisposition ContentDisposition {
85 get { return contentDisposition; }
88 public string Name {
89 get { return ContentType.Name; }
90 set { ContentType.Name = value; }
93 public Encoding NameEncoding {
94 get { return nameEncoding; }
95 set { nameEncoding = value; }
98 #endregion // Properties
100 #region Methods
102 public static Attachment CreateAttachmentFromString (string content, ContentType contentType)
104 if (content == null)
105 throw new ArgumentNullException ("content");
106 MemoryStream ms = new MemoryStream ();
107 StreamWriter sw = new StreamWriter (ms);
108 sw.Write (content);
109 sw.Flush ();
110 ms.Position = 0;
111 Attachment a = new Attachment (ms, contentType);
112 a.TransferEncoding = TransferEncoding.QuotedPrintable;
113 return a;
116 public static Attachment CreateAttachmentFromString (string content, string name)
118 if (content == null)
119 throw new ArgumentNullException ("content");
120 MemoryStream ms = new MemoryStream ();
121 StreamWriter sw = new StreamWriter (ms);
122 sw.Write (content);
123 sw.Flush ();
124 ms.Position = 0;
125 Attachment a = new Attachment (ms, new ContentType ("text/plain"));
126 a.TransferEncoding = TransferEncoding.QuotedPrintable;
127 a.Name = name;
128 return a;
131 public static Attachment CreateAttachmentFromString (string content, string name, Encoding contentEncoding, string mediaType)
133 if (content == null)
134 throw new ArgumentNullException ("content");
135 MemoryStream ms = new MemoryStream ();
136 StreamWriter sw = new StreamWriter (ms, contentEncoding);
137 sw.Write (content);
138 sw.Flush ();
139 ms.Position = 0;
140 Attachment a = new Attachment (ms, name, mediaType);
141 a.TransferEncoding = ContentType.GuessTransferEncoding (contentEncoding);
142 a.ContentType.CharSet = sw.Encoding.BodyName;
143 return a;
146 #endregion // Methods
148 private void InitName (string fileName) {
149 if (fileName == null) {
150 throw new ArgumentNullException ("fileName");
153 Name = Path.GetFileName (fileName);
159 #endif // NET_2_0