451c49014c63c69354dfe0c869227df756ebfb26
[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 //
7 //
8 using System;
9 using System.Collections;
10 using System.Text;
11
12 namespace System.Web.Mail {
13
14     // wraps a MailMessage to make an easier
15     // interface to work with collections of
16     // addresses instead of a single string
17     internal class MailMessageWrapper {
18                 
19         private MailAddressCollection bcc = new MailAddressCollection();
20         private MailAddressCollection cc = new MailAddressCollection();         
21         private MailAddress from;
22         private MailAddressCollection to = new MailAddressCollection();
23         private MailHeader header = new MailHeader();
24         private MailMessage message;
25         private string body;
26                 
27         // Constructor          
28         public MailMessageWrapper( MailMessage message )
29         {
30             this.message = message;
31             
32             if( message.From != null ) {
33                 from = MailAddress.Parse( message.From );
34                 header.From = from.ToString();
35             }
36             
37             if( message.To != null ) {
38                 to = MailAddressCollection.Parse( message.To );
39                 header.To = to.ToString();
40             }
41             
42             if( message.Cc != null ) {
43                 cc = MailAddressCollection.Parse( message.Cc );
44                 header.Cc = cc.ToString();
45             }
46                 
47             if( message.Bcc != null ) {
48                 bcc = MailAddressCollection.Parse( message.Bcc );
49                 header.Bcc = bcc.ToString();
50             }
51
52             
53             // set the subject
54             if( message.Subject != null ) {
55                 
56                 // encode the subject if it needs encoding
57                 if( MailUtil.NeedEncoding( message.Subject ) ) {
58                                 
59                     byte[] subjectBytes = message.BodyEncoding.GetBytes( message.Subject );
60                     // encode the subject with Base64
61                     header.Subject = String.Format( "=?{0}?B?{1}?=" , 
62                                                     message.BodyEncoding.BodyName ,
63                                                     Convert.ToBase64String( subjectBytes ) );
64                 } else {
65                     
66                     header.Subject = message.Subject;
67                 
68                 }
69             }
70
71             // convert single '.' on a line with ".." to not
72             // confuse the smtp server since the DATA command
73             // is terminated with a '.' on a single line.
74             // this is also according to the smtp specs.
75             if( message.Body != null ) {
76                 body = message.Body.Replace( "\n.\n" , "\n..\n" );
77                 body = body.Replace( "\r\n.\r\n" , "\r\n..\r\n" );
78             }
79             
80             
81             // set the Contet-Base header
82             if( message.UrlContentBase != null ) 
83                 header.ContentBase = message.UrlContentBase;
84             
85             // set the Contet-Location header
86             if( message.UrlContentLocation != null ) 
87                 header.ContentLocation = message.UrlContentLocation;
88
89                     
90             // set the content type
91             switch( message.BodyFormat ) {
92                 
93             case MailFormat.Html: 
94                 header.ContentType = 
95                     String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName ); 
96                 break;
97             
98             case MailFormat.Text: 
99                 header.ContentType = 
100                     String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName );
101                 break;
102             
103             default: 
104                 header.ContentType = 
105                     String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName );
106                 break;
107             }
108             
109                     
110             // set the priority as in the same way as .NET sdk does
111             switch( message.Priority ) {
112                 
113             case MailPriority.High: 
114                 header.Importance = "high";
115                 break;
116             
117             case MailPriority.Low: 
118                 header.Importance = "low";
119                 break;
120                 
121             case MailPriority.Normal: 
122                 header.Importance = "normal";
123                 break;
124                 
125             default: 
126                 header.Importance = "normal";
127                 break;
128
129             }
130
131             // .NET sdk allways sets this to normal
132             header.Priority = "normal";
133             
134             
135             // Set the mime version
136             header.MimeVersion = "1.0";
137             
138             // Set the transfer encoding
139             if( message.BodyEncoding is ASCIIEncoding ) {
140                 header.ContentTransferEncoding = "7bit";
141             } else {
142                 header.ContentTransferEncoding = "8bit";
143             }
144
145             
146             // Add the custom headers
147             foreach( string key in message.Headers.Keys )
148                 header.Data[ key ] = (string)this.message.Headers[ key ];
149         }               
150         
151         // Properties
152         public IList Attachments {
153             get { return message.Attachments; }
154         }               
155                 
156         public MailAddressCollection Bcc {
157             get { return bcc; } 
158         }
159         
160         public string Body {
161             get { return body; } 
162             set { body = value; } 
163         }
164
165         public Encoding BodyEncoding {
166             get { return message.BodyEncoding; } 
167             set { message.BodyEncoding = value; }
168         }
169
170         public MailFormat BodyFormat {
171             get { return message.BodyFormat; } 
172             set { message.BodyFormat = value; }
173         }               
174
175         public MailAddressCollection Cc {
176             get { return cc; } 
177         }
178
179         public MailAddress From {
180             get { return from; } 
181         }
182
183         public MailHeader Header {
184             get { return header; }
185         }
186                 
187         public MailPriority Priority {
188             get { return message.Priority; } 
189             set { message.Priority = value; }
190         }
191                 
192         public string Subject {
193             get { return message.Subject; } 
194             set { message.Subject = value; }
195         }
196
197         public MailAddressCollection To {
198             get { return to; }   
199         }
200
201         public string UrlContentBase {
202             get { return message.UrlContentBase; } 
203             
204         }
205
206         public string UrlContentLocation {
207             get { return message.UrlContentLocation; } 
208         }
209     }
210
211 }