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