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