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