New test.
[mono.git] / mcs / class / System / System.Net.Mail / SmtpClient.cs
index b781d9c9773fa8da8615165f863a7561a5a6afa5..8fe2b6ae83df9d688041155c05132b316e178951 100644 (file)
 
 #if NET_2_0
 
+#if SECURITY_DEP
+extern alias PrebuiltSystem;
+#endif
+
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
+using System.Globalization;
 using System.IO;
 using System.Net;
 using System.Net.Mime;
@@ -43,6 +48,12 @@ using System.Threading;
 using System.Reflection;
 using System.Net.Configuration;
 using System.Configuration;
+using System.Net.Security;
+using System.Security.Authentication;
+
+#if SECURITY_DEP
+using X509CertificateCollection = PrebuiltSystem::System.Security.Cryptography.X509Certificates.X509CertificateCollection;
+#endif
 
 namespace System.Net.Mail {
        public class SmtpClient
@@ -53,22 +64,43 @@ namespace System.Net.Mail {
                int port;
                int timeout = 100000;
                ICredentialsByHost credentials;
-               bool useDefaultCredentials = false;
                string pickupDirectoryLocation;
                SmtpDeliveryMethod deliveryMethod;
                bool enableSsl;
+#if SECURITY_DEP               
                X509CertificateCollection clientCertificates;
+#endif         
 
                TcpClient client;
-               NetworkStream stream;
+               Stream stream;
                StreamWriter writer;
                StreamReader reader;
                int boundaryIndex;
                MailAddress defaultFrom;
 
-               Mutex mutex = new Mutex ();
+               MailMessage messageInProcess;
+
+               BackgroundWorker worker;
+               object user_async_state;
+
+               // ESMTP state
+               [Flags]
+               enum AuthMechs {
+                       None        = 0,
+                       CramMD5     = 0x01,
+                       DigestMD5   = 0x02,
+                       GssAPI      = 0x04,
+                       Kerberos4   = 0x08,
+                       Login       = 0x10,
+                       Plain       = 0x20,
+               }
 
-               const string MimeVersion = "1.0 (produced by Mono System.Net.Mail.SmtpClient)";
+               class CancellationException : Exception
+               {
+               }
+
+               AuthMechs authMechs = AuthMechs.None;
+               Mutex mutex = new Mutex ();
 
                #endregion // Fields
 
@@ -91,6 +123,11 @@ namespace System.Net.Mail {
                        if (cfg != null) {
                                this.host = cfg.Network.Host;
                                this.port = cfg.Network.Port;
+                               TargetName = cfg.Network.TargetName;
+                               if (this.TargetName == null)
+                                       TargetName = "SMTPSVC/" + (host != null ? host : "");
+
+                               
                                if (cfg.Network.UserName != null) {
                                        string password = String.Empty;
 
@@ -103,6 +140,9 @@ namespace System.Net.Mail {
                                if (cfg.From != null)
                                        defaultFrom = new MailAddress (cfg.From);
                        }
+#else
+                       // Just to eliminate the warning, this codepath does not end up in production.
+                       defaultFrom = null;
 #endif
 
                        if (!String.IsNullOrEmpty (host))
@@ -116,37 +156,54 @@ namespace System.Net.Mail {
 
                #region Properties
 
-               [MonoTODO("Client certificates are not supported")]
+#if SECURITY_DEP
+               [MonoTODO("Client certificates not used")]
                public X509CertificateCollection ClientCertificates {
                        get {
-                               throw new NotImplementedException ("Client certificates are not supported");
+                               if (clientCertificates == null)
+                                       clientCertificates = new X509CertificateCollection ();
                                return clientCertificates;
                        }
                }
+#endif
+
+#if NET_4_0
+               public
+#endif
+               string TargetName { get; set; }
 
                public ICredentialsByHost Credentials {
                        get { return credentials; }
-                       set { credentials = value; }
+                       set {
+                               CheckState ();
+                               credentials = value;
+                       }
                }
 
                public SmtpDeliveryMethod DeliveryMethod {
                        get { return deliveryMethod; }
-                       set { deliveryMethod = value; }
+                       set {
+                               CheckState ();
+                               deliveryMethod = value;
+                       }
                }
 
                public bool EnableSsl {
                        get { return enableSsl; }
-                       set { enableSsl = value; }
+                       set {
+                               CheckState ();
+                               enableSsl = value;
+                       }
                }
 
                public string Host {
                        get { return host; }
-                       // FIXME: Check to make sure an email is not being sent.
                        set {
                                if (value == null)
-                                       throw new ArgumentNullException ();
+                                       throw new ArgumentNullException ("value");
                                if (value.Length == 0)
-                                       throw new ArgumentException ();
+                                       throw new ArgumentException ("An empty string is not allowed.", "value");
+                               CheckState ();
                                host = value;
                        }
                }
@@ -158,31 +215,37 @@ namespace System.Net.Mail {
 
                public int Port {
                        get { return port; }
-                       // FIXME: Check to make sure an email is not being sent.
                        set { 
                                if (value <= 0)
-                                       throw new ArgumentOutOfRangeException ();
-                               port = value; 
+                                       throw new ArgumentOutOfRangeException ("value");
+                               CheckState ();
+                               port = value;
                        }
                }
 
+               [MonoTODO]
                public ServicePoint ServicePoint {
                        get { throw new NotImplementedException (); }
                }
 
                public int Timeout {
                        get { return timeout; }
-                       // FIXME: Check to make sure an email is not being sent.
                        set { 
                                if (value < 0)
-                                       throw new ArgumentOutOfRangeException ();
+                                       throw new ArgumentOutOfRangeException ("value");
+                               CheckState ();
                                timeout = value; 
                        }
                }
 
                public bool UseDefaultCredentials {
-                       get { return useDefaultCredentials; }
-                       set { throw new NotImplementedException ("Default credentials are not supported"); }
+                       get { return false; }
+                       [MonoNotSupported ("no DefaultCredential support in Mono")]
+                       set {
+                               if (value)
+                                       throw new NotImplementedException ("Default credentials are not supported");
+                               CheckState ();
+                       }
                }
 
                #endregion // Properties
@@ -195,6 +258,71 @@ namespace System.Net.Mail {
 
                #region Methods
 
+               private void CheckState ()
+               {
+                       if (messageInProcess != null)
+                               throw new InvalidOperationException ("Cannot set Timeout while Sending a message");
+               }
+               
+               private static string EncodeAddress(MailAddress address)
+               {
+                       string encodedDisplayName = ContentType.EncodeSubjectRFC2047 (address.DisplayName, Encoding.UTF8);
+                       return "\"" + encodedDisplayName + "\" <" + address.Address + ">";
+               }
+
+               private static string EncodeAddresses(MailAddressCollection addresses)
+               {
+                       StringBuilder sb = new StringBuilder();
+                       bool first = true;
+                       foreach (MailAddress address in addresses) {
+                               if (!first) {
+                                       sb.Append(", ");
+                               }
+                               sb.Append(EncodeAddress(address));
+                               first = false;
+                       }
+                       return sb.ToString();
+               }
+
+               private string EncodeSubjectRFC2047 (MailMessage message)
+               {
+                       return ContentType.EncodeSubjectRFC2047 (message.Subject, message.SubjectEncoding);
+               }
+
+               private string EncodeBody (MailMessage message)
+               {
+                       string body = message.Body;
+                       Encoding encoding = message.BodyEncoding;
+                       // RFC 2045 encoding
+                       switch (message.ContentTransferEncoding) {
+                       case TransferEncoding.SevenBit:
+                               return body;
+                       case TransferEncoding.Base64:
+                               return Convert.ToBase64String (encoding.GetBytes (body), Base64FormattingOptions.InsertLineBreaks);
+                       default:
+                               return ToQuotedPrintable (body, encoding);
+                       }
+               }
+
+               private string EncodeBody (AlternateView av)
+               {
+                       //Encoding encoding = av.ContentType.CharSet != null ? Encoding.GetEncoding (av.ContentType.CharSet) : Encoding.UTF8;
+
+                       byte [] bytes = new byte [av.ContentStream.Length];
+                       av.ContentStream.Read (bytes, 0, bytes.Length);
+
+                       // RFC 2045 encoding
+                       switch (av.TransferEncoding) {
+                       case TransferEncoding.SevenBit:
+                               return Encoding.ASCII.GetString (bytes);
+                       case TransferEncoding.Base64:
+                               return Convert.ToBase64String (bytes, Base64FormattingOptions.InsertLineBreaks);
+                       default:
+                               return ToQuotedPrintable (bytes);
+                       }
+               }
+
+
                private void EndSection (string section)
                {
                        SendData (String.Format ("--{0}--", section));
@@ -220,8 +348,19 @@ namespace System.Net.Mail {
 
                protected void OnSendCompleted (AsyncCompletedEventArgs e)
                {
-                       if (SendCompleted != null)
-                               SendCompleted (this, e);
+                       try {
+                               if (SendCompleted != null)
+                                       SendCompleted (this, e);
+                       } finally {
+                               worker = null;
+                               user_async_state = null;
+                       }
+               }
+
+               private void CheckCancellation ()
+               {
+                       if (worker != null && worker.CancellationPending)
+                               throw new CancellationException ();
                }
 
                private SmtpResponse Read () {
@@ -230,6 +369,8 @@ namespace System.Net.Mail {
                        bool lastLine = false;
 
                        do {
+                               CheckCancellation ();
+
                                int readLength = stream.Read (buffer, position, buffer.Length - position);
                                if (readLength > 0) {
                                        int available = position + readLength - 1;
@@ -265,49 +406,213 @@ namespace System.Net.Mail {
                                SmtpResponse response = SmtpResponse.Parse (line);
 
                                return response;
-                       }
-                       else {
+                       } else {
                                throw new System.IO.IOException ("Connection closed");
                        }
                }
 
-               public void Send (MailMessage message) {
-                       CheckHostAndPort ();
+               void ResetExtensions()
+               {
+                       authMechs = AuthMechs.None;
+               }
 
+               void ParseExtensions (string extens)
+               {
+                       char[] delims = new char [1] { ' ' };
+                       string[] parts = extens.Split ('\n');
+
+                       foreach (string part in parts) {
+                               if (part.Length < 4)
+                                       continue;
+
+                               string start = part.Substring (4);
+                               if (start.StartsWith ("AUTH ", StringComparison.Ordinal)) {
+                                       string[] options = start.Split (delims);
+                                       for (int k = 1; k < options.Length; k++) {
+                                               string option = options[k].Trim();
+                                               switch (option) {
+                                               case "CRAM-MD5":
+                                                       authMechs |= AuthMechs.CramMD5;
+                                                       break;
+                                               case "DIGEST-MD5":
+                                                       authMechs |= AuthMechs.DigestMD5;
+                                                       break;
+                                               case "GSSAPI":
+                                                       authMechs |= AuthMechs.GssAPI;
+                                                       break;
+                                               case "KERBEROS_V4":
+                                                       authMechs |= AuthMechs.Kerberos4;
+                                                       break;
+                                               case "LOGIN":
+                                                       authMechs |= AuthMechs.Login;
+                                                       break;
+                                               case "PLAIN":
+                                                       authMechs |= AuthMechs.Plain;
+                                                       break;
+                                               }
+                                       }
+                               }
+                       }
+               }
+
+               public void Send (MailMessage message)
+               {
+                       if (message == null)
+                               throw new ArgumentNullException ("message");
+
+                       if (deliveryMethod == SmtpDeliveryMethod.Network && (Host == null || Host.Trim ().Length == 0))
+                               throw new InvalidOperationException ("The SMTP host was not specified");
+                       else if (deliveryMethod == SmtpDeliveryMethod.PickupDirectoryFromIis)
+                               throw new NotSupportedException("IIS delivery is not supported");
+
+                       if (port == 0)
+                               port = 25;
+                       
                        // Block while sending
                        mutex.WaitOne ();
+                       try {
+                               messageInProcess = message;
+                               if (deliveryMethod == SmtpDeliveryMethod.SpecifiedPickupDirectory)
+                                       SendToFile (message);
+                               else
+                                       SendInternal (message);
+                       } catch (CancellationException) {
+                               // This exception is introduced for convenient cancellation process.
+                       } catch (SmtpException) {
+                               throw;
+                       } catch (Exception ex) {
+                               throw new SmtpException ("Message could not be sent.", ex);
+                       } finally {
+                               // Release the mutex to allow other threads access
+                               mutex.ReleaseMutex ();
+                               messageInProcess = null;
+                       }
+               }
 
-                       SmtpResponse status;
+               private void SendInternal (MailMessage message)
+               {
+                       CheckCancellation ();
 
-                       client = new TcpClient (host, port);
-                       stream = client.GetStream ();
-                       writer = new StreamWriter (stream);
-                       reader = new StreamReader (stream);
+                       try {
+                               client = new TcpClient (host, port);
+                               stream = client.GetStream ();
+                               // FIXME: this StreamWriter creation is bogus.
+                               // It expects as if a Stream were able to switch to SSL
+                               // mode (such behavior is only in Mainsoft Socket API).
+                               writer = new StreamWriter (stream);
+                               reader = new StreamReader (stream);
+
+                               SendCore (message);
+                       } finally {
+                               if (writer != null)
+                                       writer.Close ();
+                               if (reader != null)
+                                       reader.Close ();
+                               if (stream != null)
+                                       stream.Close ();
+                               if (client != null)
+                                       client.Close ();
+                       }
+               }
+               // FIXME: simple implementation, could be brushed up.
+               private void SendToFile (MailMessage message)
+               {
+                       if (!Path.IsPathRooted (pickupDirectoryLocation))
+                               throw new SmtpException("Only absolute directories are allowed for pickup directory.");
+
+                       string filename = Path.Combine (pickupDirectoryLocation,
+                               Guid.NewGuid() + ".eml");
+
+                       try {
+                               writer = new StreamWriter(filename);
+
+                               MailAddress from = message.From;
+
+                               if (from == null)
+                                       from = defaultFrom;
+                               
+                               SendHeader (HeaderName.Date, DateTime.Now.ToString ("ddd, dd MMM yyyy HH':'mm':'ss zzz", DateTimeFormatInfo.InvariantInfo));
+                               SendHeader (HeaderName.From, from.ToString ());
+                               SendHeader (HeaderName.To, message.To.ToString ());
+                               if (message.CC.Count > 0)
+                                       SendHeader (HeaderName.Cc, message.CC.ToString ());
+                               SendHeader (HeaderName.Subject, EncodeSubjectRFC2047 (message));
+
+                               foreach (string s in message.Headers.AllKeys)
+                                       SendHeader (s, message.Headers [s]);
+
+                               AddPriorityHeader (message);
+
+                               boundaryIndex = 0;
+                               if (message.Attachments.Count > 0)
+                                       SendWithAttachments (message);
+                               else
+                                       SendWithoutAttachments (message, null, false);
+
+
+                       } finally {
+                               if (writer != null) writer.Close(); writer = null;
+                       }
+               }
+
+               private void SendCore (MailMessage message)
+               {
+                       SmtpResponse status;
 
                        status = Read ();
                        if (IsError (status))
                                throw new SmtpException (status.StatusCode, status.Description);
 
                        // EHLO
-                       status = SendCommand (Command.Ehlo, Dns.GetHostName ());
-
+                       
+                       // FIXME: parse the list of extensions so we don't bother wasting
+                       // our time trying commands if they aren't supported.
+                       status = SendCommand ("EHLO " + Dns.GetHostName ());
+                       
                        if (IsError (status)) {
-                               throw new SmtpException (status.StatusCode, status.Description);
+                               status = SendCommand ("HELO " + Dns.GetHostName ());
+                               
+                               if (IsError (status))
+                                       throw new SmtpException (status.StatusCode, status.Description);
+                       } else {
+                               // Parse ESMTP extensions
+                               string extens = status.Description;
+                               
+                               if (extens != null)
+                                       ParseExtensions (extens);
                        }
-
-                       if (EnableSsl) {
+                       
+                       if (enableSsl) {
                                InitiateSecureConnection ();
+                               ResetExtensions();
+                               writer = new StreamWriter (stream);
+                               reader = new StreamReader (stream);
+                               status = SendCommand ("EHLO " + Dns.GetHostName ());
+                       
+                               if (IsError (status)) {
+                                       status = SendCommand ("HELO " + Dns.GetHostName ());
+                               
+                                       if (IsError (status))
+                                               throw new SmtpException (status.StatusCode, status.Description);
+                               } else {
+                                       // Parse ESMTP extensions
+                                       string extens = status.Description;
+                                       if (extens != null)
+                                               ParseExtensions (extens);
+                               }
                        }
-
-                       PerformAuthentication ();
-
+                       
+                       if (authMechs != AuthMechs.None)
+                               Authenticate ();
+                       
                        MailAddress from = message.From;
 
                        if (from == null)
                                from = defaultFrom;
                        
                        // MAIL FROM:
-                       status = SendCommand (Command.MailFrom, '<' + from.Address + '>');
+                       status = SendCommand ("MAIL FROM:<" + from.Address + '>');
                        if (IsError (status)) {
                                throw new SmtpException (status.StatusCode, status.Description);
                        }
@@ -316,17 +621,17 @@ namespace System.Net.Mail {
                        List<SmtpFailedRecipientException> sfre = new List<SmtpFailedRecipientException> ();
 
                        for (int i = 0; i < message.To.Count; i ++) {
-                               status = SendCommand (Command.RcptTo, '<' + message.To [i].Address + '>');
+                               status = SendCommand ("RCPT TO:<" + message.To [i].Address + '>');
                                if (IsError (status)) 
                                        sfre.Add (new SmtpFailedRecipientException (status.StatusCode, message.To [i].Address));
                        }
                        for (int i = 0; i < message.CC.Count; i ++) {
-                               status = SendCommand (Command.RcptTo, '<' + message.CC [i].Address + '>');
+                               status = SendCommand ("RCPT TO:<" + message.CC [i].Address + '>');
                                if (IsError (status)) 
                                        sfre.Add (new SmtpFailedRecipientException (status.StatusCode, message.CC [i].Address));
                        }
                        for (int i = 0; i < message.Bcc.Count; i ++) {
-                               status = SendCommand (Command.RcptTo, '<' + message.Bcc [i].Address + '>');
+                               status = SendCommand ("RCPT TO:<" + message.Bcc [i].Address + '>');
                                if (IsError (status)) 
                                        sfre.Add (new SmtpFailedRecipientException (status.StatusCode, message.Bcc [i].Address));
                        }
@@ -343,54 +648,70 @@ namespace System.Net.Mail {
 #endif
 
                        // DATA
-                       status = SendCommand (Command.Data);
+                       status = SendCommand ("DATA");
                        if (IsError (status))
                                throw new SmtpException (status.StatusCode, status.Description);
 
                        // Send message headers
-                       SendHeader (HeaderName.From, from.ToString ());
-                       SendHeader (HeaderName.To, message.To.ToString ());
+                       string dt = DateTime.Now.ToString ("ddd, dd MMM yyyy HH':'mm':'ss zzz", DateTimeFormatInfo.InvariantInfo);
+                       // remove ':' from time zone offset (e.g. from "+01:00")
+                       dt = dt.Remove (dt.Length - 3, 1);
+                       SendHeader (HeaderName.Date, dt);
+
+                       SendHeader (HeaderName.From, EncodeAddress (from));
+                       SendHeader (HeaderName.To, EncodeAddresses (message.To));
                        if (message.CC.Count > 0)
-                               SendHeader (HeaderName.Cc, message.CC.ToString ());
-                       if (message.Bcc.Count > 0)
-                               SendHeader (HeaderName.Bcc, message.Bcc.ToString ());
-                       SendHeader (HeaderName.Subject, message.Subject);
+                               SendHeader (HeaderName.Cc, EncodeAddresses (message.CC));
+                       SendHeader (HeaderName.Subject, EncodeSubjectRFC2047 (message));
+
+                       string v = "normal";
+                               
+                       switch (message.Priority){
+                       case MailPriority.Normal:
+                               v = "normal";
+                               break;
+                               
+                       case MailPriority.Low:
+                               v = "non-urgent";
+                               break;
+                               
+                       case MailPriority.High:
+                               v = "urgent";
+                               break;
+                       }
+                       SendHeader ("Priority", v);
+                       if (message.Sender != null)
+                               SendHeader ("Sender", EncodeAddress (message.Sender));
+                       if (message.ReplyToList.Count > 0)
+                               SendHeader ("Reply-To", EncodeAddresses (message.ReplyToList));
 
+#if NET_4_0
+                       foreach (string s in message.Headers.AllKeys)
+                               SendHeader (s, ContentType.EncodeSubjectRFC2047 (message.Headers [s], message.HeadersEncoding));
+#else
                        foreach (string s in message.Headers.AllKeys)
                                SendHeader (s, message.Headers [s]);
-
+#endif
+       
                        AddPriorityHeader (message);
 
-                       bool hasAlternateViews = (message.AlternateViews.Count > 0);
-                       bool hasAttachments = (message.Attachments.Count > 0);
-
-                       if (hasAttachments || hasAlternateViews) {
-                               SendMultipartBody (message);
-                       }
-                       else {
-                               SendSimpleBody (message);
-                       }
+                       boundaryIndex = 0;
+                       if (message.Attachments.Count > 0)
+                               SendWithAttachments (message);
+                       else
+                               SendWithoutAttachments (message, null, false);
 
-                       SendData (".");
+                       SendDot ();
 
                        status = Read ();
                        if (IsError (status))
                                throw new SmtpException (status.StatusCode, status.Description);
 
                        try {
-                               status = SendCommand (Command.Quit);
+                               status = SendCommand ("QUIT");
+                       } catch (System.IO.IOException) {
+                               // We excuse server for the rude connection closing as a response to QUIT
                        }
-                       catch (System.IO.IOException) {
-                               //We excuse server for the rude connection closing as a response to QUIT
-                       }
-
-                       writer.Close ();
-                       reader.Close ();
-                       stream.Close ();
-                       client.Close ();
-
-                       // Release the mutex to allow other threads access
-                       mutex.ReleaseMutex ();
                }
 
                public void Send (string from, string to, string subject, string body)
@@ -398,18 +719,63 @@ namespace System.Net.Mail {
                        Send (new MailMessage (from, to, subject, body));
                }
 
+               private void SendDot()
+               {
+                       writer.Write(".\r\n");
+                       writer.Flush();
+               }
+
                private void SendData (string data)
                {
-                       writer.Write (data);
-                       // Certain SMTP servers will reject mail sent with unix line-endings; see http://cr.yp.to/docs/smtplf.html
-                       writer.Write ("\r\n");
+                       if (String.IsNullOrEmpty (data)) {
+                               writer.Write("\r\n");
+                               writer.Flush();
+                               return;
+                       }
+
+                       StringReader sr = new StringReader (data);
+                       string line;
+                       bool escapeDots = deliveryMethod == SmtpDeliveryMethod.Network;
+                       while ((line = sr.ReadLine ()) != null) {
+                               CheckCancellation ();
+
+                               if (escapeDots) {
+                                       int i;
+                                       for (i = 0; i < line.Length; i++) {
+                                               if (line[i] != '.')
+                                                       break;
+                                       }
+                                       if (i > 0 && i == line.Length) {
+                                               line += ".";
+                                       }
+                               }
+                               writer.Write (line);
+                               writer.Write ("\r\n");
+                       }
                        writer.Flush ();
                }
 
                public void SendAsync (MailMessage message, object userToken)
                {
-                       Send (message);
-                       OnSendCompleted (new AsyncCompletedEventArgs (null, false, userToken));
+                       if (worker != null)
+                               throw new InvalidOperationException ("Another SendAsync operation is in progress");
+
+                       worker = new BackgroundWorker ();
+                       worker.DoWork += delegate (object o, DoWorkEventArgs ea) {
+                               try {
+                                       user_async_state = ea.Argument;
+                                       Send (message);
+                               } catch (Exception ex) {
+                                       ea.Result = ex;
+                                       throw ex;
+                               }
+                       };
+                       worker.WorkerSupportsCancellation = true;
+                       worker.RunWorkerCompleted += delegate (object o, RunWorkerCompletedEventArgs ea) {
+                               // Note that RunWorkerCompletedEventArgs.UserState cannot be used (LAMESPEC)
+                               OnSendCompleted (new AsyncCompletedEventArgs (ea.Error, ea.Cancelled, user_async_state));
+                       };
+                       worker.RunWorkerAsync (userToken);
                }
 
                public void SendAsync (string from, string to, string subject, string body, object userToken)
@@ -419,7 +785,9 @@ namespace System.Net.Mail {
 
                public void SendAsyncCancel ()
                {
-                       throw new NotImplementedException ();
+                       if (worker == null)
+                               throw new InvalidOperationException ("SendAsync operation is not in progress");
+                       worker.CancelAsync ();
                }
 
                private void AddPriorityHeader (MailMessage message) {
@@ -438,59 +806,112 @@ namespace System.Net.Mail {
                }
 
                private void SendSimpleBody (MailMessage message) {
-                       SendHeader ("Content-Type", message.BodyContentType.ToString ());
+                       SendHeader (HeaderName.ContentType, message.BodyContentType.ToString ());
+                       if (message.ContentTransferEncoding != TransferEncoding.SevenBit)
+                               SendHeader (HeaderName.ContentTransferEncoding, GetTransferEncodingName (message.ContentTransferEncoding));
                        SendData (string.Empty);
 
-                       SendData (message.Body);
+                       SendData (EncodeBody (message));
                }
 
-               private void SendMultipartBody (MailMessage message) {
-                       boundaryIndex = 0;
+               private void SendBodylessSingleAlternate (AlternateView av) {
+                       SendHeader (HeaderName.ContentType, av.ContentType.ToString ());
+                       if (av.TransferEncoding != TransferEncoding.SevenBit)
+                               SendHeader (HeaderName.ContentTransferEncoding, GetTransferEncodingName (av.TransferEncoding));
+                       SendData (string.Empty);
+
+                       SendData (EncodeBody (av));
+               }
+
+               private void SendWithoutAttachments (MailMessage message, string boundary, bool attachmentExists)
+               {
+                       if (message.Body == null && message.AlternateViews.Count == 1)
+                               SendBodylessSingleAlternate (message.AlternateViews [0]);
+                       else if (message.AlternateViews.Count > 0)
+                               SendBodyWithAlternateViews (message, boundary, attachmentExists);
+                       else
+                               SendSimpleBody (message);
+               }
+
+
+               private void SendWithAttachments (MailMessage message) {
                        string boundary = GenerateBoundary ();
 
-                       // Figure out the message content type
-                       ContentType messageContentType = message.BodyContentType;
+                       // first "multipart/mixed"
+                       ContentType messageContentType = new ContentType ();
                        messageContentType.Boundary = boundary;
                        messageContentType.MediaType = "multipart/mixed";
+                       messageContentType.CharSet = null;
 
-                       SendHeader ("Content-Type", messageContentType.ToString ());
-                       SendData (string.Empty);
-
-                       SendData (message.Body);
-                       SendData (string.Empty);
+                       SendHeader (HeaderName.ContentType, messageContentType.ToString ());
+                       SendData (String.Empty);
 
-                       message.AlternateViews.Add (AlternateView.CreateAlternateViewFromString (message.Body, new ContentType ("text/plain")));
+                       // body section
+                       Attachment body = null;
 
-                       if (message.AlternateViews.Count > 0) {
-                               SendAlternateViews (message, boundary);
+                       if (message.AlternateViews.Count > 0)
+                               SendWithoutAttachments (message, boundary, true);
+                       else {
+                               body = Attachment.CreateAttachmentFromString (message.Body, null, message.BodyEncoding, message.IsBodyHtml ? "text/html" : "text/plain");
+                               message.Attachments.Insert (0, body);
                        }
 
-                       if (message.Attachments.Count > 0) {
-                               SendAttachments (message, boundary);
+                       try {
+                               SendAttachments (message, body, boundary);
+                       } finally {
+                               if (body != null)
+                                       message.Attachments.Remove (body);
                        }
 
                        EndSection (boundary);
                }
 
-               private void SendAlternateViews (MailMessage message, string boundary) {
+               private void SendBodyWithAlternateViews (MailMessage message, string boundary, bool attachmentExists)
+               {
                        AlternateViewCollection alternateViews = message.AlternateViews;
 
                        string inner_boundary = GenerateBoundary ();
 
-                       ContentType messageContentType = message.BodyContentType;
+                       ContentType messageContentType = new ContentType ();
                        messageContentType.Boundary = inner_boundary;
                        messageContentType.MediaType = "multipart/alternative";
 
-                       StartSection (boundary, messageContentType);
+                       if (!attachmentExists) {
+                               SendHeader (HeaderName.ContentType, messageContentType.ToString ());
+                               SendData (String.Empty);
+                       }
 
-                       for (int i = 0; i < alternateViews.Count; i += 1) {
-                               ContentType contentType = new ContentType (alternateViews [i].ContentType.ToString ());
-                               StartSection (inner_boundary, contentType, alternateViews [i].TransferEncoding);
+                       // body section
+                       AlternateView body = null;
+                       if (message.Body != null) {
+                               body = AlternateView.CreateAlternateViewFromString (message.Body, message.BodyEncoding, message.IsBodyHtml ? "text/html" : "text/plain");
+                               alternateViews.Insert (0, body);
+                               StartSection (boundary, messageContentType);
+                       }
+
+try {
+                       // alternate view sections
+                       foreach (AlternateView av in alternateViews) {
+
+                               string alt_boundary = null;
+                               ContentType contentType;
+                               if (av.LinkedResources.Count > 0) {
+                                       alt_boundary = GenerateBoundary ();
+                                       contentType = new ContentType ("multipart/related");
+                                       contentType.Boundary = alt_boundary;
+                                       
+                                       contentType.Parameters ["type"] = av.ContentType.ToString ();
+                                       StartSection (inner_boundary, contentType);
+                                       StartSection (alt_boundary, av.ContentType, av.TransferEncoding);
+                               } else {
+                                       contentType = new ContentType (av.ContentType.ToString ());
+                                       StartSection (inner_boundary, contentType, av.TransferEncoding);
+                               }
 
-                               switch (alternateViews [i].TransferEncoding) {
+                               switch (av.TransferEncoding) {
                                case TransferEncoding.Base64:
-                                       byte [] content = new byte [alternateViews [i].ContentStream.Length];
-                                       alternateViews [i].ContentStream.Read (content, 0, content.Length);
+                                       byte [] content = new byte [av.ContentStream.Length];
+                                       av.ContentStream.Read (content, 0, content.Length);
 #if TARGET_JVM
                                        SendData (Convert.ToBase64String (content));
 #else
@@ -498,34 +919,79 @@ namespace System.Net.Mail {
 #endif
                                        break;
                                case TransferEncoding.QuotedPrintable:
-                                       StreamReader sr = new StreamReader (alternateViews [i].ContentStream);
-                                       SendData (ToQuotedPrintable (sr.ReadToEnd ()));
+                                       byte [] bytes = new byte [av.ContentStream.Length];
+                                       av.ContentStream.Read (bytes, 0, bytes.Length);
+                                       SendData (ToQuotedPrintable (bytes));
                                        break;
-                               //case TransferEncoding.SevenBit:
-                               //case TransferEncoding.Unknown:
-                               default:
-                                       SendData ("TO BE IMPLEMENTED");
+                               case TransferEncoding.SevenBit:
+                               case TransferEncoding.Unknown:
+                                       content = new byte [av.ContentStream.Length];
+                                       av.ContentStream.Read (content, 0, content.Length);
+                                       SendData (Encoding.ASCII.GetString (content));
                                        break;
                                }
 
-                               SendData (string.Empty);
+                               if (av.LinkedResources.Count > 0) {
+                                       SendLinkedResources (message, av.LinkedResources, alt_boundary);
+                                       EndSection (alt_boundary);
+                               }
+
+                               if (!attachmentExists)
+                                       SendData (string.Empty);
                        }
 
+} finally {
+                       if (body != null)
+                               alternateViews.Remove (body);
+}
                        EndSection (inner_boundary);
                }
 
-               private void SendAttachments (MailMessage message, string boundary) {
-                       AttachmentCollection attachments = message.Attachments;
+               private void SendLinkedResources (MailMessage message, LinkedResourceCollection resources, string boundary)
+               {
+                       foreach (LinkedResource lr in resources) {
+                               StartSection (boundary, lr.ContentType, lr.TransferEncoding, lr);
 
-                       for (int i = 0; i < attachments.Count; i += 1) {
-                               ContentType contentType = new ContentType (attachments [i].ContentType.ToString ());
-                               attachments [i].ContentDisposition.FileName = attachments [i].Name;
-                               StartSection (boundary, contentType, attachments [i].TransferEncoding, attachments [i].ContentDisposition);
+                               switch (lr.TransferEncoding) {
+                               case TransferEncoding.Base64:
+                                       byte [] content = new byte [lr.ContentStream.Length];
+                                       lr.ContentStream.Read (content, 0, content.Length);
+#if TARGET_JVM
+                                       SendData (Convert.ToBase64String (content));
+#else
+                                           SendData (Convert.ToBase64String (content, Base64FormattingOptions.InsertLineBreaks));
+#endif
+                                       break;
+                               case TransferEncoding.QuotedPrintable:
+                                       byte [] bytes = new byte [lr.ContentStream.Length];
+                                       lr.ContentStream.Read (bytes, 0, bytes.Length);
+                                       SendData (ToQuotedPrintable (bytes));
+                                       break;
+                               case TransferEncoding.SevenBit:
+                               case TransferEncoding.Unknown:
+                                       content = new byte [lr.ContentStream.Length];
+                                       lr.ContentStream.Read (content, 0, content.Length);
+                                       SendData (Encoding.ASCII.GetString (content));
+                                       break;
+                               }
+                       }
+               }
 
-                               switch (attachments [i].TransferEncoding) {
+               private void SendAttachments (MailMessage message, Attachment body, string boundary) {
+                       foreach (Attachment att in message.Attachments) {
+                               ContentType contentType = new ContentType (att.ContentType.ToString ());
+                               if (att.Name != null) {
+                                       contentType.Name = att.Name;
+                                       if (att.NameEncoding != null)
+                                               contentType.CharSet = att.NameEncoding.HeaderName;
+                                       att.ContentDisposition.FileName = att.Name;
+                               }
+                               StartSection (boundary, contentType, att.TransferEncoding, att == body ? null : att.ContentDisposition);
+
+                               byte [] content = new byte [att.ContentStream.Length];
+                               att.ContentStream.Read (content, 0, content.Length);
+                               switch (att.TransferEncoding) {
                                case TransferEncoding.Base64:
-                                       byte[] content = new byte [attachments [i].ContentStream.Length];
-                                       attachments [i].ContentStream.Read (content, 0, content.Length);
 #if TARGET_JVM
                                        SendData (Convert.ToBase64String (content));
 #else
@@ -533,13 +999,11 @@ namespace System.Net.Mail {
 #endif
                                        break;
                                case TransferEncoding.QuotedPrintable:
-                                       StreamReader sr = new StreamReader (attachments [i].ContentStream);
-                                       SendData (ToQuotedPrintable (sr.ReadToEnd ()));
+                                       SendData (ToQuotedPrintable (content));
                                        break;
-                               //case TransferEncoding.SevenBit:
-                               //case TransferEncoding.Unknown:
-                               default:
-                                       SendData ("TO BE IMPLEMENTED");
+                               case TransferEncoding.SevenBit:
+                               case TransferEncoding.Unknown:
+                                       SendData (Encoding.ASCII.GetString (content));
                                        break;
                                }
 
@@ -547,14 +1011,6 @@ namespace System.Net.Mail {
                        }
                }
 
-               private SmtpResponse SendCommand (string command, string data)
-               {
-                       writer.Write (command);
-                       writer.Write (" ");
-                       SendData (data);
-                       return Read ();
-               }
-
                private SmtpResponse SendCommand (string command)
                {
                        writer.Write (command);
@@ -571,7 +1027,6 @@ namespace System.Net.Mail {
 
                private void StartSection (string section, ContentType sectionContentType)
                {
-                       SendData (string.Empty);
                        SendData (String.Format ("--{0}", section));
                        SendHeader ("content-type", sectionContentType.ToString ());
                        SendData (string.Empty);
@@ -585,31 +1040,71 @@ namespace System.Net.Mail {
                        SendData (string.Empty);
                }
 
+               private void StartSection(string section, ContentType sectionContentType, TransferEncoding transferEncoding, LinkedResource lr)
+               {
+                       SendData (String.Format("--{0}", section));
+                       SendHeader ("content-type", sectionContentType.ToString ());
+                       SendHeader ("content-transfer-encoding", GetTransferEncodingName (transferEncoding));
+
+                       if (lr.ContentId != null && lr.ContentId.Length > 0)
+                               SendHeader("content-ID", "<" + lr.ContentId + ">");
+
+                       SendData (string.Empty);
+               }
+
                private void StartSection (string section, ContentType sectionContentType, TransferEncoding transferEncoding, ContentDisposition contentDisposition) {
                        SendData (String.Format ("--{0}", section));
                        SendHeader ("content-type", sectionContentType.ToString ());
                        SendHeader ("content-transfer-encoding", GetTransferEncodingName (transferEncoding));
-                       SendHeader ("content-disposition", contentDisposition.ToString ());
+                       if (contentDisposition != null)
+                               SendHeader ("content-disposition", contentDisposition.ToString ());
                        SendData (string.Empty);
                }
 
-               private string ToQuotedPrintable (string input) {
-                       StringReader reader = new StringReader (input);
-                       StringWriter writer = new StringWriter ();
-                       int i;
+               // use proper encoding to escape input
+               private string ToQuotedPrintable (string input, Encoding enc)
+               {
+                       byte [] bytes = enc.GetBytes (input);
+                       return ToQuotedPrintable (bytes);
+               }
 
-                       while ((i = reader.Read ()) > 0) {
-                               if (i > 127) {
-                                       writer.Write ("=");
-                                       writer.Write (Convert.ToString (i, 16).ToUpper ());
+               private string ToQuotedPrintable (byte [] bytes)
+               {
+                       StringWriter writer = new StringWriter ();
+                       int charsInLine = 0;
+                       int curLen;
+                       StringBuilder sb = new StringBuilder("=", 3);
+                       byte equalSign = (byte)'=';
+                       char c = (char)0;
+
+                       foreach (byte i in bytes) {
+                               if (i > 127 || i == equalSign) {
+                                       sb.Length = 1;
+                                       sb.Append(Convert.ToString (i, 16).ToUpperInvariant ());
+                                       curLen = 3;
+                               } else {
+                                       c = Convert.ToChar (i);
+                                       if (c == '\r' || c == '\n') {
+                                               writer.Write (c);
+                                               charsInLine = 0;
+                                               continue;
+                                       }
+                                       curLen = 1;
+                               }
+                               
+                               charsInLine += curLen;
+                               if (charsInLine > 75) {
+                                       writer.Write ("=\r\n");
+                                       charsInLine = curLen;
                                }
+                               if (curLen == 1)
+                                       writer.Write (c);
                                else
-                                       writer.Write (Convert.ToChar (i));
+                                       writer.Write (sb.ToString ());
                        }
 
-                       return writer.GetStringBuilder ().ToString ();
+                       return writer.ToString ();
                }
-
                private static string GetTransferEncodingName (TransferEncoding encoding)
                {
                        switch (encoding) {
@@ -623,49 +1118,62 @@ namespace System.Net.Mail {
                        return "unknown";
                }
 
+#if SECURITY_DEP
+               RemoteCertificateValidationCallback callback = delegate (object sender,
+                                                                        X509Certificate certificate,
+                                                                        X509Chain chain,
+                                                                        SslPolicyErrors sslPolicyErrors) {
+                       // honor any exciting callback defined on ServicePointManager
+                       if (ServicePointManager.ServerCertificateValidationCallback != null)
+                               return ServicePointManager.ServerCertificateValidationCallback (sender, certificate, chain, sslPolicyErrors);
+                       // otherwise provide our own
+                       if (sslPolicyErrors != SslPolicyErrors.None)
+                               throw new InvalidOperationException ("SSL authentication error: " + sslPolicyErrors);
+                       return true;
+                       };
+#endif
+
                private void InitiateSecureConnection () {
-                       SmtpResponse response = SendCommand (Command.StartTls);
+                       SmtpResponse response = SendCommand ("STARTTLS");
 
                        if (IsError (response)) {
                                throw new SmtpException (SmtpStatusCode.GeneralFailure, "Server does not support secure connections.");
                        }
 
-                       ChangeToSSLSocket ();
-               }
-
-               private bool ChangeToSSLSocket () {
 #if TARGET_JVM
-                       stream.ChangeToSSLSocket ();
+                       ((NetworkStream) stream).ChangeToSSLSocket ();
+#elif SECURITY_DEP
+                       SslStream sslStream = new SslStream (stream, false, callback, null);
+                       CheckCancellation ();
+                       sslStream.AuthenticateAsClient (Host, this.ClientCertificates, SslProtocols.Default, false);
+                       stream = sslStream;
 
-                       return true;
 #else
-                       throw new NotImplementedException ();
+                       throw new SystemException ("You are using an incomplete System.dll build");
 #endif
                }
-
-               void CheckHostAndPort () {
-                       if (String.IsNullOrEmpty (Host))
-                               throw new InvalidOperationException ("The SMTP host was not specified");
-
-                       if (Port == 0)
-                               Port = 25;
-               }
                
-               void PerformAuthentication () {
+               void Authenticate ()
+               {
+                       string user = null, pass = null;
+                       
                        if (UseDefaultCredentials) {
-                               Authenticate (
-                                       CredentialCache.DefaultCredentials.GetCredential (new System.Uri ("smtp://" + host), "basic").UserName,
-                                       CredentialCache.DefaultCredentials.GetCredential (new System.Uri ("smtp://" + host), "basic").Password);
-                       }
-                       else if (Credentials != null) {
-                               Authenticate (
-                                       Credentials.GetCredential (host, port, "smtp").UserName,
-                                       Credentials.GetCredential (host, port, "smtp").Password);
+                               user = CredentialCache.DefaultCredentials.GetCredential (new System.Uri ("smtp://" + host), "basic").UserName;
+                               pass =  CredentialCache.DefaultCredentials.GetCredential (new System.Uri ("smtp://" + host), "basic").Password;
+                       } else if (Credentials != null) {
+                               user = Credentials.GetCredential (host, port, "smtp").UserName;
+                               pass = Credentials.GetCredential (host, port, "smtp").Password;
+                       } else {
+                               return;
                        }
+                       
+                       Authenticate (user, pass);
                }
 
-               void Authenticate (string Username, string Password) {
-                       SmtpResponse status = SendCommand (Command.AuthLogin);
+               void Authenticate (string Username, string Password)
+               {
+                       // FIXME: use the proper AuthMech
+                       SmtpResponse status = SendCommand ("AUTH LOGIN");
                        if (((int) status.StatusCode) != 334) {
                                throw new SmtpException (status.StatusCode, status.Description);
                        }
@@ -680,27 +1188,9 @@ namespace System.Net.Mail {
                                throw new SmtpException (status.StatusCode, status.Description);
                        }
                }
-               /*
-                               [MonoTODO]
-                               private sealed ContextAwareResult IGetContextAwareResult.GetContextAwareResult ()
-                               {
-                                       throw new NotImplementedException ();
-                               }
-               */
+               
                #endregion // Methods
-
-               // The Command struct is used to store constant string values representing SMTP commands.
-               private struct Command {
-                       public const string Data = "DATA";
-                       public const string Helo = "HELO";
-                       public const string Ehlo = "EHLO";
-                       public const string MailFrom = "MAIL FROM:";
-                       public const string Quit = "QUIT";
-                       public const string RcptTo = "RCPT TO:";
-                       public const string StartTls = "STARTTLS";
-                       public const string AuthLogin = "AUTH LOGIN";
-               }
-
+               
                // The HeaderName struct is used to store constant string values representing mail headers.
                private struct HeaderName {
                        public const string ContentTransferEncoding = "Content-Transfer-Encoding";
@@ -715,6 +1205,7 @@ namespace System.Net.Mail {
                        public const string Priority = "Priority";
                        public const string Importance = "Importance";
                        public const string XPriority = "X-Priority";
+                       public const string Date = "Date";
                }
 
                // This object encapsulates the status code and description of an SMTP response.
@@ -727,7 +1218,7 @@ namespace System.Net.Mail {
 
                                if (line.Length < 4)
                                        throw new SmtpException ("Response is to short " +
-                                                                  line.Length + ".");
+                                                                line.Length + ".");
 
                                if ((line [3] != ' ') && (line [3] != '-'))
                                        throw new SmtpException ("Response format is wrong.(" +
@@ -736,7 +1227,7 @@ namespace System.Net.Mail {
                                // parse the response code
                                response.StatusCode = (SmtpStatusCode) Int32.Parse (line.Substring (0, 3));
 
-                               // set the rawsponse
+                               // set the raw response
                                response.Description = line;
 
                                return response;