Merge pull request #995 from yjoly/master
[mono.git] / mcs / class / System / System.Net.Mail / SmtpClient.cs
index 46a812df323709194d108aab8d84ea5cae359a71..c6abb68630e6eb9f60d043b808294012f0849c93 100644 (file)
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if NET_2_0
-
 #if SECURITY_DEP
+
+#if MONOTOUCH || MONODROID
+using System.Security.Cryptography.X509Certificates;
+#else
 extern alias PrebuiltSystem;
+using X509CertificateCollection = PrebuiltSystem::System.Security.Cryptography.X509Certificates.X509CertificateCollection;
+using System.Security.Cryptography.X509Certificates;
+#endif
+
 #endif
 
 using System;
@@ -42,7 +48,6 @@ using System.IO;
 using System.Net;
 using System.Net.Mime;
 using System.Net.Sockets;
-using System.Security.Cryptography.X509Certificates;
 using System.Text;
 using System.Threading;
 using System.Reflection;
@@ -50,9 +55,8 @@ 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;
+#if NET_4_5
+using System.Threading.Tasks;
 #endif
 
 namespace System.Net.Mail {
@@ -151,6 +155,8 @@ namespace System.Net.Mail {
 
                        if (port != 0)
                                this.port = port;
+                       else if (this.port == 0)
+                               this.port = 25;
                }
 
                #endregion // Constructors
@@ -544,8 +550,12 @@ namespace System.Net.Mail {
                                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));
+
+                               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)
@@ -738,6 +748,43 @@ namespace System.Net.Mail {
                        Send (new MailMessage (from, to, subject, body));
                }
 
+#if NET_4_5
+               public Task SendMailAsync (MailMessage message)
+               {
+                       var tcs = new TaskCompletionSource<object> ();
+                       SendCompletedEventHandler handler = null;
+                       handler = (s, e) => SendMailAsyncCompletedHandler (tcs, e, handler, this);
+                       SendCompleted += handler;
+                       SendAsync (message, tcs);
+                       return tcs.Task;
+               }
+
+               public Task SendMailAsync (string from, string recipients, string subject, string body)
+               {
+                       return SendMailAsync (new MailMessage (from, recipients, subject, body));
+               }
+
+               static void SendMailAsyncCompletedHandler (TaskCompletionSource<object> source, AsyncCompletedEventArgs e, SendCompletedEventHandler handler, SmtpClient client)
+               {
+                       if (handler != e.UserState)
+                               return;
+
+                       client.SendCompleted -= handler;
+
+                       if (e.Error != null) {
+                               source.SetException (e.Error);
+                               return;
+                       }
+
+                       if (e.Cancelled) {
+                               source.SetCanceled ();
+                               return;
+                       }
+
+                       source.SetResult (null);
+               }
+#endif
+
                private void SendDot()
                {
                        writer.Write(".\r\n");
@@ -921,10 +968,10 @@ try {
                                        
                                        contentType.Parameters ["type"] = av.ContentType.ToString ();
                                        StartSection (inner_boundary, contentType);
-                                       StartSection (alt_boundary, av.ContentType, av.TransferEncoding);
+                                       StartSection (alt_boundary, av.ContentType, av);
                                } else {
                                        contentType = new ContentType (av.ContentType.ToString ());
-                                       StartSection (inner_boundary, contentType, av.TransferEncoding);
+                                       StartSection (inner_boundary, contentType, av);
                                }
 
                                switch (av.TransferEncoding) {
@@ -969,7 +1016,7 @@ try {
                private void SendLinkedResources (MailMessage message, LinkedResourceCollection resources, string boundary)
                {
                        foreach (LinkedResource lr in resources) {
-                               StartSection (boundary, lr.ContentType, lr.TransferEncoding, lr);
+                               StartSection (boundary, lr.ContentType, lr);
 
                                switch (lr.TransferEncoding) {
                                case TransferEncoding.Base64:
@@ -1005,7 +1052,7 @@ try {
                                                contentType.CharSet = att.NameEncoding.HeaderName;
                                        att.ContentDisposition.FileName = att.Name;
                                }
-                               StartSection (boundary, contentType, att.TransferEncoding, att == body ? null : att.ContentDisposition);
+                               StartSection (boundary, contentType, att, att != body);
 
                                byte [] content = new byte [att.ContentStream.Length];
                                att.ContentStream.Read (content, 0, content.Length);
@@ -1051,35 +1098,27 @@ try {
                        SendData (string.Empty);
                }
 
-               private void StartSection (string section, ContentType sectionContentType,TransferEncoding transferEncoding)
+               private void StartSection (string section, ContentType sectionContentType, AttachmentBase att)
                {
                        SendData (String.Format ("--{0}", section));
                        SendHeader ("content-type", sectionContentType.ToString ());
-                       SendHeader ("content-transfer-encoding", GetTransferEncodingName (transferEncoding));
-                       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 + ">");
-
+                       SendHeader ("content-transfer-encoding", GetTransferEncodingName (att.TransferEncoding));
+                       if (!string.IsNullOrEmpty (att.ContentId))
+                               SendHeader("content-ID", "<" + att.ContentId + ">");
                        SendData (string.Empty);
                }
 
-               private void StartSection (string section, ContentType sectionContentType, TransferEncoding transferEncoding, ContentDisposition contentDisposition) {
+               private void StartSection (string section, ContentType sectionContentType, Attachment att, bool sendDisposition) {
                        SendData (String.Format ("--{0}", section));
+                       if (!string.IsNullOrEmpty (att.ContentId))
+                               SendHeader("content-ID", "<" + att.ContentId + ">");
                        SendHeader ("content-type", sectionContentType.ToString ());
-                       SendHeader ("content-transfer-encoding", GetTransferEncodingName (transferEncoding));
-                       if (contentDisposition != null)
-                               SendHeader ("content-disposition", contentDisposition.ToString ());
+                       SendHeader ("content-transfer-encoding", GetTransferEncodingName (att.TransferEncoding));
+                       if (sendDisposition)
+                               SendHeader ("content-disposition", att.ContentDisposition.ToString ());
                        SendData (string.Empty);
                }
-
+               
                // use proper encoding to escape input
                private string ToQuotedPrintable (string input, Encoding enc)
                {
@@ -1298,4 +1337,3 @@ try {
        }
 }
 
-#endif // NET_2_0