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.
32 using System.Net.Mime;
35 namespace System.Net.Mail {
36 public class Attachment : AttachmentBase
40 ContentDisposition contentDisposition = new ContentDisposition ();
41 Encoding nameEncoding;
47 public Attachment (string fileName)
52 public Attachment (string fileName, string mediaType)
53 : base (fileName, mediaType) {
57 public Attachment (string fileName, ContentType contentType)
58 : base (fileName, contentType) {
62 public Attachment (Stream contentStream, ContentType contentType)
63 : base (contentStream, contentType) {
66 public Attachment (Stream contentStream, string name)
67 : base (contentStream) {
71 public Attachment (Stream contentStream, string name, string mediaType)
72 : base (contentStream, mediaType) {
78 #endregion // Constructors
82 public ContentDisposition ContentDisposition {
83 get { return contentDisposition; }
87 get { return ContentType.Name; }
88 set { ContentType.Name = value; }
91 public Encoding NameEncoding {
92 get { return nameEncoding; }
93 set { nameEncoding = value; }
96 #endregion // Properties
100 public static Attachment CreateAttachmentFromString (string content, ContentType contentType)
103 throw new ArgumentNullException ("content");
104 MemoryStream ms = new MemoryStream ();
105 StreamWriter sw = new StreamWriter (ms);
109 Attachment a = new Attachment (ms, contentType);
110 a.TransferEncoding = TransferEncoding.QuotedPrintable;
114 public static Attachment CreateAttachmentFromString (string content, string name)
117 throw new ArgumentNullException ("content");
118 MemoryStream ms = new MemoryStream ();
119 StreamWriter sw = new StreamWriter (ms);
123 Attachment a = new Attachment (ms, new ContentType ("text/plain"));
124 a.TransferEncoding = TransferEncoding.QuotedPrintable;
129 public static Attachment CreateAttachmentFromString (string content, string name, Encoding contentEncoding, string mediaType)
132 throw new ArgumentNullException ("content");
133 MemoryStream ms = new MemoryStream ();
134 StreamWriter sw = new StreamWriter (ms, contentEncoding);
138 Attachment a = new Attachment (ms, name, mediaType);
139 a.TransferEncoding = ContentType.GuessTransferEncoding (contentEncoding);
140 a.ContentType.CharSet = sw.Encoding.BodyName;
144 #endregion // Methods
146 private void InitName (string fileName) {
147 if (fileName == null) {
148 throw new ArgumentNullException ("fileName");
151 Name = Path.GetFileName (fileName);