Merge pull request #1971 from angeloc/master
[mono.git] / mcs / class / System / System.Net.Mail / SmtpClient.cs
1 //
2 // System.Net.Mail.SmtpClient.cs
3 //
4 // Author:
5 //      Tim Coleman (tim@timcoleman.com)
6 //
7 // Copyright (C) Tim Coleman, 2004
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 #if SECURITY_DEP
32
33 #if MONOTOUCH || MONODROID
34 using System.Security.Cryptography.X509Certificates;
35 #else
36 extern alias PrebuiltSystem;
37 using X509CertificateCollection = PrebuiltSystem::System.Security.Cryptography.X509Certificates.X509CertificateCollection;
38 using System.Security.Cryptography.X509Certificates;
39 #endif
40
41 #endif
42
43 using System;
44 using System.Collections.Generic;
45 using System.ComponentModel;
46 using System.Globalization;
47 using System.IO;
48 using System.Net;
49 using System.Net.Mime;
50 using System.Net.Sockets;
51 using System.Text;
52 using System.Threading;
53 using System.Reflection;
54 using System.Net.Configuration;
55 using System.Configuration;
56 using System.Net.Security;
57 using System.Security.Authentication;
58 using System.Threading.Tasks;
59
60 namespace System.Net.Mail {
61         [Obsolete ("SmtpClient and its network of types are poorly designed, we strongly recommend you use https://github.com/jstedfast/MailKit and https://github.com/jstedfast/MimeKit instead")]
62         public class SmtpClient
63         : IDisposable
64         {
65                 #region Fields
66
67                 string host;
68                 int port;
69                 int timeout = 100000;
70                 ICredentialsByHost credentials;
71                 string pickupDirectoryLocation;
72                 SmtpDeliveryMethod deliveryMethod;
73                 bool enableSsl;
74 #if SECURITY_DEP                
75                 X509CertificateCollection clientCertificates;
76 #endif          
77
78                 TcpClient client;
79                 Stream stream;
80                 StreamWriter writer;
81                 StreamReader reader;
82                 int boundaryIndex;
83                 MailAddress defaultFrom;
84
85                 MailMessage messageInProcess;
86
87                 BackgroundWorker worker;
88                 object user_async_state;
89
90                 [Flags]
91                 enum AuthMechs {
92                         None        = 0,
93                         Login       = 0x01,
94                         Plain       = 0x02,
95                 }
96
97                 class CancellationException : Exception
98                 {
99                 }
100
101                 AuthMechs authMechs;
102                 Mutex mutex = new Mutex ();
103
104                 #endregion // Fields
105
106                 #region Constructors
107
108                 public SmtpClient ()
109                         : this (null, 0)
110                 {
111                 }
112
113                 public SmtpClient (string host)
114                         : this (host, 0)
115                 {
116                 }
117
118                 public SmtpClient (string host, int port) {
119 #if CONFIGURATION_DEP
120                         SmtpSection cfg = (SmtpSection) ConfigurationManager.GetSection ("system.net/mailSettings/smtp");
121
122                         if (cfg != null) {
123                                 this.host = cfg.Network.Host;
124                                 this.port = cfg.Network.Port;
125                                 this.enableSsl = cfg.Network.EnableSsl;
126                                 TargetName = cfg.Network.TargetName;
127                                 if (this.TargetName == null)
128                                         TargetName = "SMTPSVC/" + (host != null ? host : "");
129
130                                 
131                                 if (cfg.Network.UserName != null) {
132                                         string password = String.Empty;
133
134                                         if (cfg.Network.Password != null)
135                                                 password = cfg.Network.Password;
136
137                                         Credentials = new CCredentialsByHost (cfg.Network.UserName, password);
138                                 }
139
140                                 if (!String.IsNullOrEmpty (cfg.From))
141                                         defaultFrom = new MailAddress (cfg.From);
142                         }
143 #else
144                         // Just to eliminate the warning, this codepath does not end up in production.
145                         defaultFrom = null;
146 #endif
147
148                         if (!String.IsNullOrEmpty (host))
149                                 this.host = host;
150
151                         if (port != 0)
152                                 this.port = port;
153                         else if (this.port == 0)
154                                 this.port = 25;
155                 }
156
157                 #endregion // Constructors
158
159                 #region Properties
160
161 #if SECURITY_DEP
162                 [MonoTODO("Client certificates not used")]
163                 public X509CertificateCollection ClientCertificates {
164                         get {
165                                 if (clientCertificates == null)
166                                         clientCertificates = new X509CertificateCollection ();
167                                 return clientCertificates;
168                         }
169                 }
170 #endif
171
172                 public
173                 string TargetName { get; set; }
174
175                 public ICredentialsByHost Credentials {
176                         get { return credentials; }
177                         set {
178                                 CheckState ();
179                                 credentials = value;
180                         }
181                 }
182
183                 public SmtpDeliveryMethod DeliveryMethod {
184                         get { return deliveryMethod; }
185                         set {
186                                 CheckState ();
187                                 deliveryMethod = value;
188                         }
189                 }
190
191                 public bool EnableSsl {
192                         get { return enableSsl; }
193                         set {
194                                 CheckState ();
195                                 enableSsl = value;
196                         }
197                 }
198
199                 public string Host {
200                         get { return host; }
201                         set {
202                                 if (value == null)
203                                         throw new ArgumentNullException ("value");
204                                 if (value.Length == 0)
205                                         throw new ArgumentException ("An empty string is not allowed.", "value");
206                                 CheckState ();
207                                 host = value;
208                         }
209                 }
210
211                 public string PickupDirectoryLocation {
212                         get { return pickupDirectoryLocation; }
213                         set { pickupDirectoryLocation = value; }
214                 }
215
216                 public int Port {
217                         get { return port; }
218                         set { 
219                                 if (value <= 0)
220                                         throw new ArgumentOutOfRangeException ("value");
221                                 CheckState ();
222                                 port = value;
223                         }
224                 }
225
226                 [MonoTODO]
227                 public ServicePoint ServicePoint {
228                         get { throw new NotImplementedException (); }
229                 }
230
231                 public int Timeout {
232                         get { return timeout; }
233                         set { 
234                                 if (value < 0)
235                                         throw new ArgumentOutOfRangeException ("value");
236                                 CheckState ();
237                                 timeout = value; 
238                         }
239                 }
240
241                 public bool UseDefaultCredentials {
242                         get { return false; }
243                         [MonoNotSupported ("no DefaultCredential support in Mono")]
244                         set {
245                                 if (value)
246                                         throw new NotImplementedException ("Default credentials are not supported");
247                                 CheckState ();
248                         }
249                 }
250
251                 #endregion // Properties
252
253                 #region Events 
254
255                 public event SendCompletedEventHandler SendCompleted;
256
257                 #endregion // Events 
258
259                 #region Methods
260                 public void Dispose ()
261                 {
262                         Dispose (true);
263                 }
264
265                 [MonoTODO ("Does nothing at the moment.")]
266                 protected virtual void Dispose (bool disposing)
267                 {
268                         // TODO: We should close all the connections and abort any async operations here
269                 }
270                 private void CheckState ()
271                 {
272                         if (messageInProcess != null)
273                                 throw new InvalidOperationException ("Cannot set Timeout while Sending a message");
274                 }
275                 
276                 private static string EncodeAddress(MailAddress address)
277                 {
278                         if (!String.IsNullOrEmpty (address.DisplayName)) {
279                                 string encodedDisplayName = ContentType.EncodeSubjectRFC2047 (address.DisplayName, Encoding.UTF8);
280                                 return "\"" + encodedDisplayName + "\" <" + address.Address + ">";
281                         }
282                         return address.ToString ();
283                 }
284
285                 private static string EncodeAddresses(MailAddressCollection addresses)
286                 {
287                         StringBuilder sb = new StringBuilder();
288                         bool first = true;
289                         foreach (MailAddress address in addresses) {
290                                 if (!first) {
291                                         sb.Append(", ");
292                                 }
293                                 sb.Append(EncodeAddress(address));
294                                 first = false;
295                         }
296                         return sb.ToString();
297                 }
298
299                 private string EncodeSubjectRFC2047 (MailMessage message)
300                 {
301                         return ContentType.EncodeSubjectRFC2047 (message.Subject, message.SubjectEncoding);
302                 }
303
304                 private string EncodeBody (MailMessage message)
305                 {
306                         string body = message.Body;
307                         Encoding encoding = message.BodyEncoding;
308                         // RFC 2045 encoding
309                         switch (message.ContentTransferEncoding) {
310                         case TransferEncoding.SevenBit:
311                                 return body;
312                         case TransferEncoding.Base64:
313                                 return Convert.ToBase64String (encoding.GetBytes (body), Base64FormattingOptions.InsertLineBreaks);
314                         default:
315                                 return ToQuotedPrintable (body, encoding);
316                         }
317                 }
318
319                 private string EncodeBody (AlternateView av)
320                 {
321                         //Encoding encoding = av.ContentType.CharSet != null ? Encoding.GetEncoding (av.ContentType.CharSet) : Encoding.UTF8;
322
323                         byte [] bytes = new byte [av.ContentStream.Length];
324                         av.ContentStream.Read (bytes, 0, bytes.Length);
325
326                         // RFC 2045 encoding
327                         switch (av.TransferEncoding) {
328                         case TransferEncoding.SevenBit:
329                                 return Encoding.ASCII.GetString (bytes);
330                         case TransferEncoding.Base64:
331                                 return Convert.ToBase64String (bytes, Base64FormattingOptions.InsertLineBreaks);
332                         default:
333                                 return ToQuotedPrintable (bytes);
334                         }
335                 }
336
337
338                 private void EndSection (string section)
339                 {
340                         SendData (String.Format ("--{0}--", section));
341                         SendData (string.Empty);
342                 }
343
344                 private string GenerateBoundary ()
345                 {
346                         string output = GenerateBoundary (boundaryIndex);
347                         boundaryIndex += 1;
348                         return output;
349                 }
350
351                 private static string GenerateBoundary (int index)
352                 {
353                         return String.Format ("--boundary_{0}_{1}", index, Guid.NewGuid ().ToString ("D"));
354                 }
355
356                 private bool IsError (SmtpResponse status)
357                 {
358                         return ((int) status.StatusCode) >= 400;
359                 }
360
361                 protected void OnSendCompleted (AsyncCompletedEventArgs e)
362                 {
363                         try {
364                                 if (SendCompleted != null)
365                                         SendCompleted (this, e);
366                         } finally {
367                                 worker = null;
368                                 user_async_state = null;
369                         }
370                 }
371
372                 private void CheckCancellation ()
373                 {
374                         if (worker != null && worker.CancellationPending)
375                                 throw new CancellationException ();
376                 }
377
378                 private SmtpResponse Read () {
379                         byte [] buffer = new byte [512];
380                         int position = 0;
381                         bool lastLine = false;
382
383                         do {
384                                 CheckCancellation ();
385
386                                 int readLength = stream.Read (buffer, position, buffer.Length - position);
387                                 if (readLength > 0) {
388                                         int available = position + readLength - 1;
389                                         if (available > 4 && (buffer [available] == '\n' || buffer [available] == '\r'))
390                                                 for (int index = available - 3; ; index--) {
391                                                         if (index < 0 || buffer [index] == '\n' || buffer [index] == '\r') {
392                                                                 lastLine = buffer [index + 4] == ' ';
393                                                                 break;
394                                                         }
395                                                 }
396
397                                         // move position
398                                         position += readLength;
399
400                                         // check if buffer is full
401                                         if (position == buffer.Length) {
402                                                 byte [] newBuffer = new byte [buffer.Length * 2];
403                                                 Array.Copy (buffer, 0, newBuffer, 0, buffer.Length);
404                                                 buffer = newBuffer;
405                                         }
406                                 }
407                                 else {
408                                         break;
409                                 }
410                         } while (!lastLine);
411
412                         if (position > 0) {
413                                 Encoding encoding = new ASCIIEncoding ();
414
415                                 string line = encoding.GetString (buffer, 0, position - 1);
416
417                                 // parse the line to the lastResponse object
418                                 SmtpResponse response = SmtpResponse.Parse (line);
419
420                                 return response;
421                         } else {
422                                 throw new System.IO.IOException ("Connection closed");
423                         }
424                 }
425
426                 void ResetExtensions()
427                 {
428                         authMechs = AuthMechs.None;
429                 }
430
431                 void ParseExtensions (string extens)
432                 {
433                         string[] parts = extens.Split ('\n');
434
435                         foreach (string part in parts) {
436                                 if (part.Length < 4)
437                                         continue;
438
439                                 string start = part.Substring (4);
440                                 if (start.StartsWith ("AUTH ", StringComparison.Ordinal)) {
441                                         string[] options = start.Split (' ');
442                                         for (int k = 1; k < options.Length; k++) {
443                                                 string option = options[k].Trim();
444                                                 // GSSAPI, KERBEROS_V4, NTLM not supported
445                                                 switch (option) {
446                                                 /*
447                                                 case "CRAM-MD5":
448                                                         authMechs |= AuthMechs.CramMD5;
449                                                         break;
450                                                 case "DIGEST-MD5":
451                                                         authMechs |= AuthMechs.DigestMD5;
452                                                         break;
453                                                 */
454                                                 case "LOGIN":
455                                                         authMechs |= AuthMechs.Login;
456                                                         break;
457                                                 case "PLAIN":
458                                                         authMechs |= AuthMechs.Plain;
459                                                         break;
460                                                 }
461                                         }
462                                 }
463                         }
464                 }
465
466                 public void Send (MailMessage message)
467                 {
468                         if (message == null)
469                                 throw new ArgumentNullException ("message");
470
471                         if (deliveryMethod == SmtpDeliveryMethod.Network && (Host == null || Host.Trim ().Length == 0))
472                                 throw new InvalidOperationException ("The SMTP host was not specified");
473                         else if (deliveryMethod == SmtpDeliveryMethod.PickupDirectoryFromIis)
474                                 throw new NotSupportedException("IIS delivery is not supported");
475
476                         if (port == 0)
477                                 port = 25;
478                         
479                         // Block while sending
480                         mutex.WaitOne ();
481                         try {
482                                 messageInProcess = message;
483                                 if (deliveryMethod == SmtpDeliveryMethod.SpecifiedPickupDirectory)
484                                         SendToFile (message);
485                                 else
486                                         SendInternal (message);
487                         } catch (CancellationException) {
488                                 // This exception is introduced for convenient cancellation process.
489                         } catch (SmtpException) {
490                                 throw;
491                         } catch (Exception ex) {
492                                 throw new SmtpException ("Message could not be sent.", ex);
493                         } finally {
494                                 // Release the mutex to allow other threads access
495                                 mutex.ReleaseMutex ();
496                                 messageInProcess = null;
497                         }
498                 }
499
500                 private void SendInternal (MailMessage message)
501                 {
502                         CheckCancellation ();
503
504                         try {
505                                 client = new TcpClient (host, port);
506                                 stream = client.GetStream ();
507                                 // FIXME: this StreamWriter creation is bogus.
508                                 // It expects as if a Stream were able to switch to SSL
509                                 // mode (such behavior is only in Mainsoft Socket API).
510                                 writer = new StreamWriter (stream);
511                                 reader = new StreamReader (stream);
512
513                                 SendCore (message);
514                         } finally {
515                                 if (writer != null)
516                                         writer.Close ();
517                                 if (reader != null)
518                                         reader.Close ();
519                                 if (stream != null)
520                                         stream.Close ();
521                                 if (client != null)
522                                         client.Close ();
523                         }
524                 }
525  
526                 // FIXME: simple implementation, could be brushed up.
527                 private void SendToFile (MailMessage message)
528                 {
529                         if (!Path.IsPathRooted (pickupDirectoryLocation))
530                                 throw new SmtpException("Only absolute directories are allowed for pickup directory.");
531
532                         string filename = Path.Combine (pickupDirectoryLocation,
533                                 Guid.NewGuid() + ".eml");
534
535                         try {
536                                 writer = new StreamWriter(filename);
537
538                                 // FIXME: See how Microsoft fixed the bug about envelope senders, and how it actually represents the info in .eml file headers
539                                 //        For all we know, defaultFrom may be the envelope sender
540                                 // For now, we are no worse than some versions of .NET
541                                 MailAddress from = message.From;
542                                 if (from == null)
543                                         from = defaultFrom;
544
545                                 string dt = DateTime.Now.ToString("ddd, dd MMM yyyy HH':'mm':'ss zzz", DateTimeFormatInfo.InvariantInfo);
546                                 // remove ':' from time zone offset (e.g. from "+01:00")
547                                 dt = dt.Remove(dt.Length - 3, 1);
548                                 SendHeader(HeaderName.Date, dt);
549
550                                 SendHeader (HeaderName.From, EncodeAddress(from));
551                                 SendHeader (HeaderName.To, EncodeAddresses(message.To));
552                                 if (message.CC.Count > 0)
553                                         SendHeader (HeaderName.Cc, EncodeAddresses(message.CC));
554                                 SendHeader (HeaderName.Subject, EncodeSubjectRFC2047 (message));
555
556                                 foreach (string s in message.Headers.AllKeys)
557                                         SendHeader (s, message.Headers [s]);
558
559                                 AddPriorityHeader (message);
560
561                                 boundaryIndex = 0;
562                                 if (message.Attachments.Count > 0)
563                                         SendWithAttachments (message);
564                                 else
565                                         SendWithoutAttachments (message, null, false);
566
567
568                         } finally {
569                                 if (writer != null) writer.Close(); writer = null;
570                         }
571                 }
572
573                 private void SendCore (MailMessage message)
574                 {
575                         SmtpResponse status;
576
577                         status = Read ();
578                         if (IsError (status))
579                                 throw new SmtpException (status.StatusCode, status.Description);
580
581                         // EHLO
582                         
583                         // FIXME: parse the list of extensions so we don't bother wasting
584                         // our time trying commands if they aren't supported.
585                         
586                         // Get the FQDN of the local machine
587                         string fqdn = Dns.GetHostEntry (Dns.GetHostName ()).HostName;
588                         status = SendCommand ("EHLO " + fqdn);
589                         
590                         if (IsError (status)) {
591                                 status = SendCommand ("HELO " + fqdn);
592                                 
593                                 if (IsError (status))
594                                         throw new SmtpException (status.StatusCode, status.Description);
595                         } else {
596                                 // Parse ESMTP extensions
597                                 string extens = status.Description;
598                                 
599                                 if (extens != null)
600                                         ParseExtensions (extens);
601                         }
602                         
603                         if (enableSsl) {
604                                 InitiateSecureConnection ();
605                                 ResetExtensions();
606                                 writer = new StreamWriter (stream);
607                                 reader = new StreamReader (stream);
608                                 status = SendCommand ("EHLO " + fqdn);
609                         
610                                 if (IsError (status)) {
611                                         status = SendCommand ("HELO " + fqdn);
612                                 
613                                         if (IsError (status))
614                                                 throw new SmtpException (status.StatusCode, status.Description);
615                                 } else {
616                                         // Parse ESMTP extensions
617                                         string extens = status.Description;
618                                         if (extens != null)
619                                                 ParseExtensions (extens);
620                                 }
621                         }
622                         
623                         if (authMechs != AuthMechs.None)
624                                 Authenticate ();
625
626                         // The envelope sender: use 'Sender:' in preference of 'From:'
627                         MailAddress sender = message.Sender;
628                         if (sender == null)
629                                 sender = message.From;
630                         if (sender == null)
631                                 sender = defaultFrom;
632                         
633                         // MAIL FROM:
634                         status = SendCommand ("MAIL FROM:<" + sender.Address + '>');
635                         if (IsError (status)) {
636                                 throw new SmtpException (status.StatusCode, status.Description);
637                         }
638
639                         // Send RCPT TO: for all recipients
640                         List<SmtpFailedRecipientException> sfre = new List<SmtpFailedRecipientException> ();
641
642                         for (int i = 0; i < message.To.Count; i ++) {
643                                 status = SendCommand ("RCPT TO:<" + message.To [i].Address + '>');
644                                 if (IsError (status)) 
645                                         sfre.Add (new SmtpFailedRecipientException (status.StatusCode, message.To [i].Address));
646                         }
647                         for (int i = 0; i < message.CC.Count; i ++) {
648                                 status = SendCommand ("RCPT TO:<" + message.CC [i].Address + '>');
649                                 if (IsError (status)) 
650                                         sfre.Add (new SmtpFailedRecipientException (status.StatusCode, message.CC [i].Address));
651                         }
652                         for (int i = 0; i < message.Bcc.Count; i ++) {
653                                 status = SendCommand ("RCPT TO:<" + message.Bcc [i].Address + '>');
654                                 if (IsError (status)) 
655                                         sfre.Add (new SmtpFailedRecipientException (status.StatusCode, message.Bcc [i].Address));
656                         }
657
658                         if (sfre.Count >0)
659                                 throw new SmtpFailedRecipientsException ("failed recipients", sfre.ToArray ());
660
661                         // DATA
662                         status = SendCommand ("DATA");
663                         if (IsError (status))
664                                 throw new SmtpException (status.StatusCode, status.Description);
665
666                         // Send message headers
667                         string dt = DateTime.Now.ToString ("ddd, dd MMM yyyy HH':'mm':'ss zzz", DateTimeFormatInfo.InvariantInfo);
668                         // remove ':' from time zone offset (e.g. from "+01:00")
669                         dt = dt.Remove (dt.Length - 3, 1);
670                         SendHeader (HeaderName.Date, dt);
671
672                         MailAddress from = message.From;
673                         if (from == null)
674                                 from = defaultFrom;
675
676                         SendHeader (HeaderName.From, EncodeAddress (from));
677                         SendHeader (HeaderName.To, EncodeAddresses (message.To));
678                         if (message.CC.Count > 0)
679                                 SendHeader (HeaderName.Cc, EncodeAddresses (message.CC));
680                         SendHeader (HeaderName.Subject, EncodeSubjectRFC2047 (message));
681
682                         string v = "normal";
683                                 
684                         switch (message.Priority){
685                         case MailPriority.Normal:
686                                 v = "normal";
687                                 break;
688                                 
689                         case MailPriority.Low:
690                                 v = "non-urgent";
691                                 break;
692                                 
693                         case MailPriority.High:
694                                 v = "urgent";
695                                 break;
696                         }
697                         SendHeader ("Priority", v);
698                         if (message.Sender != null)
699                                 SendHeader ("Sender", EncodeAddress (message.Sender));
700                         if (message.ReplyToList.Count > 0)
701                                 SendHeader ("Reply-To", EncodeAddresses (message.ReplyToList));
702
703                         foreach (string s in message.Headers.AllKeys)
704                                 SendHeader (s, ContentType.EncodeSubjectRFC2047 (message.Headers [s], message.HeadersEncoding));
705         
706                         AddPriorityHeader (message);
707
708                         boundaryIndex = 0;
709                         if (message.Attachments.Count > 0)
710                                 SendWithAttachments (message);
711                         else
712                                 SendWithoutAttachments (message, null, false);
713
714                         SendDot ();
715
716                         status = Read ();
717                         if (IsError (status))
718                                 throw new SmtpException (status.StatusCode, status.Description);
719
720                         try {
721                                 status = SendCommand ("QUIT");
722                         } catch (System.IO.IOException) {
723                                 // We excuse server for the rude connection closing as a response to QUIT
724                         }
725                 }
726
727                 public void Send (string from, string to, string subject, string body)
728                 {
729                         Send (new MailMessage (from, to, subject, body));
730                 }
731
732                 public Task SendMailAsync (MailMessage message)
733                 {
734                         var tcs = new TaskCompletionSource<object> ();
735                         SendCompletedEventHandler handler = null;
736                         handler = (s, e) => SendMailAsyncCompletedHandler (tcs, e, handler, this);
737                         SendCompleted += handler;
738                         SendAsync (message, tcs);
739                         return tcs.Task;
740                 }
741
742                 public Task SendMailAsync (string from, string recipients, string subject, string body)
743                 {
744                         return SendMailAsync (new MailMessage (from, recipients, subject, body));
745                 }
746
747                 static void SendMailAsyncCompletedHandler (TaskCompletionSource<object> source, AsyncCompletedEventArgs e, SendCompletedEventHandler handler, SmtpClient client)
748                 {
749                         if (source != e.UserState)
750                                 return;
751
752                         client.SendCompleted -= handler;
753
754                         if (e.Error != null) {
755                                 source.SetException (e.Error);
756                                 return;
757                         }
758
759                         if (e.Cancelled) {
760                                 source.SetCanceled ();
761                                 return;
762                         }
763
764                         source.SetResult (null);
765                 }
766
767                 private void SendDot()
768                 {
769                         writer.Write(".\r\n");
770                         writer.Flush();
771                 }
772
773                 private void SendData (string data)
774                 {
775                         if (String.IsNullOrEmpty (data)) {
776                                 writer.Write("\r\n");
777                                 writer.Flush();
778                                 return;
779                         }
780
781                         StringReader sr = new StringReader (data);
782                         string line;
783                         bool escapeDots = deliveryMethod == SmtpDeliveryMethod.Network;
784                         while ((line = sr.ReadLine ()) != null) {
785                                 CheckCancellation ();
786
787                                 if (escapeDots) {
788                                         int i;
789                                         for (i = 0; i < line.Length; i++) {
790                                                 if (line[i] != '.')
791                                                         break;
792                                         }
793                                         if (i > 0 && i == line.Length) {
794                                                 line += ".";
795                                         }
796                                 }
797                                 writer.Write (line);
798                                 writer.Write ("\r\n");
799                         }
800                         writer.Flush ();
801                 }
802
803                 public void SendAsync (MailMessage message, object userToken)
804                 {
805                         if (worker != null)
806                                 throw new InvalidOperationException ("Another SendAsync operation is in progress");
807
808                         worker = new BackgroundWorker ();
809                         worker.DoWork += delegate (object o, DoWorkEventArgs ea) {
810                                 try {
811                                         user_async_state = ea.Argument;
812                                         Send (message);
813                                 } catch (Exception ex) {
814                                         ea.Result = ex;
815                                         throw ex;
816                                 }
817                         };
818                         worker.WorkerSupportsCancellation = true;
819                         worker.RunWorkerCompleted += delegate (object o, RunWorkerCompletedEventArgs ea) {
820                                 // Note that RunWorkerCompletedEventArgs.UserState cannot be used (LAMESPEC)
821                                 OnSendCompleted (new AsyncCompletedEventArgs (ea.Error, ea.Cancelled, user_async_state));
822                         };
823                         worker.RunWorkerAsync (userToken);
824                 }
825
826                 public void SendAsync (string from, string to, string subject, string body, object userToken)
827                 {
828                         SendAsync (new MailMessage (from, to, subject, body), userToken);
829                 }
830
831                 public void SendAsyncCancel ()
832                 {
833                         if (worker == null)
834                                 throw new InvalidOperationException ("SendAsync operation is not in progress");
835                         worker.CancelAsync ();
836                 }
837
838                 private void AddPriorityHeader (MailMessage message) {
839                         switch (message.Priority) {
840                         case MailPriority.High:
841                                 SendHeader (HeaderName.Priority, "Urgent");
842                                 SendHeader (HeaderName.Importance, "high");
843                                 SendHeader (HeaderName.XPriority, "1");
844                                 break;
845                         case MailPriority.Low:
846                                 SendHeader (HeaderName.Priority, "Non-Urgent");
847                                 SendHeader (HeaderName.Importance, "low");
848                                 SendHeader (HeaderName.XPriority, "5");
849                                 break;
850                         }
851                 }
852
853                 private void SendSimpleBody (MailMessage message) {
854                         SendHeader (HeaderName.ContentType, message.BodyContentType.ToString ());
855                         if (message.ContentTransferEncoding != TransferEncoding.SevenBit)
856                                 SendHeader (HeaderName.ContentTransferEncoding, GetTransferEncodingName (message.ContentTransferEncoding));
857                         SendData (string.Empty);
858
859                         SendData (EncodeBody (message));
860                 }
861
862                 private void SendBodylessSingleAlternate (AlternateView av) {
863                         SendHeader (HeaderName.ContentType, av.ContentType.ToString ());
864                         if (av.TransferEncoding != TransferEncoding.SevenBit)
865                                 SendHeader (HeaderName.ContentTransferEncoding, GetTransferEncodingName (av.TransferEncoding));
866                         SendData (string.Empty);
867
868                         SendData (EncodeBody (av));
869                 }
870
871                 private void SendWithoutAttachments (MailMessage message, string boundary, bool attachmentExists)
872                 {
873                         if (message.Body == null && message.AlternateViews.Count == 1)
874                                 SendBodylessSingleAlternate (message.AlternateViews [0]);
875                         else if (message.AlternateViews.Count > 0)
876                                 SendBodyWithAlternateViews (message, boundary, attachmentExists);
877                         else
878                                 SendSimpleBody (message);
879                 }
880
881
882                 private void SendWithAttachments (MailMessage message) {
883                         string boundary = GenerateBoundary ();
884
885                         // first "multipart/mixed"
886                         ContentType messageContentType = new ContentType ();
887                         messageContentType.Boundary = boundary;
888                         messageContentType.MediaType = "multipart/mixed";
889                         messageContentType.CharSet = null;
890
891                         SendHeader (HeaderName.ContentType, messageContentType.ToString ());
892                         SendData (String.Empty);
893
894                         // body section
895                         Attachment body = null;
896
897                         if (message.AlternateViews.Count > 0)
898                                 SendWithoutAttachments (message, boundary, true);
899                         else {
900                                 body = Attachment.CreateAttachmentFromString (message.Body, null, message.BodyEncoding, message.IsBodyHtml ? "text/html" : "text/plain");
901                                 message.Attachments.Insert (0, body);
902                         }
903
904                         try {
905                                 SendAttachments (message, body, boundary);
906                         } finally {
907                                 if (body != null)
908                                         message.Attachments.Remove (body);
909                         }
910
911                         EndSection (boundary);
912                 }
913
914                 private void SendBodyWithAlternateViews (MailMessage message, string boundary, bool attachmentExists)
915                 {
916                         AlternateViewCollection alternateViews = message.AlternateViews;
917
918                         string inner_boundary = GenerateBoundary ();
919
920                         ContentType messageContentType = new ContentType ();
921                         messageContentType.Boundary = inner_boundary;
922                         messageContentType.MediaType = "multipart/alternative";
923
924                         if (!attachmentExists) {
925                                 SendHeader (HeaderName.ContentType, messageContentType.ToString ());
926                                 SendData (String.Empty);
927                         }
928
929                         // body section
930                         AlternateView body = null;
931                         if (message.Body != null) {
932                                 body = AlternateView.CreateAlternateViewFromString (message.Body, message.BodyEncoding, message.IsBodyHtml ? "text/html" : "text/plain");
933                                 alternateViews.Insert (0, body);
934                                 StartSection (boundary, messageContentType);
935                         }
936
937 try {
938                         // alternate view sections
939                         foreach (AlternateView av in alternateViews) {
940
941                                 string alt_boundary = null;
942                                 ContentType contentType;
943                                 if (av.LinkedResources.Count > 0) {
944                                         alt_boundary = GenerateBoundary ();
945                                         contentType = new ContentType ("multipart/related");
946                                         contentType.Boundary = alt_boundary;
947                                         
948                                         contentType.Parameters ["type"] = av.ContentType.ToString ();
949                                         StartSection (inner_boundary, contentType);
950                                         StartSection (alt_boundary, av.ContentType, av);
951                                 } else {
952                                         contentType = new ContentType (av.ContentType.ToString ());
953                                         StartSection (inner_boundary, contentType, av);
954                                 }
955
956                                 switch (av.TransferEncoding) {
957                                 case TransferEncoding.Base64:
958                                         byte [] content = new byte [av.ContentStream.Length];
959                                         av.ContentStream.Read (content, 0, content.Length);
960                                             SendData (Convert.ToBase64String (content, Base64FormattingOptions.InsertLineBreaks));
961                                         break;
962                                 case TransferEncoding.QuotedPrintable:
963                                         byte [] bytes = new byte [av.ContentStream.Length];
964                                         av.ContentStream.Read (bytes, 0, bytes.Length);
965                                         SendData (ToQuotedPrintable (bytes));
966                                         break;
967                                 case TransferEncoding.SevenBit:
968                                 case TransferEncoding.Unknown:
969                                         content = new byte [av.ContentStream.Length];
970                                         av.ContentStream.Read (content, 0, content.Length);
971                                         SendData (Encoding.ASCII.GetString (content));
972                                         break;
973                                 }
974
975                                 if (av.LinkedResources.Count > 0) {
976                                         SendLinkedResources (message, av.LinkedResources, alt_boundary);
977                                         EndSection (alt_boundary);
978                                 }
979
980                                 if (!attachmentExists)
981                                         SendData (string.Empty);
982                         }
983
984 } finally {
985                         if (body != null)
986                                 alternateViews.Remove (body);
987 }
988                         EndSection (inner_boundary);
989                 }
990
991                 private void SendLinkedResources (MailMessage message, LinkedResourceCollection resources, string boundary)
992                 {
993                         foreach (LinkedResource lr in resources) {
994                                 StartSection (boundary, lr.ContentType, lr);
995
996                                 switch (lr.TransferEncoding) {
997                                 case TransferEncoding.Base64:
998                                         byte [] content = new byte [lr.ContentStream.Length];
999                                         lr.ContentStream.Read (content, 0, content.Length);
1000                                             SendData (Convert.ToBase64String (content, Base64FormattingOptions.InsertLineBreaks));
1001                                         break;
1002                                 case TransferEncoding.QuotedPrintable:
1003                                         byte [] bytes = new byte [lr.ContentStream.Length];
1004                                         lr.ContentStream.Read (bytes, 0, bytes.Length);
1005                                         SendData (ToQuotedPrintable (bytes));
1006                                         break;
1007                                 case TransferEncoding.SevenBit:
1008                                 case TransferEncoding.Unknown:
1009                                         content = new byte [lr.ContentStream.Length];
1010                                         lr.ContentStream.Read (content, 0, content.Length);
1011                                         SendData (Encoding.ASCII.GetString (content));
1012                                         break;
1013                                 }
1014                         }
1015                 }
1016
1017                 private void SendAttachments (MailMessage message, Attachment body, string boundary) {
1018                         foreach (Attachment att in message.Attachments) {
1019                                 ContentType contentType = new ContentType (att.ContentType.ToString ());
1020                                 if (att.Name != null) {
1021                                         contentType.Name = att.Name;
1022                                         if (att.NameEncoding != null)
1023                                                 contentType.CharSet = att.NameEncoding.HeaderName;
1024                                         att.ContentDisposition.FileName = att.Name;
1025                                 }
1026                                 StartSection (boundary, contentType, att, att != body);
1027
1028                                 byte [] content = new byte [att.ContentStream.Length];
1029                                 att.ContentStream.Read (content, 0, content.Length);
1030                                 switch (att.TransferEncoding) {
1031                                 case TransferEncoding.Base64:
1032                                         SendData (Convert.ToBase64String (content, Base64FormattingOptions.InsertLineBreaks));
1033                                         break;
1034                                 case TransferEncoding.QuotedPrintable:
1035                                         SendData (ToQuotedPrintable (content));
1036                                         break;
1037                                 case TransferEncoding.SevenBit:
1038                                 case TransferEncoding.Unknown:
1039                                         SendData (Encoding.ASCII.GetString (content));
1040                                         break;
1041                                 }
1042
1043                                 SendData (string.Empty);
1044                         }
1045                 }
1046
1047                 private SmtpResponse SendCommand (string command)
1048                 {
1049                         writer.Write (command);
1050                         // Certain SMTP servers will reject mail sent with unix line-endings; see http://cr.yp.to/docs/smtplf.html
1051                         writer.Write ("\r\n");
1052                         writer.Flush ();
1053                         return Read ();
1054                 }
1055
1056                 private void SendHeader (string name, string value)
1057                 {
1058                         SendData (String.Format ("{0}: {1}", name, value));
1059                 }
1060
1061                 private void StartSection (string section, ContentType sectionContentType)
1062                 {
1063                         SendData (String.Format ("--{0}", section));
1064                         SendHeader ("content-type", sectionContentType.ToString ());
1065                         SendData (string.Empty);
1066                 }
1067
1068                 private void StartSection (string section, ContentType sectionContentType, AttachmentBase att)
1069                 {
1070                         SendData (String.Format ("--{0}", section));
1071                         SendHeader ("content-type", sectionContentType.ToString ());
1072                         SendHeader ("content-transfer-encoding", GetTransferEncodingName (att.TransferEncoding));
1073                         if (!string.IsNullOrEmpty (att.ContentId))
1074                                 SendHeader("content-ID", "<" + att.ContentId + ">");
1075                         SendData (string.Empty);
1076                 }
1077
1078                 private void StartSection (string section, ContentType sectionContentType, Attachment att, bool sendDisposition) {
1079                         SendData (String.Format ("--{0}", section));
1080                         if (!string.IsNullOrEmpty (att.ContentId))
1081                                 SendHeader("content-ID", "<" + att.ContentId + ">");
1082                         SendHeader ("content-type", sectionContentType.ToString ());
1083                         SendHeader ("content-transfer-encoding", GetTransferEncodingName (att.TransferEncoding));
1084                         if (sendDisposition)
1085                                 SendHeader ("content-disposition", att.ContentDisposition.ToString ());
1086                         SendData (string.Empty);
1087                 }
1088                 
1089                 // use proper encoding to escape input
1090                 private string ToQuotedPrintable (string input, Encoding enc)
1091                 {
1092                         byte [] bytes = enc.GetBytes (input);
1093                         return ToQuotedPrintable (bytes);
1094                 }
1095
1096                 private string ToQuotedPrintable (byte [] bytes)
1097                 {
1098                         StringWriter writer = new StringWriter ();
1099                         int charsInLine = 0;
1100                         int curLen;
1101                         StringBuilder sb = new StringBuilder("=", 3);
1102                         byte equalSign = (byte)'=';
1103                         char c = (char)0;
1104
1105                         foreach (byte i in bytes) {
1106                                 if (i > 127 || i == equalSign) {
1107                                         sb.Length = 1;
1108                                         sb.Append(Convert.ToString (i, 16).ToUpperInvariant ());
1109                                         curLen = 3;
1110                                 } else {
1111                                         c = Convert.ToChar (i);
1112                                         if (c == '\r' || c == '\n') {
1113                                                 writer.Write (c);
1114                                                 charsInLine = 0;
1115                                                 continue;
1116                                         }
1117                                         curLen = 1;
1118                                 }
1119                                 
1120                                 charsInLine += curLen;
1121                                 if (charsInLine > 75) {
1122                                         writer.Write ("=\r\n");
1123                                         charsInLine = curLen;
1124                                 }
1125                                 if (curLen == 1)
1126                                         writer.Write (c);
1127                                 else
1128                                         writer.Write (sb.ToString ());
1129                         }
1130
1131                         return writer.ToString ();
1132                 }
1133                 private static string GetTransferEncodingName (TransferEncoding encoding)
1134                 {
1135                         switch (encoding) {
1136                         case TransferEncoding.QuotedPrintable:
1137                                 return "quoted-printable";
1138                         case TransferEncoding.SevenBit:
1139                                 return "7bit";
1140                         case TransferEncoding.Base64:
1141                                 return "base64";
1142                         }
1143                         return "unknown";
1144                 }
1145
1146 #if SECURITY_DEP
1147                 RemoteCertificateValidationCallback callback = delegate (object sender,
1148                                                                          X509Certificate certificate,
1149                                                                          X509Chain chain,
1150                                                                          SslPolicyErrors sslPolicyErrors) {
1151                         // honor any exciting callback defined on ServicePointManager
1152                         if (ServicePointManager.ServerCertificateValidationCallback != null)
1153                                 return ServicePointManager.ServerCertificateValidationCallback (sender, certificate, chain, sslPolicyErrors);
1154                         // otherwise provide our own
1155                         if (sslPolicyErrors != SslPolicyErrors.None)
1156                                 throw new InvalidOperationException ("SSL authentication error: " + sslPolicyErrors);
1157                         return true;
1158                         };
1159 #endif
1160
1161                 private void InitiateSecureConnection () {
1162                         SmtpResponse response = SendCommand ("STARTTLS");
1163
1164                         if (IsError (response)) {
1165                                 throw new SmtpException (SmtpStatusCode.GeneralFailure, "Server does not support secure connections.");
1166                         }
1167
1168 #if   SECURITY_DEP
1169                         SslStream sslStream = new SslStream (stream, false, callback, null);
1170                         CheckCancellation ();
1171                         sslStream.AuthenticateAsClient (Host, this.ClientCertificates, SslProtocols.Default, false);
1172                         stream = sslStream;
1173
1174 #else
1175                         throw new SystemException ("You are using an incomplete System.dll build");
1176 #endif
1177                 }
1178                 
1179                 void Authenticate ()
1180                 {
1181                         string user = null, pass = null;
1182                         
1183                         if (UseDefaultCredentials) {
1184                                 user = CredentialCache.DefaultCredentials.GetCredential (new System.Uri ("smtp://" + host), "basic").UserName;
1185                                 pass =  CredentialCache.DefaultCredentials.GetCredential (new System.Uri ("smtp://" + host), "basic").Password;
1186                         } else if (Credentials != null) {
1187                                 user = Credentials.GetCredential (host, port, "smtp").UserName;
1188                                 pass = Credentials.GetCredential (host, port, "smtp").Password;
1189                         } else {
1190                                 return;
1191                         }
1192                         
1193                         Authenticate (user, pass);
1194                 }
1195
1196                 void CheckStatus (SmtpResponse status, int i)
1197                 {
1198                         if (((int) status.StatusCode) != i)
1199                                 throw new SmtpException (status.StatusCode, status.Description);
1200                 }
1201
1202                 void ThrowIfError (SmtpResponse status)
1203                 {
1204                         if (IsError (status))
1205                                 throw new SmtpException (status.StatusCode, status.Description);
1206                 }
1207
1208                 void Authenticate (string user, string password)
1209                 {
1210                         if (authMechs == AuthMechs.None)
1211                                 return;
1212
1213                         SmtpResponse status;
1214                         /*
1215                         if ((authMechs & AuthMechs.DigestMD5) != 0) {
1216                                 status = SendCommand ("AUTH DIGEST-MD5");
1217                                 CheckStatus (status, 334);
1218                                 string challenge = Encoding.ASCII.GetString (Convert.FromBase64String (status.Description.Substring (4)));
1219                                 Console.WriteLine ("CHALLENGE: {0}", challenge);
1220                                 DigestSession session = new DigestSession ();
1221                                 session.Parse (false, challenge);
1222                                 string response = session.Authenticate (this, user, password);
1223                                 status = SendCommand (Convert.ToBase64String (Encoding.UTF8.GetBytes (response)));
1224                                 CheckStatus (status, 235);
1225                         } else */
1226                         if ((authMechs & AuthMechs.Login) != 0) {
1227                                 status = SendCommand ("AUTH LOGIN");
1228                                 CheckStatus (status, 334);
1229                                 status = SendCommand (Convert.ToBase64String (Encoding.UTF8.GetBytes (user)));
1230                                 CheckStatus (status, 334);
1231                                 status = SendCommand (Convert.ToBase64String (Encoding.UTF8.GetBytes (password)));
1232                                 CheckStatus (status, 235);
1233                         } else if ((authMechs & AuthMechs.Plain) != 0) {
1234                                 string s = String.Format ("\0{0}\0{1}", user, password);
1235                                 s = Convert.ToBase64String (Encoding.UTF8.GetBytes (s));
1236                                 status = SendCommand ("AUTH PLAIN " + s);
1237                                 CheckStatus (status, 235);
1238                         } else {
1239                                 throw new SmtpException ("AUTH types PLAIN, LOGIN not supported by the server");
1240                         }
1241                 }
1242
1243                 #endregion // Methods
1244                 
1245                 // The HeaderName struct is used to store constant string values representing mail headers.
1246                 private struct HeaderName {
1247                         public const string ContentTransferEncoding = "Content-Transfer-Encoding";
1248                         public const string ContentType = "Content-Type";
1249                         public const string Bcc = "Bcc";
1250                         public const string Cc = "Cc";
1251                         public const string From = "From";
1252                         public const string Subject = "Subject";
1253                         public const string To = "To";
1254                         public const string MimeVersion = "MIME-Version";
1255                         public const string MessageId = "Message-ID";
1256                         public const string Priority = "Priority";
1257                         public const string Importance = "Importance";
1258                         public const string XPriority = "X-Priority";
1259                         public const string Date = "Date";
1260                 }
1261
1262                 // This object encapsulates the status code and description of an SMTP response.
1263                 private struct SmtpResponse {
1264                         public SmtpStatusCode StatusCode;
1265                         public string Description;
1266
1267                         public static SmtpResponse Parse (string line) {
1268                                 SmtpResponse response = new SmtpResponse ();
1269
1270                                 if (line.Length < 4)
1271                                         throw new SmtpException ("Response is to short " +
1272                                                                  line.Length + ".");
1273
1274                                 if ((line [3] != ' ') && (line [3] != '-'))
1275                                         throw new SmtpException ("Response format is wrong.(" +
1276                                                                  line + ")");
1277
1278                                 // parse the response code
1279                                 response.StatusCode = (SmtpStatusCode) Int32.Parse (line.Substring (0, 3));
1280
1281                                 // set the raw response
1282                                 response.Description = line;
1283
1284                                 return response;
1285                         }
1286                 }
1287         }
1288
1289         class CCredentialsByHost : ICredentialsByHost
1290         {
1291                 public CCredentialsByHost (string userName, string password) {
1292                         this.userName = userName;
1293                         this.password = password;
1294                 }
1295
1296                 public NetworkCredential GetCredential (string host, int port, string authenticationType) {
1297                         return new NetworkCredential (userName, password);
1298                 }
1299
1300                 private string userName;
1301                 private string password;
1302         }
1303 }
1304