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