System.Drawing: added email to icon and test file headers
[mono.git] / mcs / class / System.Web / System.Web.Mail / MailMessageWrapper.cs
1 //
2 // System.Web.Mail.MailMessageWrapper.cs
3 //
4 // Author(s):
5 //   Per Arneng <pt99par@student.bth.se>
6 //   Sanjay Gupta <gsanjay@novell.com>
7 //
8 //   (C) 2004, Novell, Inc. (http://www.novell.com)
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 using System;
31 using System.Collections;
32 using System.Text;
33
34 namespace System.Web.Mail {
35
36     // wraps a MailMessage to make an easier
37     // interface to work with collections of
38     // addresses instead of a single string
39     internal class MailMessageWrapper {
40                 
41         MailAddressCollection bcc = new MailAddressCollection();
42         MailAddressCollection cc = new MailAddressCollection();
43         MailAddress from;
44         MailAddressCollection to = new MailAddressCollection();
45         MailHeader header = new MailHeader();
46         MailMessage message;
47         string body;
48                 
49         // Constructor          
50         public MailMessageWrapper( MailMessage message )
51         {
52             this.message = message;
53             
54             if( message.From != null ) {
55                         from = MailAddress.Parse( message.From );
56                         header.From = from.ToString();
57             }
58             
59             if( message.To != null ) {
60                 to = MailAddressCollection.Parse( message.To );
61                 header.To = to.ToString();
62             }
63             
64             if( message.Cc != null ) {
65                 cc = MailAddressCollection.Parse( message.Cc );
66                 header.Cc = cc.ToString();
67             }
68                 
69             if( message.Bcc != null ) {
70                 bcc = MailAddressCollection.Parse( message.Bcc );
71                 header.Bcc = bcc.ToString();
72             }
73    
74             // set the subject
75             if( message.Subject != null ) {
76                 
77                 // encode the subject if it needs encoding
78                 if( MailUtil.NeedEncoding( message.Subject ) ) {
79                                 
80                     byte[] subjectBytes = message.BodyEncoding.GetBytes( message.Subject );
81                     // encode the subject with Base64
82                     header.Subject = "=?" + message.BodyEncoding.BodyName + "?B?" + Convert.ToBase64String (subjectBytes) + "?=";
83                 } else {
84                     
85                     header.Subject = message.Subject;
86                 
87                 }
88             }
89
90             // convert single '.' on a line with ".." to not
91             // confuse the smtp server since the DATA command
92             // is terminated with a '.' on a single line.
93             // this is also according to the smtp specs.
94             if( message.Body != null ) {
95                 body = message.Body.Replace( "\n.\n" , "\n..\n" );
96                 body = body.Replace( "\r\n.\r\n" , "\r\n..\r\n" );
97             }
98             
99             
100             // set the Contet-Base header
101             if( message.UrlContentBase != null ) 
102                 header.ContentBase = message.UrlContentBase;
103             
104             // set the Contet-Location header
105             if( message.UrlContentLocation != null ) 
106                 header.ContentLocation = message.UrlContentLocation;
107
108                     
109             // set the content type
110             switch( message.BodyFormat ) {              
111                     case MailFormat.Html: 
112                             header.ContentType = String.Concat ( "text/html; charset=\"", message.BodyEncoding.BodyName, "\""); 
113                             break;
114             
115                     case MailFormat.Text: 
116                             header.ContentType = String.Concat ( "text/plain; charset=\"", message.BodyEncoding.BodyName, "\"");
117                             break;
118             
119                     default: 
120                             header.ContentType = String.Concat ( "text/html; charset=\"", message.BodyEncoding.BodyName, "\"");
121                             break;
122             }
123             
124                     
125             // set the priority as in the same way as .NET sdk does
126             switch( message.Priority ) {
127                 
128             case MailPriority.High: 
129                 header.Importance = "high";
130                 break;
131             
132             case MailPriority.Low: 
133                 header.Importance = "low";
134                 break;
135                 
136             case MailPriority.Normal: 
137                 header.Importance = "normal";
138                 break;
139                 
140             default: 
141                 header.Importance = "normal";
142                 break;
143
144             }
145
146             // .NET sdk allways sets this to normal
147             header.Priority = "normal";
148             
149             
150             // Set the mime version
151             header.MimeVersion = "1.0";
152             
153             // Set the transfer encoding
154             if( message.BodyEncoding is ASCIIEncoding ) {
155                 header.ContentTransferEncoding = "7bit";
156             } else {
157                 header.ContentTransferEncoding = "8bit";
158             }
159
160             // Add Date header, we were missing earlier 27/08/04
161             // RFC822 requires date to be in format Fri, 27 Aug 2004 20:13:20 +0530
162             //DateTime.Now gives in format 8/27/2004 8:13:00 PM
163             // Need to explore further dateTime formats available or do we need
164             // to write a function to convert.
165                 //header.Data.Add ("Date", DateTime.Now.ToString()); 
166
167             // Add the custom headers
168             foreach( string key in message.Headers.Keys )
169                 header.Data[ key ] = (string)this.message.Headers[ key ];
170         }               
171         
172         // Properties
173         public IList Attachments {
174             get { return message.Attachments; }
175         }               
176                 
177         public MailAddressCollection Bcc {
178             get { return bcc; } 
179         }
180         
181         public string Body {
182             get { return body; } 
183             set { body = value; } 
184         }
185
186         public Encoding BodyEncoding {
187             get { return message.BodyEncoding; } 
188             set { message.BodyEncoding = value; }
189         }
190
191         public MailFormat BodyFormat {
192             get { return message.BodyFormat; } 
193             set { message.BodyFormat = value; }
194         }               
195
196         public MailAddressCollection Cc {
197             get { return cc; } 
198         }
199
200         public MailAddress From {
201             get { return from; } 
202         }
203
204         public MailHeader Header {
205             get { return header; }
206         }
207                 
208         public MailPriority Priority {
209             get { return message.Priority; } 
210             set { message.Priority = value; }
211         }
212                 
213         public string Subject {
214             get { return message.Subject; } 
215             set { message.Subject = value; }
216         }
217
218         public MailAddressCollection To {
219             get { return to; }   
220         }
221
222         public string UrlContentBase {
223             get { return message.UrlContentBase; } 
224             
225         }
226
227         public string UrlContentLocation {
228             get { return message.UrlContentLocation; } 
229         }
230
231 #if NET_1_1
232                 public MailHeader Fields {
233                         get {
234                                         MailHeader bodyHeaders = new MailHeader();
235                                         // Add Fields to MailHeader Object
236                                         foreach( string key in message.Fields.Keys )
237                                                 bodyHeaders.Data[ key ] = this.message.Fields[ key ].ToString();
238
239                                         return bodyHeaders;
240                         }
241                         
242                 }
243 #endif
244     }    
245 }