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