Merge pull request #995 from yjoly/master
[mono.git] / mcs / class / System / System.Net.Mail / SmtpClient.cs
index b151571620f12969ebab482469964ec5b285f772..c6abb68630e6eb9f60d043b808294012f0849c93 100644 (file)
 
 #if SECURITY_DEP
 
-#if MONOTOUCH
+#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
@@ -47,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;
@@ -55,6 +55,9 @@ using System.Net.Configuration;
 using System.Configuration;
 using System.Net.Security;
 using System.Security.Authentication;
+#if NET_4_5
+using System.Threading.Tasks;
+#endif
 
 namespace System.Net.Mail {
        public class SmtpClient
@@ -745,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");