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