**** Merged from MCS ****
[mono-project.git] / mcs / class / System / System.Net.Mail / Attachment.cs
blobe034a154b699bd169654cafe9d8f98aff916a877
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 : IDisposable
40 #region Fields
42 ContentType contentType = new ContentType ();
43 Encoding encoding;
45 ContentDisposition contentDisposition;
46 Stream contentStream;
47 string contentString;
48 string name;
49 TransferEncoding transferEncoding;
51 #endregion // Fields
53 #region Constructors
55 public Attachment ()
57 contentDisposition = new ContentDisposition ("attachment");
60 public Attachment (string contentString)
62 SetContent (contentString);
65 public Attachment (Stream contentStream, ContentType contentType)
67 SetContent (contentStream);
68 this.contentType = contentType;
71 public Attachment (Stream contentStream, string name)
73 SetContent (contentStream, name);
76 public Attachment (string contentString, string mediaType)
78 SetContent (contentString, mediaType);
81 public Attachment (Stream contentStream, string name, string mediaType)
83 SetContent (contentStream, name, mediaType);
86 public Attachment (string contentString, string mediaType, Encoding encoding)
88 SetContent (contentString, mediaType, encoding);
92 #endregion // Constructors
94 #region Properties
96 [MonoTODO]
97 public ContentDisposition ContentDisposition {
98 get { return contentDisposition; }
101 public Stream ContentStream {
102 get { return contentStream; }
105 public string ContentString {
106 get { return contentString; }
109 public Encoding ContentStringEncoding {
110 get { return encoding; }
113 public ContentType ContentType {
114 get { return contentType; }
117 [MonoTODO]
118 public string FileName {
119 get { throw new NotImplementedException (); }
122 public string Name {
123 get { return contentType.Name; }
124 set {
125 if (value == null)
126 throw new ArgumentNullException ();
127 if (value.Equals (""))
128 throw new ArgumentException ();
129 contentType.Name = value;
133 public TransferEncoding TransferEncoding {
134 get { return transferEncoding; }
135 set { transferEncoding = value; }
138 #endregion // Properties
140 #region Methods
142 [MonoTODO]
143 public void Dispose ()
147 public void SetContent (Stream contentStream)
149 this.contentStream = contentStream;
150 contentType.MediaType = MediaTypeNames.Application.Octet;
151 contentType.CharSet = null;
152 transferEncoding = TransferEncoding.Base64;
155 public void SetContent (string contentString)
157 SetContent (contentString, MediaTypeNames.Text.Plain, Encoding.Default);
160 public void SetContent (Stream contentStream, string name)
162 SetContent (contentStream);
163 contentType.Name = name;
166 public void SetContent (string contentString, string mediaType)
168 SetContent (contentString, mediaType, Encoding.Default);
171 public void SetContent (Stream contentStream, string name, string mediaType)
173 SetContent (contentStream, name);
174 contentType.MediaType = mediaType;
177 public void SetContent (string contentString, string mediaType, Encoding encoding)
179 contentType.MediaType = mediaType;
180 contentType.CharSet = encoding.WebName;
182 transferEncoding = TransferEncoding.QuotedPrintable;
184 contentStream = new MemoryStream ();
185 StreamWriter sw = new StreamWriter (contentStream);
186 sw.Write (contentString);
187 sw.Flush ();
189 contentStream.Position = 0;
191 this.contentString = contentString;
194 public void SetContent (string contentString, string mediaType, Encoding encoding, TransferEncoding transferEncoding)
198 public void SetContentFromFile (string fileName)
200 SetContentFromFile (fileName, null, null);
203 public void SetContentFromFile (string fileName, string name)
205 SetContentFromFile (fileName, name, null);
208 public void SetContentFromFile (string fileName, string name, string mediaType)
210 if (name == null) {
211 char[] match = new char [2] {';','\\'};
212 if (fileName.IndexOfAny (match) > 0)
213 name = fileName.Substring (fileName.LastIndexOfAny (match));
216 Stream s = new FileStream (fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
218 SetContent (s, name, mediaType);
221 #endregion // Methods
225 #endif // NET_2_0