Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / System / System.Net.Mail / Attachment.cs
1 //
2 // System.Net.Mail.Attachment.cs
3 //
4 // Author:
5 //      Tim Coleman (tim@timcoleman.com)
6 //
7 // Copyright (C) Tim Coleman, 2004
8 //
9
10 //
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.
29 //
30
31 using System.IO;
32 using System.Net.Mime;
33 using System.Text;
34
35 namespace System.Net.Mail {
36         public class Attachment : AttachmentBase
37         {
38                 #region Fields
39
40                 ContentDisposition contentDisposition = new ContentDisposition ();
41                 Encoding nameEncoding;
42
43                 #endregion // Fields
44
45                 #region Constructors
46
47                 public Attachment (string fileName)
48                         : base (fileName) {
49                         InitName (fileName);
50                 }
51
52                 public Attachment (string fileName, string mediaType)
53                         : base (fileName, mediaType) {
54                         InitName (fileName);
55                 }
56
57                 public Attachment (string fileName, ContentType contentType)
58                         : base (fileName, contentType) {
59                         InitName (fileName);
60                 }
61
62                 public Attachment (Stream contentStream, ContentType contentType)
63                         : base (contentStream, contentType) {
64                 }
65
66                 public Attachment (Stream contentStream, string name)
67                         : base (contentStream) {
68                         Name = name;
69                 }
70
71                 public Attachment (Stream contentStream, string name, string mediaType)
72                         : base (contentStream, mediaType) {
73                         Name = name;
74                 }
75
76
77
78                 #endregion // Constructors
79
80                 #region Properties
81
82                 public ContentDisposition ContentDisposition {
83                         get { return contentDisposition; }
84                 }
85
86                 public string Name {
87                         get { return ContentType.Name; }
88                         set { ContentType.Name = value; }
89                 }
90
91                 public Encoding NameEncoding {
92                         get { return nameEncoding; }
93                         set { nameEncoding = value; }
94                 }
95
96                 #endregion // Properties
97
98                 #region Methods
99                 
100                 public static Attachment CreateAttachmentFromString (string content, ContentType contentType)
101                 {
102                         if (content == null)
103                                 throw new ArgumentNullException ("content");
104                         MemoryStream ms = new MemoryStream ();
105                         StreamWriter sw = new StreamWriter (ms);
106                         sw.Write (content);
107                         sw.Flush ();
108                         ms.Position = 0;
109                         Attachment a = new Attachment (ms, contentType);
110                         a.TransferEncoding = TransferEncoding.QuotedPrintable;
111                         return a;
112                 }
113                 
114                 public static Attachment CreateAttachmentFromString (string content, string name)
115                 {
116                         if (content == null)
117                                 throw new ArgumentNullException ("content");
118                         MemoryStream ms = new MemoryStream ();
119                         StreamWriter sw = new StreamWriter (ms);
120                         sw.Write (content);
121                         sw.Flush ();
122                         ms.Position = 0;
123                         Attachment a = new Attachment (ms, new ContentType ("text/plain"));
124                         a.TransferEncoding = TransferEncoding.QuotedPrintable;
125                         a.Name = name;
126                         return a;
127                 }
128                 
129                 public static Attachment CreateAttachmentFromString (string content, string name, Encoding contentEncoding, string mediaType)
130                 {
131                         if (content == null)
132                                 throw new ArgumentNullException ("content");
133                         MemoryStream ms = new MemoryStream ();
134                         StreamWriter sw = new StreamWriter (ms, contentEncoding);
135                         sw.Write (content);
136                         sw.Flush ();
137                         ms.Position = 0;
138                         Attachment a = new Attachment (ms, name, mediaType);
139                         a.TransferEncoding = ContentType.GuessTransferEncoding (contentEncoding);
140                         a.ContentType.CharSet = sw.Encoding.BodyName;
141                         return a;
142                 }
143
144                 #endregion // Methods
145
146                 private void InitName (string fileName) {
147                         if (fileName == null) {
148                                 throw new ArgumentNullException ("fileName");
149                         }
150
151                         Name = Path.GetFileName (fileName);
152                 }
153
154         }
155 }
156