2008-11-17 Gonzalo Paniagua Javier <gonzalo@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.ReplyTo != null)
673                                 SendHeader ("ReplyTo", EncodeAddress (message.ReplyTo));
674                         
675                         foreach (string s in message.Headers.AllKeys)
676                                 SendHeader (s, message.Headers [s]);
677
678                         AddPriorityHeader (message);
679
680                         boundaryIndex = 0;
681                         if (message.Attachments.Count > 0)
682                                 SendWithAttachments (message);
683                         else
684                                 SendWithoutAttachments (message, null, false);
685
686                         SendData (".");
687
688                         status = Read ();
689                         if (IsError (status))
690                                 throw new SmtpException (status.StatusCode, status.Description);
691
692                         try {
693                                 status = SendCommand ("QUIT");
694                         } catch (System.IO.IOException) {
695                                 // We excuse server for the rude connection closing as a response to QUIT
696                         }
697                 }
698
699                 public void Send (string from, string to, string subject, string body)
700                 {
701                         Send (new MailMessage (from, to, subject, body));
702                 }
703
704                 private void SendData (string data)
705                 {
706                         CheckCancellation ();
707
708                         // Certain SMTP servers will reject mail sent with unix line-endings; see http://cr.yp.to/docs/smtplf.html
709                         StringBuilder sb = new StringBuilder (data);
710                         sb.Replace ("\r\n", "\n");
711                         sb.Replace ('\r', '\n');
712                         sb.Replace ("\n", "\r\n");
713                         writer.Write (sb.ToString ());
714                         writer.Write ("\r\n");
715                         writer.Flush ();
716                 }
717
718                 public void SendAsync (MailMessage message, object userToken)
719                 {
720                         if (worker != null)
721                                 throw new InvalidOperationException ("Another SendAsync operation is in progress");
722
723                         worker = new BackgroundWorker ();
724                         worker.DoWork += delegate (object o, DoWorkEventArgs ea) {
725                                 try {
726                                         user_async_state = ea.Argument;
727                                         Send (message);
728                                 } catch (Exception ex) {
729                                         ea.Result = ex;
730                                 }
731                         };
732                         worker.WorkerSupportsCancellation = true;
733                         worker.RunWorkerCompleted += delegate (object o, RunWorkerCompletedEventArgs ea) {
734                                 // Note that RunWorkerCompletedEventArgs.UserState cannot be used (LAMESPEC)
735                                 OnSendCompleted (new AsyncCompletedEventArgs (ea.Error, ea.Cancelled, user_async_state));
736                         };
737                         worker.RunWorkerAsync (userToken);
738                 }
739
740                 public void SendAsync (string from, string to, string subject, string body, object userToken)
741                 {
742                         SendAsync (new MailMessage (from, to, subject, body), userToken);
743                 }
744
745                 public void SendAsyncCancel ()
746                 {
747                         if (worker == null)
748                                 throw new InvalidOperationException ("SendAsync operation is not in progress");
749                         worker.CancelAsync ();
750                 }
751
752                 private void AddPriorityHeader (MailMessage message) {
753                         switch (message.Priority) {
754                         case MailPriority.High:
755                                 SendHeader (HeaderName.Priority, "Urgent");
756                                 SendHeader (HeaderName.Importance, "high");
757                                 SendHeader (HeaderName.XPriority, "1");
758                                 break;
759                         case MailPriority.Low:
760                                 SendHeader (HeaderName.Priority, "Non-Urgent");
761                                 SendHeader (HeaderName.Importance, "low");
762                                 SendHeader (HeaderName.XPriority, "5");
763                                 break;
764                         }
765                 }
766
767                 private void SendSimpleBody (MailMessage message) {
768                         SendHeader (HeaderName.ContentType, message.BodyContentType.ToString ());
769                         if (message.ContentTransferEncoding != TransferEncoding.SevenBit)
770                                 SendHeader (HeaderName.ContentTransferEncoding, GetTransferEncodingName (message.ContentTransferEncoding));
771                         SendData (string.Empty);
772
773                         SendData (EncodeBody (message));
774                 }
775
776                 private void SendBodylessSingleAlternate (AlternateView av) {
777                         SendHeader (HeaderName.ContentType, av.ContentType.ToString ());
778                         if (av.TransferEncoding != TransferEncoding.SevenBit)
779                                 SendHeader (HeaderName.ContentTransferEncoding, GetTransferEncodingName (av.TransferEncoding));
780                         SendData (string.Empty);
781
782                         SendData (EncodeBody (av));
783                 }
784
785                 private void SendWithoutAttachments (MailMessage message, string boundary, bool attachmentExists)
786                 {
787                         if (message.Body == null && message.AlternateViews.Count == 1)
788                                 SendBodylessSingleAlternate (message.AlternateViews [0]);
789                         else if (message.AlternateViews.Count > 0)
790                                 SendBodyWithAlternateViews (message, boundary, attachmentExists);
791                         else
792                                 SendSimpleBody (message);
793                 }
794
795
796                 private void SendWithAttachments (MailMessage message) {
797                         string boundary = GenerateBoundary ();
798
799                         // first "multipart/mixed"
800                         ContentType messageContentType = new ContentType ();
801                         messageContentType.Boundary = boundary;
802                         messageContentType.MediaType = "multipart/mixed";
803                         messageContentType.CharSet = null;
804
805                         SendHeader (HeaderName.ContentType, messageContentType.ToString ());
806                         SendData (String.Empty);
807
808                         // body section
809                         Attachment body = null;
810
811                         if (message.AlternateViews.Count > 0)
812                                 SendWithoutAttachments (message, boundary, true);
813                         else {
814                                 body = Attachment.CreateAttachmentFromString (message.Body, null, message.BodyEncoding, message.IsBodyHtml ? "text/html" : "text/plain");
815                                 message.Attachments.Insert (0, body);
816                         }
817
818                         try {
819                                 SendAttachments (message, body, boundary);
820                         } finally {
821                                 if (body != null)
822                                         message.Attachments.Remove (body);
823                         }
824
825                         EndSection (boundary);
826                 }
827
828                 private void SendBodyWithAlternateViews (MailMessage message, string boundary, bool attachmentExists)
829                 {
830                         AlternateViewCollection alternateViews = message.AlternateViews;
831
832                         string inner_boundary = GenerateBoundary ();
833
834                         ContentType messageContentType = new ContentType ();
835                         messageContentType.Boundary = inner_boundary;
836                         messageContentType.MediaType = "multipart/alternative";
837
838                         if (!attachmentExists) {
839                                 SendHeader (HeaderName.ContentType, messageContentType.ToString ());
840                                 SendData (String.Empty);
841                         }
842
843                         // body section
844                         AlternateView body = null;
845                         if (message.Body != null) {
846                                 body = AlternateView.CreateAlternateViewFromString (message.Body, message.BodyEncoding, message.IsBodyHtml ? "text/html" : "text/plain");
847                                 alternateViews.Insert (0, body);
848                                 StartSection (boundary, messageContentType);
849                         }
850
851 try {
852                         // alternate view sections
853                         foreach (AlternateView av in alternateViews) {
854
855                                 string alt_boundary = null;
856                                 ContentType contentType;
857                                 if (av.LinkedResources.Count > 0) {
858                                         alt_boundary = GenerateBoundary ();
859                                         contentType = new ContentType ("multipart/related");
860                                         contentType.Boundary = alt_boundary;
861                                         contentType.Parameters ["type"] = "application/octet-stream";
862                                         StartSection (inner_boundary, contentType);
863                                 } else {
864                                         contentType = new ContentType (av.ContentType.ToString ());
865                                         StartSection (inner_boundary, contentType, av.TransferEncoding);
866                                 }
867
868                                 switch (av.TransferEncoding) {
869                                 case TransferEncoding.Base64:
870                                         byte [] content = new byte [av.ContentStream.Length];
871                                         av.ContentStream.Read (content, 0, content.Length);
872 #if TARGET_JVM
873                                         SendData (Convert.ToBase64String (content));
874 #else
875                                             SendData (Convert.ToBase64String (content, Base64FormattingOptions.InsertLineBreaks));
876 #endif
877                                         break;
878                                 case TransferEncoding.QuotedPrintable:
879                                         byte [] bytes = new byte [av.ContentStream.Length];
880                                         av.ContentStream.Read (bytes, 0, bytes.Length);
881                                         SendData (ToQuotedPrintable (bytes));
882                                         break;
883                                 case TransferEncoding.SevenBit:
884                                 case TransferEncoding.Unknown:
885                                         content = new byte [av.ContentStream.Length];
886                                         av.ContentStream.Read (content, 0, content.Length);
887                                         SendData (Encoding.ASCII.GetString (content));
888                                         break;
889                                 }
890
891                                 if (av.LinkedResources.Count > 0) {
892                                         SendLinkedResources (message, av.LinkedResources, alt_boundary);
893                                         EndSection (alt_boundary);
894                                 }
895
896                                 if (!attachmentExists)
897                                         SendData (string.Empty);
898                         }
899
900 } finally {
901                         if (body != null)
902                                 alternateViews.Remove (body);
903 }
904                         EndSection (inner_boundary);
905                 }
906
907                 private void SendLinkedResources (MailMessage message, LinkedResourceCollection resources, string boundary)
908                 {
909                         foreach (LinkedResource lr in resources) {
910                                 ContentType contentType = new ContentType (lr.ContentType.ToString ());
911                                 StartSection (boundary, contentType, lr.TransferEncoding);
912
913                                 switch (lr.TransferEncoding) {
914                                 case TransferEncoding.Base64:
915                                         byte [] content = new byte [lr.ContentStream.Length];
916                                         lr.ContentStream.Read (content, 0, content.Length);
917 #if TARGET_JVM
918                                         SendData (Convert.ToBase64String (content));
919 #else
920                                             SendData (Convert.ToBase64String (content, Base64FormattingOptions.InsertLineBreaks));
921 #endif
922                                         break;
923                                 case TransferEncoding.QuotedPrintable:
924                                         byte [] bytes = new byte [lr.ContentStream.Length];
925                                         lr.ContentStream.Read (bytes, 0, bytes.Length);
926                                         SendData (ToQuotedPrintable (bytes));
927                                         break;
928                                 case TransferEncoding.SevenBit:
929                                 case TransferEncoding.Unknown:
930                                         content = new byte [lr.ContentStream.Length];
931                                         lr.ContentStream.Read (content, 0, content.Length);
932                                         SendData (Encoding.ASCII.GetString (content));
933                                         break;
934                                 }
935                         }
936                 }
937
938                 private void SendAttachments (MailMessage message, Attachment body, string boundary) {
939                         foreach (Attachment att in message.Attachments) {
940                                 ContentType contentType = new ContentType (att.ContentType.ToString ());
941                                 if (att.Name != null) {
942                                         contentType.Name = att.Name;
943                                         if (att.NameEncoding != null)
944                                                 contentType.CharSet = att.NameEncoding.HeaderName;
945                                         att.ContentDisposition.FileName = att.Name;
946                                 }
947                                 StartSection (boundary, contentType, att.TransferEncoding, att == body ? null : att.ContentDisposition);
948
949                                 byte [] content = new byte [att.ContentStream.Length];
950                                 att.ContentStream.Read (content, 0, content.Length);
951                                 switch (att.TransferEncoding) {
952                                 case TransferEncoding.Base64:
953 #if TARGET_JVM
954                                         SendData (Convert.ToBase64String (content));
955 #else
956                                         SendData (Convert.ToBase64String (content, Base64FormattingOptions.InsertLineBreaks));
957 #endif
958                                         break;
959                                 case TransferEncoding.QuotedPrintable:
960                                         SendData (ToQuotedPrintable (content));
961                                         break;
962                                 case TransferEncoding.SevenBit:
963                                 case TransferEncoding.Unknown:
964                                         SendData (Encoding.ASCII.GetString (content));
965                                         break;
966                                 }
967
968                                 SendData (string.Empty);
969                         }
970                 }
971
972                 private SmtpResponse SendCommand (string command)
973                 {
974                         writer.Write (command);
975                         // Certain SMTP servers will reject mail sent with unix line-endings; see http://cr.yp.to/docs/smtplf.html
976                         writer.Write ("\r\n");
977                         writer.Flush ();
978                         return Read ();
979                 }
980
981                 private void SendHeader (string name, string value)
982                 {
983                         SendData (String.Format ("{0}: {1}", name, value));
984                 }
985
986                 private void StartSection (string section, ContentType sectionContentType)
987                 {
988                         SendData (String.Format ("--{0}", section));
989                         SendHeader ("content-type", sectionContentType.ToString ());
990                         SendData (string.Empty);
991                 }
992
993                 private void StartSection (string section, ContentType sectionContentType,TransferEncoding transferEncoding)
994                 {
995                         SendData (String.Format ("--{0}", section));
996                         SendHeader ("content-type", sectionContentType.ToString ());
997                         SendHeader ("content-transfer-encoding", GetTransferEncodingName (transferEncoding));
998                         SendData (string.Empty);
999                 }
1000
1001                 private void StartSection (string section, ContentType sectionContentType, TransferEncoding transferEncoding, ContentDisposition contentDisposition) {
1002                         SendData (String.Format ("--{0}", section));
1003                         SendHeader ("content-type", sectionContentType.ToString ());
1004                         SendHeader ("content-transfer-encoding", GetTransferEncodingName (transferEncoding));
1005                         if (contentDisposition != null)
1006                                 SendHeader ("content-disposition", contentDisposition.ToString ());
1007                         SendData (string.Empty);
1008                 }
1009
1010                 // use proper encoding to escape input
1011                 private string ToQuotedPrintable (string input, Encoding enc)
1012                 {
1013                         byte [] bytes = enc.GetBytes (input);
1014                         return ToQuotedPrintable (bytes);
1015                 }
1016
1017                 private string ToQuotedPrintable (byte [] bytes)
1018                 {
1019                         StringWriter writer = new StringWriter ();
1020                         int charsInLine = 0;
1021                         int curLen;
1022                         StringBuilder sb = new StringBuilder("=", 3);
1023                         byte equalSign = (byte)'=';
1024                         char c = (char)0;
1025
1026                         foreach (byte i in bytes) {
1027                                 if (i > 127 || i == equalSign) {
1028                                         sb.Length = 1;
1029                                         sb.Append(Convert.ToString (i, 16).ToUpperInvariant ());
1030                                         curLen = 3;
1031                                 } else {
1032                                         c = Convert.ToChar (i);
1033                                         if (c == '\r' || c == '\n') {
1034                                                 writer.Write (c);
1035                                                 charsInLine = 0;
1036                                                 continue;
1037                                         }
1038                                         curLen = 1;
1039                                 }
1040                                 
1041                                 charsInLine += curLen;
1042                                 if (charsInLine > 75) {
1043                                         writer.Write ("=\r\n");
1044                                         charsInLine = curLen;
1045                                 }
1046                                 if (curLen == 1)
1047                                         writer.Write (c);
1048                                 else
1049                                         writer.Write (sb.ToString ());
1050                         }
1051
1052                         return writer.ToString ();
1053                 }
1054                 private static string GetTransferEncodingName (TransferEncoding encoding)
1055                 {
1056                         switch (encoding) {
1057                         case TransferEncoding.QuotedPrintable:
1058                                 return "quoted-printable";
1059                         case TransferEncoding.SevenBit:
1060                                 return "7bit";
1061                         case TransferEncoding.Base64:
1062                                 return "base64";
1063                         }
1064                         return "unknown";
1065                 }
1066
1067 #if SECURITY_DEP
1068                 RemoteCertificateValidationCallback callback = delegate (object sender,
1069                                                                          X509Certificate certificate,
1070                                                                          X509Chain chain,
1071                                                                          SslPolicyErrors sslPolicyErrors) {
1072                         if (sslPolicyErrors != SslPolicyErrors.None)
1073                                 throw new InvalidOperationException ("SSL authentication error: " + sslPolicyErrors);
1074                         return true;
1075                         };
1076 #endif
1077
1078                 private void InitiateSecureConnection () {
1079                         SmtpResponse response = SendCommand ("STARTTLS");
1080
1081                         if (IsError (response)) {
1082                                 throw new SmtpException (SmtpStatusCode.GeneralFailure, "Server does not support secure connections.");
1083                         }
1084
1085 #if TARGET_JVM
1086                         ((NetworkStream) stream).ChangeToSSLSocket ();
1087 #elif SECURITY_DEP
1088                         SslStream sslStream = new SslStream (stream, false, callback, null);
1089                         CheckCancellation ();
1090                         sslStream.AuthenticateAsClient (Host, this.ClientCertificates, SslProtocols.Default, false);
1091                         stream = sslStream;
1092
1093 #else
1094                         throw new SystemException ("You are using an incomplete System.dll build");
1095 #endif
1096                 }
1097                 
1098                 void Authenticate ()
1099                 {
1100                         string user = null, pass = null;
1101                         
1102                         if (UseDefaultCredentials) {
1103                                 user = CredentialCache.DefaultCredentials.GetCredential (new System.Uri ("smtp://" + host), "basic").UserName;
1104                                 pass =  CredentialCache.DefaultCredentials.GetCredential (new System.Uri ("smtp://" + host), "basic").Password;
1105                         } else if (Credentials != null) {
1106                                 user = Credentials.GetCredential (host, port, "smtp").UserName;
1107                                 pass = Credentials.GetCredential (host, port, "smtp").Password;
1108                         } else {
1109                                 return;
1110                         }
1111                         
1112                         Authenticate (user, pass);
1113                 }
1114
1115                 void Authenticate (string Username, string Password)
1116                 {
1117                         // FIXME: use the proper AuthMech
1118                         SmtpResponse status = SendCommand ("AUTH LOGIN");
1119                         if (((int) status.StatusCode) != 334) {
1120                                 throw new SmtpException (status.StatusCode, status.Description);
1121                         }
1122
1123                         status = SendCommand (Convert.ToBase64String (Encoding.ASCII.GetBytes (Username)));
1124                         if (((int) status.StatusCode) != 334) {
1125                                 throw new SmtpException (status.StatusCode, status.Description);
1126                         }
1127
1128                         status = SendCommand (Convert.ToBase64String (Encoding.ASCII.GetBytes (Password)));
1129                         if (IsError (status)) {
1130                                 throw new SmtpException (status.StatusCode, status.Description);
1131                         }
1132                 }
1133                 
1134                 #endregion // Methods
1135                 
1136                 // The HeaderName struct is used to store constant string values representing mail headers.
1137                 private struct HeaderName {
1138                         public const string ContentTransferEncoding = "Content-Transfer-Encoding";
1139                         public const string ContentType = "Content-Type";
1140                         public const string Bcc = "Bcc";
1141                         public const string Cc = "Cc";
1142                         public const string From = "From";
1143                         public const string Subject = "Subject";
1144                         public const string To = "To";
1145                         public const string MimeVersion = "MIME-Version";
1146                         public const string MessageId = "Message-ID";
1147                         public const string Priority = "Priority";
1148                         public const string Importance = "Importance";
1149                         public const string XPriority = "X-Priority";
1150                         public const string Date = "Date";
1151                 }
1152
1153                 // This object encapsulates the status code and description of an SMTP response.
1154                 private struct SmtpResponse {
1155                         public SmtpStatusCode StatusCode;
1156                         public string Description;
1157
1158                         public static SmtpResponse Parse (string line) {
1159                                 SmtpResponse response = new SmtpResponse ();
1160
1161                                 if (line.Length < 4)
1162                                         throw new SmtpException ("Response is to short " +
1163                                                                  line.Length + ".");
1164
1165                                 if ((line [3] != ' ') && (line [3] != '-'))
1166                                         throw new SmtpException ("Response format is wrong.(" +
1167                                                                  line + ")");
1168
1169                                 // parse the response code
1170                                 response.StatusCode = (SmtpStatusCode) Int32.Parse (line.Substring (0, 3));
1171
1172                                 // set the raw response
1173                                 response.Description = line;
1174
1175                                 return response;
1176                         }
1177                 }
1178         }
1179
1180         class CCredentialsByHost : ICredentialsByHost
1181         {
1182                 public CCredentialsByHost (string userName, string password) {
1183                         this.userName = userName;
1184                         this.password = password;
1185                 }
1186
1187                 public NetworkCredential GetCredential (string host, int port, string authenticationType) {
1188                         return new NetworkCredential (userName, password);
1189                 }
1190
1191                 private string userName;
1192                 private string password;
1193         }
1194 }
1195
1196 #endif // NET_2_0