Added MailHeader to handle the mail headers
authorPer Arneng <per@mono-cvs.ximian.com>
Wed, 12 Mar 2003 23:20:03 +0000 (23:20 -0000)
committerPer Arneng <per@mono-cvs.ximian.com>
Wed, 12 Mar 2003 23:20:03 +0000 (23:20 -0000)
svn path=/trunk/mcs/; revision=12465

mcs/class/System.Web/System.Web.Mail/MailHeader.cs [new file with mode: 0644]
mcs/class/System.Web/System.Web.Mail/MailMessageWrapper.cs
mcs/class/System.Web/System.Web.Mail/SmtpClient.cs
mcs/class/System.Web/System.Web.Mail/SmtpMail.cs
mcs/class/System.Web/System.Web.Mail/SmtpStream.cs
mcs/class/System.Web/list

diff --git a/mcs/class/System.Web/System.Web.Mail/MailHeader.cs b/mcs/class/System.Web/System.Web.Mail/MailHeader.cs
new file mode 100644 (file)
index 0000000..291b816
--- /dev/null
@@ -0,0 +1,92 @@
+//
+// System.Web.Mail.MailHeader.cs
+//
+// Author(s):
+//   Per Arneng <pt99par@student.bth.se>
+//
+//
+using System;
+using System.Collections;
+using System.Collections.Specialized;
+
+namespace System.Web.Mail {
+    
+    // This class represents the header of a mail with
+    // all the header fields.
+    internal class MailHeader {
+       
+       protected NameValueCollection data = new NameValueCollection();
+       
+       public string To {
+           get { return data[ "To" ]; }
+           set { data[ "To" ] = value; }
+       }
+
+       public string From {
+           get { return data[ "From" ]; }
+           set { data[ "From" ] = value; }
+       }
+
+       public string Cc {
+           get { return data[ "Cc" ]; }
+           set { data[ "Cc" ] = value; }
+       }
+       
+       public string Bcc {
+           get { return data[ "Bcc" ]; }
+           set { data[ "Bcc" ] = value; }
+       }
+       
+       public string Subject {
+           get { return data[ "Subject" ]; }
+           set { data[ "Subject" ] = value; }
+       }
+
+       public string Importance {
+           get { return data[ "Importance" ]; }
+           set { data[ "Importance" ] = value; }
+       }
+       
+       public string Priority {
+           get { return data[ "Priority" ]; }
+           set { data[ "Priority" ] = value; }
+       }
+       
+       public string MimeVersion {
+           get { return data[ "Mime-Version" ]; }
+           set { data[ "Mime-Version" ] = value; }
+       }
+
+       public string ContentType {
+           get { return data[ "Content-Type" ]; }
+           set { data[ "Content-Type" ] = value; }
+       } 
+       
+       public string ContentTransferEncoding{
+           get { return data[ "Content-Transfer-Encoding" ]; }
+           set { data[ "Content-Transfer-Encoding" ] = value; }
+       } 
+
+       public string ContentDisposition {
+           get { return data[ "Content-Disposition" ]; }
+           set { data[ "Content-Disposition" ] = value; }
+       } 
+
+       public string ContentBase {
+           get { return data[ "Content-Base" ]; }
+           set { data[ "Content-Base" ] = value; }
+       }
+       
+       public string ContentLocation {
+           get { return data[ "Content-Location" ]; }
+           set { data[ "Content-Location" ] = value; }
+       }       
+       
+       
+       public NameValueCollection Data {
+          get { return data; } 
+       }
+       
+    }
+
+}
index 6870a44f317401a6801a495d30b0e5d95b2cd881..f074c23d867345959334642c2771431bb098b191 100644 (file)
@@ -20,6 +20,7 @@ namespace System.Web.Mail {
        private MailAddressCollection cc = new MailAddressCollection();         
        private MailAddress from;
        private MailAddressCollection to = new MailAddressCollection();
+       private MailHeader header = new MailHeader();
        private MailMessage message;
                
        // Constructor          
@@ -27,10 +28,114 @@ namespace System.Web.Mail {
        {
            this.message = message;
            
-           if(message.From != null ) from = MailAddress.Parse( message.From );
-           if(message.To != null ) to = MailAddressCollection.Parse( message.To );
-           if(message.Cc != null )cc = MailAddressCollection.Parse( message.Cc );
-           if(message.Bcc != null )bcc = MailAddressCollection.Parse( message.Bcc );
+           if( message.From != null ) {
+               from = MailAddress.Parse( message.From );
+               header.From = from.ToString();
+           }
+           
+           if( message.To != null ) {
+               to = MailAddressCollection.Parse( message.To );
+               header.To = to.ToString();
+           }
+           
+           if( message.Cc != null ) {
+               cc = MailAddressCollection.Parse( message.Cc );
+               header.Cc = cc.ToString();
+           }
+               
+           if( message.Bcc != null ) {
+               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 {
+               
+                   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 ) );
+               }
+           }
+
+           
+           // set the Contet-Base header
+           if( message.UrlContentBase != null ) 
+               header.ContentBase = message.UrlContentBase;
+           
+           // set the Contet-Location header
+           if( message.UrlContentLocation != null ) 
+               header.ContentLocation = message.UrlContentLocation;
+
+                   
+           // set the content type
+           switch( message.BodyFormat ) {
+               
+           case MailFormat.Html: 
+               header.ContentType = 
+                   String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName ); 
+               break;
+           
+           case MailFormat.Text: 
+               header.ContentType = 
+                   String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName );
+               break;
+           
+           default: 
+               header.ContentType = 
+                   String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName );
+               break;
+           }
+           
+                   
+           // set the priority as in the same way as .NET sdk does
+           switch( message.Priority ) {
+               
+           case MailPriority.High: 
+               header.Importance = "high";
+               break;
+           
+           case MailPriority.Low: 
+               header.Importance = "low";
+               break;
+               
+           case MailPriority.Normal: 
+               header.Importance = "normal";
+               break;
+               
+           default: 
+               header.Importance = "normal";
+               break;
+
+           }
+
+           // .NET sdk allways sets this to normal
+           header.Priority = "normal";
+           
+           
+           // Set the mime version
+           header.MimeVersion = "1.0";
+           
+           // Set the transfer encoding
+           if( message.BodyEncoding is ASCIIEncoding ) {
+               header.ContentTransferEncoding = "7bit";
+           } else {
+               header.ContentTransferEncoding = "8bit";
+           }
+
+           
+           // Add the custom headers
+           foreach( string key in message.Headers.Keys )
+               header.Data[ key ] = (string)this.message.Headers[ key ];
        }               
        
        // Properties
@@ -65,8 +170,8 @@ namespace System.Web.Mail {
            get { return from; } 
        }
 
-       public IDictionary Headers {
-           get { return message.Headers; }
+       public MailHeader Header {
+           get { return header; }
        }
                
        public MailPriority Priority {
index 29dd045bb2ec3b38dbd7897bcb82029389579edb..9ef8799df3ee726f5751e1c73a38d253a73baef8 100644 (file)
@@ -58,11 +58,7 @@ namespace System.Web.Mail {
                if( msg.To.Count < 1 ) throw new SmtpException( "Atleast one recipient must be set." );
            }
            
-           // if no encoding is set then set the system
-           // default encoding
-           if( msg.BodyEncoding == null ) 
-               msg.BodyEncoding = Encoding.Default;
-           
+                   
            // start with a reset incase old data
            // is present at the server in this session
            smtp.WriteRset();
@@ -103,10 +99,8 @@ namespace System.Web.Mail {
        // sends a single part mail to the server
        private void SendSinglepartMail( MailMessageWrapper msg ) {
                            
-           // create the headers
-           IDictionary headers = CreateHeaders( msg );
-       
-           smtp.WriteHeaders( headers );
+           // write the header
+           smtp.WriteHeader( msg.Header );
            
            // send the mail body FIXME
            smtp.WriteBytes( msg.BodyEncoding.GetBytes( msg.Body ) );
@@ -115,30 +109,26 @@ namespace System.Web.Mail {
        
        // sends a multipart mail to the server
        private void SendMultipartMail( MailMessageWrapper msg ) {
-                           
-           // create the headers
-           IDictionary headers = CreateHeaders( msg );
-
+                   
            // set the part boundary FIXME: THIS SHOULD NOT BE HARDCODED
            // look att  Gaurav Vaish implementation
            string boundary = "NextPart_000_1113_1962_1fe8";
                
            // set the Content-Type header to multipart/mixed
-           headers[ "Content-Type" ] = 
+           msg.Header.ContentType = 
                String.Format( "multipart/mixed;\r\n   boundary={0}" , boundary );
                
-           // write the headers
-           // and start writing the multipart body
-           smtp.WriteHeaders( headers );
+           // write the header
+           smtp.WriteHeader( msg.Header );
                
            // write the first part text part
            // before the attachments
            smtp.WriteBoundary( boundary );
                
-           Hashtable partHeaders = new Hashtable();
-           partHeaders[ "Content-Type" ] = "text/plain";
+           MailHeader partHeader = new MailHeader();
+           partHeader.ContentType = "text/plain";
                
-           smtp.WriteHeaders( partHeaders );
+           smtp.WriteHeader( partHeader );
                
          
            // FIXME: probably need to use QP or Base64 on everything higher
@@ -154,19 +144,18 @@ namespace System.Web.Mail {
                        
                FileInfo fileInfo = new FileInfo( a.Filename );
 
-               Hashtable aHeaders = new Hashtable();
+               MailHeader aHeader = new MailHeader();
                
-               aHeaders[ "Content-Type" ] = 
+               aHeader.ContentType = 
                    String.Format( "application/octet-stream; name=\"{0}\"", 
                                   fileInfo.Name  );
                
-               aHeaders[ "Content-Disposition" ] = 
+               aHeader.ContentDisposition = 
                    String.Format( "attachment; filename=\"{0}\"" , fileInfo.Name );
                
-               aHeaders[ "Content-Transfer-Encoding" ] = 
-                   (a.Encoding == MailEncoding.UUEncode ? "UUEncode" : "Base64" );
-               
-               smtp.WriteHeaders( aHeaders );
+               aHeader.ContentTransferEncoding = a.Encoding.ToString();
+                               
+               smtp.WriteHeader( aHeader );
                   
                // perform the actual writing of the file.
                // read from the file stream and write to the tcp stream
@@ -201,127 +190,6 @@ namespace System.Web.Mail {
               
        }
        
-       // send the standard headers
-       // and the custom in MailMessage
-       private IDictionary CreateHeaders( MailMessageWrapper msg ) {
-           Hashtable headers = new Hashtable(); 
-           
-           headers[ "From" ] = msg.From.ToString();
-           headers[ "To" ] = msg.To.ToString();
-                   
-           if( msg.Cc.Count > 0 ) headers[ "Cc" ] = msg.Cc.ToString();
-                           
-           if( msg.Bcc.Count > 0 ) headers[ "Bcc" ] = msg.Bcc.ToString();
-           
-           if( HasData( msg.Subject ) ) {
-               
-               // if the BodyEncoding is not 7bit us-ascii then
-               // convert using base64 
-               if( msg.BodyEncoding is ASCIIEncoding ) {
-               
-                   headers[ "Subject" ] = msg.Subject;
-               
-               } else {
-               
-                   byte[] subjectBytes = msg.BodyEncoding.GetBytes( msg.Subject );
-                   // encode the subject with Base64
-                   headers[ "Subject" ] = 
-                       String.Format( "=?{0}?{1}?{2}?=" , 
-                                      msg.BodyEncoding.BodyName , "B",
-                                      Convert.ToBase64String( subjectBytes ) );
-               }
-
-           }
-           
-           if( HasData( msg.UrlContentBase ) ) 
-               headers[ "Content-Base" ] = msg.UrlContentBase;
-           
-           if( HasData( msg.UrlContentLocation ) ) 
-               headers[ "Content-Location" ] = msg.UrlContentLocation;
-          
-           
-           string charset = String.Format( "charset=\"{0}\"" , msg.BodyEncoding.BodyName );
-
-           // set body the content type
-           switch( msg.BodyFormat ) {
-               
-           case MailFormat.Html: 
-               headers[ "Content-Type" ] = "text/html; " + charset; 
-               break;
-           
-           case MailFormat.Text: 
-               headers[ "Content-Type" ] = "text/plain; " + charset; 
-               break;
-           
-           default: 
-               headers[ "Content-Type" ] = "text/plain; " + charset; 
-               break;
-
-           }
-           
-           // set the priority as in the same way as .NET sdk does
-           switch( msg.Priority ) {
-               
-           case MailPriority.High: 
-               headers[ "Importance" ] = "high";
-               break;
-           
-           case MailPriority.Low: 
-               headers[ "Importance" ] = "low";
-               break;
-               
-           case MailPriority.Normal: 
-               headers[ "Importance" ] = "normal";
-               break;
-               
-           default: 
-               headers[ "Importance" ] = "normal";
-               break;
-
-           }
-           
-           // .NET sdk allways sets this to normal
-           headers[ "Priority" ] = "normal";
-           
-
-           // add mime version
-           headers[ "Mime-Version" ] = "1.0";
-           
-           // set the mailer -- should probably be changed
-           headers[ "X-Mailer" ] = "Mono (System.Web.Mail.SmtpMail.Send)";
-           
-           // Set the transfer encoding.. it seems like only sends 7bit
-           // if it is ASCII
-           if( msg.BodyEncoding is ASCIIEncoding ) {
-               headers[ "Content-Transfer-Encoding" ] = "7bit";
-           } else {
-               headers[ "Content-Transfer-Encoding" ] = "8bit";
-           }
-           
-           
-           // add the custom headers they will overwrite
-           // the earlier ones if they are the same
-           foreach( string key in msg.Headers.Keys )
-               headers[ key ] = (string)msg.Headers[ key ];
-               
-           
-
-           return headers;
-       }
-       
-       // returns true if str is not null and not
-       // empty
-       private bool HasData( string str ) {
-           bool hasData = false;
-           if( str != null ) {
-               if( str.Length > 0 ) {
-                   hasData = true;
-               }
-           }
-           return hasData;
-       }
-       
-       
        // send quit command and
        // closes the connection
        public void Close() {
index e11ff8a2707ab7477e569b6b51aafb669172b26c..121c07b899cf19dd84863d392c781a0d146852fb 100644 (file)
@@ -8,6 +8,7 @@
 \r
 using System;\r
 using System.Net;\r
+using System.Text;\r
 using System.IO;\r
 using System.Reflection;\r
 \r
@@ -34,7 +35,11 @@ namespace System.Web.Mail
                \r
                public static void Send (MailMessage message) \r
                {\r
-                                   \r
+                   \r
+                   // if no encoding is set then set the system\r
+                   // default encoding\r
+                   if( message.BodyEncoding == null ) message.BodyEncoding = Encoding.Default;\r
+                   \r
                    try {\r
                        \r
                        MailMessageWrapper messageWrapper = new MailMessageWrapper( message );\r
index bb0af14c56bb67cb1f711bc887cf813f473c0988..ec683940baea17933d3729fb5bacf4e72e305e8f 100644 (file)
@@ -104,10 +104,10 @@ namespace System.Web.Mail {
        }
        
        
-       public void WriteHeaders( IDictionary headers ) {
+       public void WriteHeader( MailHeader header ) {
            // write the headers
-           foreach( string key in headers.Keys )
-               WriteLine( "{0}: {1}" , key , (string)headers[ key ] );
+           foreach( string key in header.Data.AllKeys )
+               WriteLine( "{0}: {1}" , key , header.Data[ key ] );
            
            // write the header end tag
            WriteLine( "" );
index 2091d2b7b6e5befce53f08c99f4994b7c06dc41b..8d6991360b63bf22ef606902396e4fc087403210 100755 (executable)
@@ -105,6 +105,7 @@ System.Web.Mail/UUAttachmentEncoder.cs
 System.Web.Mail/IAttachmentEncoder.cs
 System.Web.Mail/Base64AttachmentEncoder.cs
 System.Web.Mail/ToUUEncodingTransform.cs
+System.Web.Mail/MailHeader.cs
 System.Web.UI/BuildMethod.cs
 System.Web.UI/BuildTemplateMethod.cs
 System.Web.UI/Control.cs