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