2 // System.Net.Mail.Attachment.cs
5 // Tim Coleman (tim@timcoleman.com)
7 // Copyright (C) Tim Coleman, 2004
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
34 using System
.Net
.Mime
;
37 namespace System
.Net
.Mail
{
38 public class Attachment
: IDisposable
42 ContentType contentType
= new ContentType ();
45 ContentDisposition contentDisposition
;
49 TransferEncoding transferEncoding
;
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
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; }
118 public string FileName
{
119 get { throw new NotImplementedException (); }
123 get { return contentType.Name; }
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
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
);
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
)
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