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
: AttachmentBase
42 ContentDisposition contentDisposition
= new ContentDisposition ();
43 Encoding nameEncoding
;
49 public Attachment (string fileName
)
54 public Attachment (string fileName
, string mediaType
)
55 : base (fileName
, mediaType
) {
59 public Attachment (string fileName
, ContentType contentType
)
60 : base (fileName
, contentType
) {
64 public Attachment (Stream contentStream
, ContentType contentType
)
65 : base (contentStream
, contentType
) {
68 public Attachment (Stream contentStream
, string name
)
69 : base (contentStream
) {
73 public Attachment (Stream contentStream
, string name
, string mediaType
)
74 : base (contentStream
, mediaType
) {
80 #endregion // Constructors
84 public ContentDisposition ContentDisposition
{
85 get { return contentDisposition; }
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
102 public static Attachment
CreateAttachmentFromString (string content
, ContentType contentType
)
105 throw new ArgumentNullException ("content");
106 MemoryStream ms
= new MemoryStream ();
107 StreamWriter sw
= new StreamWriter (ms
);
111 Attachment a
= new Attachment (ms
, contentType
);
112 a
.TransferEncoding
= TransferEncoding
.QuotedPrintable
;
116 public static Attachment
CreateAttachmentFromString (string content
, string name
)
119 throw new ArgumentNullException ("content");
120 MemoryStream ms
= new MemoryStream ();
121 StreamWriter sw
= new StreamWriter (ms
);
125 Attachment a
= new Attachment (ms
, new ContentType ("text/plain"));
126 a
.TransferEncoding
= TransferEncoding
.QuotedPrintable
;
131 public static Attachment
CreateAttachmentFromString (string content
, string name
, Encoding contentEncoding
, string mediaType
)
134 throw new ArgumentNullException ("content");
135 MemoryStream ms
= new MemoryStream ();
136 StreamWriter sw
= new StreamWriter (ms
, contentEncoding
);
140 Attachment a
= new Attachment (ms
, name
, mediaType
);
141 a
.TransferEncoding
= ContentType
.GuessTransferEncoding (contentEncoding
);
142 a
.ContentType
.CharSet
= sw
.Encoding
.BodyName
;
146 #endregion // Methods
148 private void InitName (string fileName
) {
149 if (fileName
== null) {
150 throw new ArgumentNullException ("fileName");
153 Name
= Path
.GetFileName (fileName
);