copying the latest Sys.Web.Services from trunk.
[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         private MailAddressCollection bcc = new MailAddressCollection();
42         private MailAddressCollection cc = new MailAddressCollection();         
43         private MailAddress from;
44         private MailAddressCollection to = new MailAddressCollection();
45         private MailHeader header = new MailHeader();
46         private MailMessage message;
47         private 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 = String.Format( "=?{0}?B?{1}?=" , 
83                                                     message.BodyEncoding.BodyName ,
84                                                     Convert.ToBase64String( subjectBytes ) );
85                 } else {
86                     
87                     header.Subject = message.Subject;
88                 
89                 }
90             }
91
92             // convert single '.' on a line with ".." to not
93             // confuse the smtp server since the DATA command
94             // is terminated with a '.' on a single line.
95             // this is also according to the smtp specs.
96             if( message.Body != null ) {
97                 body = message.Body.Replace( "\n.\n" , "\n..\n" );
98                 body = body.Replace( "\r\n.\r\n" , "\r\n..\r\n" );
99             }
100             
101             
102             // set the Contet-Base header
103             if( message.UrlContentBase != null ) 
104                 header.ContentBase = message.UrlContentBase;
105             
106             // set the Contet-Location header
107             if( message.UrlContentLocation != null ) 
108                 header.ContentLocation = message.UrlContentLocation;
109
110                     
111             // set the content type
112             switch( message.BodyFormat ) {
113                 
114             case MailFormat.Html: 
115                 header.ContentType = 
116                     String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName ); 
117                 break;
118             
119             case MailFormat.Text: 
120                 header.ContentType = 
121                     String.Format( "text/plain; charset=\"{0}\"" , message.BodyEncoding.BodyName );
122                 break;
123             
124             default: 
125                 header.ContentType = 
126                     String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName );
127                 break;
128             }
129             
130                     
131             // set the priority as in the same way as .NET sdk does
132             switch( message.Priority ) {
133                 
134             case MailPriority.High: 
135                 header.Importance = "high";
136                 break;
137             
138             case MailPriority.Low: 
139                 header.Importance = "low";
140                 break;
141                 
142             case MailPriority.Normal: 
143                 header.Importance = "normal";
144                 break;
145                 
146             default: 
147                 header.Importance = "normal";
148                 break;
149
150             }
151
152             // .NET sdk allways sets this to normal
153             header.Priority = "normal";
154             
155             
156             // Set the mime version
157             header.MimeVersion = "1.0";
158             
159             // Set the transfer encoding
160             if( message.BodyEncoding is ASCIIEncoding ) {
161                 header.ContentTransferEncoding = "7bit";
162             } else {
163                 header.ContentTransferEncoding = "8bit";
164             }
165
166             // Add Date header, we were missing earlier 27/08/04
167             // RFC822 requires date to be in format Fri, 27 Aug 2004 20:13:20 +0530
168             //DateTime.Now gives in format 8/27/2004 8:13:00 PM
169             // Need to explore further dateTime formats available or do we need
170             // to write a function to convert.
171                 //header.Data.Add ("Date", DateTime.Now.ToString()); 
172
173             // Add the custom headers
174             foreach( string key in message.Headers.Keys )
175                 header.Data[ key ] = (string)this.message.Headers[ key ];
176         }               
177         
178         // Properties
179         public IList Attachments {
180             get { return message.Attachments; }
181         }               
182                 
183         public MailAddressCollection Bcc {
184             get { return bcc; } 
185         }
186         
187         public string Body {
188             get { return body; } 
189             set { body = value; } 
190         }
191
192         public Encoding BodyEncoding {
193             get { return message.BodyEncoding; } 
194             set { message.BodyEncoding = value; }
195         }
196
197         public MailFormat BodyFormat {
198             get { return message.BodyFormat; } 
199             set { message.BodyFormat = value; }
200         }               
201
202         public MailAddressCollection Cc {
203             get { return cc; } 
204         }
205
206         public MailAddress From {
207             get { return from; } 
208         }
209
210         public MailHeader Header {
211             get { return header; }
212         }
213                 
214         public MailPriority Priority {
215             get { return message.Priority; } 
216             set { message.Priority = value; }
217         }
218                 
219         public string Subject {
220             get { return message.Subject; } 
221             set { message.Subject = value; }
222         }
223
224         public MailAddressCollection To {
225             get { return to; }   
226         }
227
228         public string UrlContentBase {
229             get { return message.UrlContentBase; } 
230             
231         }
232
233         public string UrlContentLocation {
234             get { return message.UrlContentLocation; } 
235         }
236
237 #if NET_1_1
238                 public MailHeader Fields {
239                         get {
240                                         MailHeader bodyHeaders = new MailHeader();
241                                         // Add Fields to MailHeader Object
242                                         foreach( string key in message.Fields.Keys )
243                                                 bodyHeaders.Data[ key ] = (string)this.message.Fields[ key ];
244
245                                         return bodyHeaders;
246                         }
247                         
248                 }
249 #endif
250
251 #if NET_2_0
252                 public IList RelatedBodyParts {
253                         get { return message.RelatedBodyParts; }
254                 }
255 #endif
256     }    
257 }