**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Web / System.Web.Mail / MailMessageWrapper.cs
blobf50ec10c7bdc5df331c7933729e54489b47465a1
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 //
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.
30 using System;
31 using System.Collections;
32 using System.Text;
34 namespace System.Web.Mail {
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 {
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;
49 // Constructor
50 public MailMessageWrapper( MailMessage message )
52 this.message = message;
54 if( message.From != null ) {
55 from = MailAddress.Parse( message.From );
56 header.From = from.ToString();
59 if( message.To != null ) {
60 to = MailAddressCollection.Parse( message.To );
61 header.To = to.ToString();
64 if( message.Cc != null ) {
65 cc = MailAddressCollection.Parse( message.Cc );
66 header.Cc = cc.ToString();
69 if( message.Bcc != null ) {
70 bcc = MailAddressCollection.Parse( message.Bcc );
71 header.Bcc = bcc.ToString();
74 // set the subject
75 if( message.Subject != null ) {
77 // encode the subject if it needs encoding
78 if( MailUtil.NeedEncoding( message.Subject ) ) {
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 {
87 header.Subject = message.Subject;
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" );
102 // set the Contet-Base header
103 if( message.UrlContentBase != null )
104 header.ContentBase = message.UrlContentBase;
106 // set the Contet-Location header
107 if( message.UrlContentLocation != null )
108 header.ContentLocation = message.UrlContentLocation;
111 // set the content type
112 switch( message.BodyFormat ) {
114 case MailFormat.Html:
115 header.ContentType =
116 String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName );
117 break;
119 case MailFormat.Text:
120 header.ContentType =
121 String.Format( "text/plain; charset=\"{0}\"" , message.BodyEncoding.BodyName );
122 break;
124 default:
125 header.ContentType =
126 String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName );
127 break;
131 // set the priority as in the same way as .NET sdk does
132 switch( message.Priority ) {
134 case MailPriority.High:
135 header.Importance = "high";
136 break;
138 case MailPriority.Low:
139 header.Importance = "low";
140 break;
142 case MailPriority.Normal:
143 header.Importance = "normal";
144 break;
146 default:
147 header.Importance = "normal";
148 break;
152 // .NET sdk allways sets this to normal
153 header.Priority = "normal";
156 // Set the mime version
157 header.MimeVersion = "1.0";
159 // Set the transfer encoding
160 if( message.BodyEncoding is ASCIIEncoding ) {
161 header.ContentTransferEncoding = "7bit";
162 } else {
163 header.ContentTransferEncoding = "8bit";
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());
173 // Add the custom headers
174 foreach( string key in message.Headers.Keys )
175 header.Data[ key ] = (string)this.message.Headers[ key ];
178 // Properties
179 public IList Attachments {
180 get { return message.Attachments; }
183 public MailAddressCollection Bcc {
184 get { return bcc; }
187 public string Body {
188 get { return body; }
189 set { body = value; }
192 public Encoding BodyEncoding {
193 get { return message.BodyEncoding; }
194 set { message.BodyEncoding = value; }
197 public MailFormat BodyFormat {
198 get { return message.BodyFormat; }
199 set { message.BodyFormat = value; }
202 public MailAddressCollection Cc {
203 get { return cc; }
206 public MailAddress From {
207 get { return from; }
210 public MailHeader Header {
211 get { return header; }
214 public MailPriority Priority {
215 get { return message.Priority; }
216 set { message.Priority = value; }
219 public string Subject {
220 get { return message.Subject; }
221 set { message.Subject = value; }
224 public MailAddressCollection To {
225 get { return to; }
228 public string UrlContentBase {
229 get { return message.UrlContentBase; }
233 public string UrlContentLocation {
234 get { return message.UrlContentLocation; }
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 ];
245 return bodyHeaders;
249 #endif
251 #if NET_2_0
252 public IList RelatedBodyParts {
253 get { return message.RelatedBodyParts; }
255 #endif