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