copying the latest Sys.Web.Services from trunk.
[mono.git] / mcs / class / System.Web / System.Web.Mail / MailMessageWrapper.cs
index f074c23d867345959334642c2771431bb098b191..f50ec10c7bdc5df331c7933729e54489b47465a1 100644 (file)
@@ -3,7 +3,29 @@
 //
 // Author(s):
 //   Per Arneng <pt99par@student.bth.se>
+//   Sanjay Gupta <gsanjay@novell.com>
 //
+//   (C) 2004, Novell, Inc. (http://www.novell.com)
+//
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 using System;
 using System.Collections;
@@ -22,6 +44,7 @@ namespace System.Web.Mail {
        private MailAddressCollection to = new MailAddressCollection();
        private MailHeader header = new MailHeader();
        private MailMessage message;
+       private string body;
                
        // Constructor          
        public MailMessageWrapper( MailMessage message )
@@ -29,8 +52,8 @@ namespace System.Web.Mail {
            this.message = message;
            
            if( message.From != null ) {
-               from = MailAddress.Parse( message.From );
-               header.From = from.ToString();
+                       from = MailAddress.Parse( message.From );
+                       header.From = from.ToString();
            }
            
            if( message.To != null ) {
@@ -47,26 +70,34 @@ namespace System.Web.Mail {
                bcc = MailAddressCollection.Parse( message.Bcc );
                header.Bcc = bcc.ToString();
            }
-
-           
+   
            // set the subject
            if( message.Subject != null ) {
-               // if the BodyEncoding is not 7bit us-ascii then
-               // convert the subject using base64 
-               if( message.BodyEncoding is ASCIIEncoding ) {
-               
-                   header.Subject = message.Subject;
-                   
-               } else {
                
+               // encode the subject if it needs encoding
+               if( MailUtil.NeedEncoding( message.Subject ) ) {
+                               
                    byte[] subjectBytes = message.BodyEncoding.GetBytes( message.Subject );
                    // encode the subject with Base64
                    header.Subject = String.Format( "=?{0}?B?{1}?=" , 
                                                    message.BodyEncoding.BodyName ,
                                                    Convert.ToBase64String( subjectBytes ) );
+               } else {
+                   
+                   header.Subject = message.Subject;
+               
                }
            }
 
+           // convert single '.' on a line with ".." to not
+           // confuse the smtp server since the DATA command
+           // is terminated with a '.' on a single line.
+           // this is also according to the smtp specs.
+           if( message.Body != null ) {
+               body = message.Body.Replace( "\n.\n" , "\n..\n" );
+               body = body.Replace( "\r\n.\r\n" , "\r\n..\r\n" );
+           }
+           
            
            // set the Contet-Base header
            if( message.UrlContentBase != null ) 
@@ -87,7 +118,7 @@ namespace System.Web.Mail {
            
            case MailFormat.Text: 
                header.ContentType = 
-                   String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName );
+                   String.Format( "text/plain; charset=\"{0}\"" , message.BodyEncoding.BodyName );
                break;
            
            default: 
@@ -132,7 +163,13 @@ namespace System.Web.Mail {
                header.ContentTransferEncoding = "8bit";
            }
 
-           
+           // Add Date header, we were missing earlier 27/08/04
+           // RFC822 requires date to be in format Fri, 27 Aug 2004 20:13:20 +0530
+           //DateTime.Now gives in format 8/27/2004 8:13:00 PM
+           // Need to explore further dateTime formats available or do we need
+           // to write a function to convert.
+               //header.Data.Add ("Date", DateTime.Now.ToString()); 
+
            // Add the custom headers
            foreach( string key in message.Headers.Keys )
                header.Data[ key ] = (string)this.message.Headers[ key ];
@@ -148,8 +185,8 @@ namespace System.Web.Mail {
        }
        
        public string Body {
-           get { return message.Body; } 
-           set { message.Body = value; } 
+           get { return body; } 
+           set { body = value; } 
        }
 
        public Encoding BodyEncoding {
@@ -196,6 +233,25 @@ namespace System.Web.Mail {
        public string UrlContentLocation {
            get { return message.UrlContentLocation; } 
        }
-    }
 
+#if NET_1_1
+               public MailHeader Fields {
+                       get {
+                                       MailHeader bodyHeaders = new MailHeader();
+                                       // Add Fields to MailHeader Object
+                                       foreach( string key in message.Fields.Keys )
+                                               bodyHeaders.Data[ key ] = (string)this.message.Fields[ key ];
+
+                                       return bodyHeaders;
+                       }
+                       
+               }
+#endif
+
+#if NET_2_0
+               public IList RelatedBodyParts {
+                       get { return message.RelatedBodyParts; }
+               }
+#endif
+    }   
 }