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