2 // System.Net.Mail.SmtpClient.cs
5 // Tim Coleman (tim@timcoleman.com)
7 // Copyright (C) Tim Coleman, 2004
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
33 #if MONOTOUCH || MONODROID
34 using System.Security.Cryptography.X509Certificates;
36 extern alias PrebuiltSystem;
37 using X509CertificateCollection = PrebuiltSystem::System.Security.Cryptography.X509Certificates.X509CertificateCollection;
38 using System.Security.Cryptography.X509Certificates;
44 using System.Collections.Generic;
45 using System.ComponentModel;
46 using System.Globalization;
49 using System.Net.Mime;
50 using System.Net.Sockets;
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 using System.Threading.Tasks;
60 namespace System.Net.Mail {
61 [Obsolete ("SmtpClient and its network of types are poorly designed, we strongly recommend you use https://github.com/jstedfast/MailKit and https://github.com/jstedfast/MimeKit instead")]
62 public class SmtpClient
70 ICredentialsByHost credentials;
71 string pickupDirectoryLocation;
72 SmtpDeliveryMethod deliveryMethod;
75 X509CertificateCollection clientCertificates;
83 MailAddress defaultFrom;
85 MailMessage messageInProcess;
87 BackgroundWorker worker;
88 object user_async_state;
97 class CancellationException : Exception
102 Mutex mutex = new Mutex ();
113 public SmtpClient (string host)
118 public SmtpClient (string host, int port) {
119 #if CONFIGURATION_DEP
120 SmtpSection cfg = (SmtpSection) ConfigurationManager.GetSection ("system.net/mailSettings/smtp");
123 this.host = cfg.Network.Host;
124 this.port = cfg.Network.Port;
125 this.enableSsl = cfg.Network.EnableSsl;
126 TargetName = cfg.Network.TargetName;
127 if (this.TargetName == null)
128 TargetName = "SMTPSVC/" + (host != null ? host : "");
131 if (cfg.Network.UserName != null) {
132 string password = String.Empty;
134 if (cfg.Network.Password != null)
135 password = cfg.Network.Password;
137 Credentials = new CCredentialsByHost (cfg.Network.UserName, password);
140 if (!String.IsNullOrEmpty (cfg.From))
141 defaultFrom = new MailAddress (cfg.From);
144 // Just to eliminate the warning, this codepath does not end up in production.
148 if (!String.IsNullOrEmpty (host))
153 else if (this.port == 0)
157 #endregion // Constructors
162 [MonoTODO("Client certificates not used")]
163 public X509CertificateCollection ClientCertificates {
165 if (clientCertificates == null)
166 clientCertificates = new X509CertificateCollection ();
167 return clientCertificates;
173 string TargetName { get; set; }
175 public ICredentialsByHost Credentials {
176 get { return credentials; }
183 public SmtpDeliveryMethod DeliveryMethod {
184 get { return deliveryMethod; }
187 deliveryMethod = value;
191 public bool EnableSsl {
192 get { return enableSsl; }
203 throw new ArgumentNullException ("value");
204 if (value.Length == 0)
205 throw new ArgumentException ("An empty string is not allowed.", "value");
211 public string PickupDirectoryLocation {
212 get { return pickupDirectoryLocation; }
213 set { pickupDirectoryLocation = value; }
220 throw new ArgumentOutOfRangeException ("value");
227 public ServicePoint ServicePoint {
228 get { throw new NotImplementedException (); }
232 get { return timeout; }
235 throw new ArgumentOutOfRangeException ("value");
241 public bool UseDefaultCredentials {
242 get { return false; }
243 [MonoNotSupported ("no DefaultCredential support in Mono")]
246 throw new NotImplementedException ("Default credentials are not supported");
251 #endregion // Properties
255 public event SendCompletedEventHandler SendCompleted;
260 public void Dispose ()
265 [MonoTODO ("Does nothing at the moment.")]
266 protected virtual void Dispose (bool disposing)
268 // TODO: We should close all the connections and abort any async operations here
270 private void CheckState ()
272 if (messageInProcess != null)
273 throw new InvalidOperationException ("Cannot set Timeout while Sending a message");
276 private static string EncodeAddress(MailAddress address)
278 if (!String.IsNullOrEmpty (address.DisplayName)) {
279 string encodedDisplayName = ContentType.EncodeSubjectRFC2047 (address.DisplayName, Encoding.UTF8);
280 return "\"" + encodedDisplayName + "\" <" + address.Address + ">";
282 return address.ToString ();
285 private static string EncodeAddresses(MailAddressCollection addresses)
287 StringBuilder sb = new StringBuilder();
289 foreach (MailAddress address in addresses) {
293 sb.Append(EncodeAddress(address));
296 return sb.ToString();
299 private string EncodeSubjectRFC2047 (MailMessage message)
301 return ContentType.EncodeSubjectRFC2047 (message.Subject, message.SubjectEncoding);
304 private string EncodeBody (MailMessage message)
306 string body = message.Body;
307 Encoding encoding = message.BodyEncoding;
309 switch (message.ContentTransferEncoding) {
310 case TransferEncoding.SevenBit:
312 case TransferEncoding.Base64:
313 return Convert.ToBase64String (encoding.GetBytes (body), Base64FormattingOptions.InsertLineBreaks);
315 return ToQuotedPrintable (body, encoding);
319 private string EncodeBody (AlternateView av)
321 //Encoding encoding = av.ContentType.CharSet != null ? Encoding.GetEncoding (av.ContentType.CharSet) : Encoding.UTF8;
323 byte [] bytes = new byte [av.ContentStream.Length];
324 av.ContentStream.Read (bytes, 0, bytes.Length);
327 switch (av.TransferEncoding) {
328 case TransferEncoding.SevenBit:
329 return Encoding.ASCII.GetString (bytes);
330 case TransferEncoding.Base64:
331 return Convert.ToBase64String (bytes, Base64FormattingOptions.InsertLineBreaks);
333 return ToQuotedPrintable (bytes);
338 private void EndSection (string section)
340 SendData (String.Format ("--{0}--", section));
341 SendData (string.Empty);
344 private string GenerateBoundary ()
346 string output = GenerateBoundary (boundaryIndex);
351 private static string GenerateBoundary (int index)
353 return String.Format ("--boundary_{0}_{1}", index, Guid.NewGuid ().ToString ("D"));
356 private bool IsError (SmtpResponse status)
358 return ((int) status.StatusCode) >= 400;
361 protected void OnSendCompleted (AsyncCompletedEventArgs e)
364 if (SendCompleted != null)
365 SendCompleted (this, e);
368 user_async_state = null;
372 private void CheckCancellation ()
374 if (worker != null && worker.CancellationPending)
375 throw new CancellationException ();
378 private SmtpResponse Read () {
379 byte [] buffer = new byte [512];
381 bool lastLine = false;
384 CheckCancellation ();
386 int readLength = stream.Read (buffer, position, buffer.Length - position);
387 if (readLength > 0) {
388 int available = position + readLength - 1;
389 if (available > 4 && (buffer [available] == '\n' || buffer [available] == '\r'))
390 for (int index = available - 3; ; index--) {
391 if (index < 0 || buffer [index] == '\n' || buffer [index] == '\r') {
392 lastLine = buffer [index + 4] == ' ';
398 position += readLength;
400 // check if buffer is full
401 if (position == buffer.Length) {
402 byte [] newBuffer = new byte [buffer.Length * 2];
403 Array.Copy (buffer, 0, newBuffer, 0, buffer.Length);
413 Encoding encoding = new ASCIIEncoding ();
415 string line = encoding.GetString (buffer, 0, position - 1);
417 // parse the line to the lastResponse object
418 SmtpResponse response = SmtpResponse.Parse (line);
422 throw new System.IO.IOException ("Connection closed");
426 void ResetExtensions()
428 authMechs = AuthMechs.None;
431 void ParseExtensions (string extens)
433 string[] parts = extens.Split ('\n');
435 foreach (string part in parts) {
439 string start = part.Substring (4);
440 if (start.StartsWith ("AUTH ", StringComparison.Ordinal)) {
441 string[] options = start.Split (' ');
442 for (int k = 1; k < options.Length; k++) {
443 string option = options[k].Trim();
444 // GSSAPI, KERBEROS_V4, NTLM not supported
448 authMechs |= AuthMechs.CramMD5;
451 authMechs |= AuthMechs.DigestMD5;
455 authMechs |= AuthMechs.Login;
458 authMechs |= AuthMechs.Plain;
466 public void Send (MailMessage message)
469 throw new ArgumentNullException ("message");
471 if (deliveryMethod == SmtpDeliveryMethod.Network && (Host == null || Host.Trim ().Length == 0))
472 throw new InvalidOperationException ("The SMTP host was not specified");
473 else if (deliveryMethod == SmtpDeliveryMethod.PickupDirectoryFromIis)
474 throw new NotSupportedException("IIS delivery is not supported");
479 // Block while sending
482 messageInProcess = message;
483 if (deliveryMethod == SmtpDeliveryMethod.SpecifiedPickupDirectory)
484 SendToFile (message);
486 SendInternal (message);
487 } catch (CancellationException) {
488 // This exception is introduced for convenient cancellation process.
489 } catch (SmtpException) {
491 } catch (Exception ex) {
492 throw new SmtpException ("Message could not be sent.", ex);
494 // Release the mutex to allow other threads access
495 mutex.ReleaseMutex ();
496 messageInProcess = null;
500 private void SendInternal (MailMessage message)
502 CheckCancellation ();
505 client = new TcpClient (host, port);
506 stream = client.GetStream ();
507 // FIXME: this StreamWriter creation is bogus.
508 // It expects as if a Stream were able to switch to SSL
509 // mode (such behavior is only in Mainsoft Socket API).
510 writer = new StreamWriter (stream);
511 reader = new StreamReader (stream);
526 // FIXME: simple implementation, could be brushed up.
527 private void SendToFile (MailMessage message)
529 if (!Path.IsPathRooted (pickupDirectoryLocation))
530 throw new SmtpException("Only absolute directories are allowed for pickup directory.");
532 string filename = Path.Combine (pickupDirectoryLocation,
533 Guid.NewGuid() + ".eml");
536 writer = new StreamWriter(filename);
538 // FIXME: See how Microsoft fixed the bug about envelope senders, and how it actually represents the info in .eml file headers
539 // For all we know, defaultFrom may be the envelope sender
540 // For now, we are no worse than some versions of .NET
541 MailAddress from = message.From;
545 string dt = DateTime.Now.ToString("ddd, dd MMM yyyy HH':'mm':'ss zzz", DateTimeFormatInfo.InvariantInfo);
546 // remove ':' from time zone offset (e.g. from "+01:00")
547 dt = dt.Remove(dt.Length - 3, 1);
548 SendHeader(HeaderName.Date, dt);
550 SendHeader (HeaderName.From, EncodeAddress(from));
551 SendHeader (HeaderName.To, EncodeAddresses(message.To));
552 if (message.CC.Count > 0)
553 SendHeader (HeaderName.Cc, EncodeAddresses(message.CC));
554 SendHeader (HeaderName.Subject, EncodeSubjectRFC2047 (message));
556 foreach (string s in message.Headers.AllKeys)
557 SendHeader (s, message.Headers [s]);
559 AddPriorityHeader (message);
562 if (message.Attachments.Count > 0)
563 SendWithAttachments (message);
565 SendWithoutAttachments (message, null, false);
569 if (writer != null) writer.Close(); writer = null;
573 private void SendCore (MailMessage message)
578 if (IsError (status))
579 throw new SmtpException (status.StatusCode, status.Description);
583 // FIXME: parse the list of extensions so we don't bother wasting
584 // our time trying commands if they aren't supported.
586 // Get the FQDN of the local machine
587 string fqdn = Dns.GetHostEntry (Dns.GetHostName ()).HostName;
588 status = SendCommand ("EHLO " + fqdn);
590 if (IsError (status)) {
591 status = SendCommand ("HELO " + fqdn);
593 if (IsError (status))
594 throw new SmtpException (status.StatusCode, status.Description);
596 // Parse ESMTP extensions
597 string extens = status.Description;
600 ParseExtensions (extens);
604 InitiateSecureConnection ();
606 writer = new StreamWriter (stream);
607 reader = new StreamReader (stream);
608 status = SendCommand ("EHLO " + fqdn);
610 if (IsError (status)) {
611 status = SendCommand ("HELO " + fqdn);
613 if (IsError (status))
614 throw new SmtpException (status.StatusCode, status.Description);
616 // Parse ESMTP extensions
617 string extens = status.Description;
619 ParseExtensions (extens);
623 if (authMechs != AuthMechs.None)
626 // The envelope sender: use 'Sender:' in preference of 'From:'
627 MailAddress sender = message.Sender;
629 sender = message.From;
631 sender = defaultFrom;
634 status = SendCommand ("MAIL FROM:<" + sender.Address + '>');
635 if (IsError (status)) {
636 throw new SmtpException (status.StatusCode, status.Description);
639 // Send RCPT TO: for all recipients
640 List<SmtpFailedRecipientException> sfre = new List<SmtpFailedRecipientException> ();
642 for (int i = 0; i < message.To.Count; i ++) {
643 status = SendCommand ("RCPT TO:<" + message.To [i].Address + '>');
644 if (IsError (status))
645 sfre.Add (new SmtpFailedRecipientException (status.StatusCode, message.To [i].Address));
647 for (int i = 0; i < message.CC.Count; i ++) {
648 status = SendCommand ("RCPT TO:<" + message.CC [i].Address + '>');
649 if (IsError (status))
650 sfre.Add (new SmtpFailedRecipientException (status.StatusCode, message.CC [i].Address));
652 for (int i = 0; i < message.Bcc.Count; i ++) {
653 status = SendCommand ("RCPT TO:<" + message.Bcc [i].Address + '>');
654 if (IsError (status))
655 sfre.Add (new SmtpFailedRecipientException (status.StatusCode, message.Bcc [i].Address));
659 throw new SmtpFailedRecipientsException ("failed recipients", sfre.ToArray ());
662 status = SendCommand ("DATA");
663 if (IsError (status))
664 throw new SmtpException (status.StatusCode, status.Description);
666 // Send message headers
667 string dt = DateTime.Now.ToString ("ddd, dd MMM yyyy HH':'mm':'ss zzz", DateTimeFormatInfo.InvariantInfo);
668 // remove ':' from time zone offset (e.g. from "+01:00")
669 dt = dt.Remove (dt.Length - 3, 1);
670 SendHeader (HeaderName.Date, dt);
672 MailAddress from = message.From;
676 SendHeader (HeaderName.From, EncodeAddress (from));
677 SendHeader (HeaderName.To, EncodeAddresses (message.To));
678 if (message.CC.Count > 0)
679 SendHeader (HeaderName.Cc, EncodeAddresses (message.CC));
680 SendHeader (HeaderName.Subject, EncodeSubjectRFC2047 (message));
684 switch (message.Priority){
685 case MailPriority.Normal:
689 case MailPriority.Low:
693 case MailPriority.High:
697 SendHeader ("Priority", v);
698 if (message.Sender != null)
699 SendHeader ("Sender", EncodeAddress (message.Sender));
700 if (message.ReplyToList.Count > 0)
701 SendHeader ("Reply-To", EncodeAddresses (message.ReplyToList));
703 foreach (string s in message.Headers.AllKeys)
704 SendHeader (s, ContentType.EncodeSubjectRFC2047 (message.Headers [s], message.HeadersEncoding));
706 AddPriorityHeader (message);
709 if (message.Attachments.Count > 0)
710 SendWithAttachments (message);
712 SendWithoutAttachments (message, null, false);
717 if (IsError (status))
718 throw new SmtpException (status.StatusCode, status.Description);
721 status = SendCommand ("QUIT");
722 } catch (System.IO.IOException) {
723 // We excuse server for the rude connection closing as a response to QUIT
727 public void Send (string from, string to, string subject, string body)
729 Send (new MailMessage (from, to, subject, body));
732 public Task SendMailAsync (MailMessage message)
734 var tcs = new TaskCompletionSource<object> ();
735 SendCompletedEventHandler handler = null;
736 handler = (s, e) => SendMailAsyncCompletedHandler (tcs, e, handler, this);
737 SendCompleted += handler;
738 SendAsync (message, tcs);
742 public Task SendMailAsync (string from, string recipients, string subject, string body)
744 return SendMailAsync (new MailMessage (from, recipients, subject, body));
747 static void SendMailAsyncCompletedHandler (TaskCompletionSource<object> source, AsyncCompletedEventArgs e, SendCompletedEventHandler handler, SmtpClient client)
749 if (source != e.UserState)
752 client.SendCompleted -= handler;
754 if (e.Error != null) {
755 source.SetException (e.Error);
760 source.SetCanceled ();
764 source.SetResult (null);
767 private void SendDot()
769 writer.Write(".\r\n");
773 private void SendData (string data)
775 if (String.IsNullOrEmpty (data)) {
776 writer.Write("\r\n");
781 StringReader sr = new StringReader (data);
783 bool escapeDots = deliveryMethod == SmtpDeliveryMethod.Network;
784 while ((line = sr.ReadLine ()) != null) {
785 CheckCancellation ();
789 for (i = 0; i < line.Length; i++) {
793 if (i > 0 && i == line.Length) {
798 writer.Write ("\r\n");
803 public void SendAsync (MailMessage message, object userToken)
806 throw new InvalidOperationException ("Another SendAsync operation is in progress");
808 worker = new BackgroundWorker ();
809 worker.DoWork += delegate (object o, DoWorkEventArgs ea) {
811 user_async_state = ea.Argument;
813 } catch (Exception ex) {
818 worker.WorkerSupportsCancellation = true;
819 worker.RunWorkerCompleted += delegate (object o, RunWorkerCompletedEventArgs ea) {
820 // Note that RunWorkerCompletedEventArgs.UserState cannot be used (LAMESPEC)
821 OnSendCompleted (new AsyncCompletedEventArgs (ea.Error, ea.Cancelled, user_async_state));
823 worker.RunWorkerAsync (userToken);
826 public void SendAsync (string from, string to, string subject, string body, object userToken)
828 SendAsync (new MailMessage (from, to, subject, body), userToken);
831 public void SendAsyncCancel ()
834 throw new InvalidOperationException ("SendAsync operation is not in progress");
835 worker.CancelAsync ();
838 private void AddPriorityHeader (MailMessage message) {
839 switch (message.Priority) {
840 case MailPriority.High:
841 SendHeader (HeaderName.Priority, "Urgent");
842 SendHeader (HeaderName.Importance, "high");
843 SendHeader (HeaderName.XPriority, "1");
845 case MailPriority.Low:
846 SendHeader (HeaderName.Priority, "Non-Urgent");
847 SendHeader (HeaderName.Importance, "low");
848 SendHeader (HeaderName.XPriority, "5");
853 private void SendSimpleBody (MailMessage message) {
854 SendHeader (HeaderName.ContentType, message.BodyContentType.ToString ());
855 if (message.ContentTransferEncoding != TransferEncoding.SevenBit)
856 SendHeader (HeaderName.ContentTransferEncoding, GetTransferEncodingName (message.ContentTransferEncoding));
857 SendData (string.Empty);
859 SendData (EncodeBody (message));
862 private void SendBodylessSingleAlternate (AlternateView av) {
863 SendHeader (HeaderName.ContentType, av.ContentType.ToString ());
864 if (av.TransferEncoding != TransferEncoding.SevenBit)
865 SendHeader (HeaderName.ContentTransferEncoding, GetTransferEncodingName (av.TransferEncoding));
866 SendData (string.Empty);
868 SendData (EncodeBody (av));
871 private void SendWithoutAttachments (MailMessage message, string boundary, bool attachmentExists)
873 if (message.Body == null && message.AlternateViews.Count == 1)
874 SendBodylessSingleAlternate (message.AlternateViews [0]);
875 else if (message.AlternateViews.Count > 0)
876 SendBodyWithAlternateViews (message, boundary, attachmentExists);
878 SendSimpleBody (message);
882 private void SendWithAttachments (MailMessage message) {
883 string boundary = GenerateBoundary ();
885 // first "multipart/mixed"
886 ContentType messageContentType = new ContentType ();
887 messageContentType.Boundary = boundary;
888 messageContentType.MediaType = "multipart/mixed";
889 messageContentType.CharSet = null;
891 SendHeader (HeaderName.ContentType, messageContentType.ToString ());
892 SendData (String.Empty);
895 Attachment body = null;
897 if (message.AlternateViews.Count > 0)
898 SendWithoutAttachments (message, boundary, true);
900 body = Attachment.CreateAttachmentFromString (message.Body, null, message.BodyEncoding, message.IsBodyHtml ? "text/html" : "text/plain");
901 message.Attachments.Insert (0, body);
905 SendAttachments (message, body, boundary);
908 message.Attachments.Remove (body);
911 EndSection (boundary);
914 private void SendBodyWithAlternateViews (MailMessage message, string boundary, bool attachmentExists)
916 AlternateViewCollection alternateViews = message.AlternateViews;
918 string inner_boundary = GenerateBoundary ();
920 ContentType messageContentType = new ContentType ();
921 messageContentType.Boundary = inner_boundary;
922 messageContentType.MediaType = "multipart/alternative";
924 if (!attachmentExists) {
925 SendHeader (HeaderName.ContentType, messageContentType.ToString ());
926 SendData (String.Empty);
930 AlternateView body = null;
931 if (message.Body != null) {
932 body = AlternateView.CreateAlternateViewFromString (message.Body, message.BodyEncoding, message.IsBodyHtml ? "text/html" : "text/plain");
933 alternateViews.Insert (0, body);
934 StartSection (boundary, messageContentType);
938 // alternate view sections
939 foreach (AlternateView av in alternateViews) {
941 string alt_boundary = null;
942 ContentType contentType;
943 if (av.LinkedResources.Count > 0) {
944 alt_boundary = GenerateBoundary ();
945 contentType = new ContentType ("multipart/related");
946 contentType.Boundary = alt_boundary;
948 contentType.Parameters ["type"] = av.ContentType.ToString ();
949 StartSection (inner_boundary, contentType);
950 StartSection (alt_boundary, av.ContentType, av);
952 contentType = new ContentType (av.ContentType.ToString ());
953 StartSection (inner_boundary, contentType, av);
956 switch (av.TransferEncoding) {
957 case TransferEncoding.Base64:
958 byte [] content = new byte [av.ContentStream.Length];
959 av.ContentStream.Read (content, 0, content.Length);
960 SendData (Convert.ToBase64String (content, Base64FormattingOptions.InsertLineBreaks));
962 case TransferEncoding.QuotedPrintable:
963 byte [] bytes = new byte [av.ContentStream.Length];
964 av.ContentStream.Read (bytes, 0, bytes.Length);
965 SendData (ToQuotedPrintable (bytes));
967 case TransferEncoding.SevenBit:
968 case TransferEncoding.Unknown:
969 content = new byte [av.ContentStream.Length];
970 av.ContentStream.Read (content, 0, content.Length);
971 SendData (Encoding.ASCII.GetString (content));
975 if (av.LinkedResources.Count > 0) {
976 SendLinkedResources (message, av.LinkedResources, alt_boundary);
977 EndSection (alt_boundary);
980 if (!attachmentExists)
981 SendData (string.Empty);
986 alternateViews.Remove (body);
988 EndSection (inner_boundary);
991 private void SendLinkedResources (MailMessage message, LinkedResourceCollection resources, string boundary)
993 foreach (LinkedResource lr in resources) {
994 StartSection (boundary, lr.ContentType, lr);
996 switch (lr.TransferEncoding) {
997 case TransferEncoding.Base64:
998 byte [] content = new byte [lr.ContentStream.Length];
999 lr.ContentStream.Read (content, 0, content.Length);
1000 SendData (Convert.ToBase64String (content, Base64FormattingOptions.InsertLineBreaks));
1002 case TransferEncoding.QuotedPrintable:
1003 byte [] bytes = new byte [lr.ContentStream.Length];
1004 lr.ContentStream.Read (bytes, 0, bytes.Length);
1005 SendData (ToQuotedPrintable (bytes));
1007 case TransferEncoding.SevenBit:
1008 case TransferEncoding.Unknown:
1009 content = new byte [lr.ContentStream.Length];
1010 lr.ContentStream.Read (content, 0, content.Length);
1011 SendData (Encoding.ASCII.GetString (content));
1017 private void SendAttachments (MailMessage message, Attachment body, string boundary) {
1018 foreach (Attachment att in message.Attachments) {
1019 ContentType contentType = new ContentType (att.ContentType.ToString ());
1020 if (att.Name != null) {
1021 contentType.Name = att.Name;
1022 if (att.NameEncoding != null)
1023 contentType.CharSet = att.NameEncoding.HeaderName;
1024 att.ContentDisposition.FileName = att.Name;
1026 StartSection (boundary, contentType, att, att != body);
1028 byte [] content = new byte [att.ContentStream.Length];
1029 att.ContentStream.Read (content, 0, content.Length);
1030 switch (att.TransferEncoding) {
1031 case TransferEncoding.Base64:
1032 SendData (Convert.ToBase64String (content, Base64FormattingOptions.InsertLineBreaks));
1034 case TransferEncoding.QuotedPrintable:
1035 SendData (ToQuotedPrintable (content));
1037 case TransferEncoding.SevenBit:
1038 case TransferEncoding.Unknown:
1039 SendData (Encoding.ASCII.GetString (content));
1043 SendData (string.Empty);
1047 private SmtpResponse SendCommand (string command)
1049 writer.Write (command);
1050 // Certain SMTP servers will reject mail sent with unix line-endings; see http://cr.yp.to/docs/smtplf.html
1051 writer.Write ("\r\n");
1056 private void SendHeader (string name, string value)
1058 SendData (String.Format ("{0}: {1}", name, value));
1061 private void StartSection (string section, ContentType sectionContentType)
1063 SendData (String.Format ("--{0}", section));
1064 SendHeader ("content-type", sectionContentType.ToString ());
1065 SendData (string.Empty);
1068 private void StartSection (string section, ContentType sectionContentType, AttachmentBase att)
1070 SendData (String.Format ("--{0}", section));
1071 SendHeader ("content-type", sectionContentType.ToString ());
1072 SendHeader ("content-transfer-encoding", GetTransferEncodingName (att.TransferEncoding));
1073 if (!string.IsNullOrEmpty (att.ContentId))
1074 SendHeader("content-ID", "<" + att.ContentId + ">");
1075 SendData (string.Empty);
1078 private void StartSection (string section, ContentType sectionContentType, Attachment att, bool sendDisposition) {
1079 SendData (String.Format ("--{0}", section));
1080 if (!string.IsNullOrEmpty (att.ContentId))
1081 SendHeader("content-ID", "<" + att.ContentId + ">");
1082 SendHeader ("content-type", sectionContentType.ToString ());
1083 SendHeader ("content-transfer-encoding", GetTransferEncodingName (att.TransferEncoding));
1084 if (sendDisposition)
1085 SendHeader ("content-disposition", att.ContentDisposition.ToString ());
1086 SendData (string.Empty);
1089 // use proper encoding to escape input
1090 private string ToQuotedPrintable (string input, Encoding enc)
1092 byte [] bytes = enc.GetBytes (input);
1093 return ToQuotedPrintable (bytes);
1096 private string ToQuotedPrintable (byte [] bytes)
1098 StringWriter writer = new StringWriter ();
1099 int charsInLine = 0;
1101 StringBuilder sb = new StringBuilder("=", 3);
1102 byte equalSign = (byte)'=';
1105 foreach (byte i in bytes) {
1106 if (i > 127 || i == equalSign) {
1108 sb.Append(Convert.ToString (i, 16).ToUpperInvariant ());
1111 c = Convert.ToChar (i);
1112 if (c == '\r' || c == '\n') {
1120 charsInLine += curLen;
1121 if (charsInLine > 75) {
1122 writer.Write ("=\r\n");
1123 charsInLine = curLen;
1128 writer.Write (sb.ToString ());
1131 return writer.ToString ();
1133 private static string GetTransferEncodingName (TransferEncoding encoding)
1136 case TransferEncoding.QuotedPrintable:
1137 return "quoted-printable";
1138 case TransferEncoding.SevenBit:
1140 case TransferEncoding.Base64:
1147 RemoteCertificateValidationCallback callback = delegate (object sender,
1148 X509Certificate certificate,
1150 SslPolicyErrors sslPolicyErrors) {
1151 // honor any exciting callback defined on ServicePointManager
1152 if (ServicePointManager.ServerCertificateValidationCallback != null)
1153 return ServicePointManager.ServerCertificateValidationCallback (sender, certificate, chain, sslPolicyErrors);
1154 // otherwise provide our own
1155 if (sslPolicyErrors != SslPolicyErrors.None)
1156 throw new InvalidOperationException ("SSL authentication error: " + sslPolicyErrors);
1161 private void InitiateSecureConnection () {
1162 SmtpResponse response = SendCommand ("STARTTLS");
1164 if (IsError (response)) {
1165 throw new SmtpException (SmtpStatusCode.GeneralFailure, "Server does not support secure connections.");
1169 SslStream sslStream = new SslStream (stream, false, callback, null);
1170 CheckCancellation ();
1171 sslStream.AuthenticateAsClient (Host, this.ClientCertificates, SslProtocols.Default, false);
1175 throw new SystemException ("You are using an incomplete System.dll build");
1179 void Authenticate ()
1181 string user = null, pass = null;
1183 if (UseDefaultCredentials) {
1184 user = CredentialCache.DefaultCredentials.GetCredential (new System.Uri ("smtp://" + host), "basic").UserName;
1185 pass = CredentialCache.DefaultCredentials.GetCredential (new System.Uri ("smtp://" + host), "basic").Password;
1186 } else if (Credentials != null) {
1187 user = Credentials.GetCredential (host, port, "smtp").UserName;
1188 pass = Credentials.GetCredential (host, port, "smtp").Password;
1193 Authenticate (user, pass);
1196 void CheckStatus (SmtpResponse status, int i)
1198 if (((int) status.StatusCode) != i)
1199 throw new SmtpException (status.StatusCode, status.Description);
1202 void ThrowIfError (SmtpResponse status)
1204 if (IsError (status))
1205 throw new SmtpException (status.StatusCode, status.Description);
1208 void Authenticate (string user, string password)
1210 if (authMechs == AuthMechs.None)
1213 SmtpResponse status;
1215 if ((authMechs & AuthMechs.DigestMD5) != 0) {
1216 status = SendCommand ("AUTH DIGEST-MD5");
1217 CheckStatus (status, 334);
1218 string challenge = Encoding.ASCII.GetString (Convert.FromBase64String (status.Description.Substring (4)));
1219 Console.WriteLine ("CHALLENGE: {0}", challenge);
1220 DigestSession session = new DigestSession ();
1221 session.Parse (false, challenge);
1222 string response = session.Authenticate (this, user, password);
1223 status = SendCommand (Convert.ToBase64String (Encoding.UTF8.GetBytes (response)));
1224 CheckStatus (status, 235);
1226 if ((authMechs & AuthMechs.Login) != 0) {
1227 status = SendCommand ("AUTH LOGIN");
1228 CheckStatus (status, 334);
1229 status = SendCommand (Convert.ToBase64String (Encoding.UTF8.GetBytes (user)));
1230 CheckStatus (status, 334);
1231 status = SendCommand (Convert.ToBase64String (Encoding.UTF8.GetBytes (password)));
1232 CheckStatus (status, 235);
1233 } else if ((authMechs & AuthMechs.Plain) != 0) {
1234 string s = String.Format ("\0{0}\0{1}", user, password);
1235 s = Convert.ToBase64String (Encoding.UTF8.GetBytes (s));
1236 status = SendCommand ("AUTH PLAIN " + s);
1237 CheckStatus (status, 235);
1239 throw new SmtpException ("AUTH types PLAIN, LOGIN not supported by the server");
1243 #endregion // Methods
1245 // The HeaderName struct is used to store constant string values representing mail headers.
1246 private struct HeaderName {
1247 public const string ContentTransferEncoding = "Content-Transfer-Encoding";
1248 public const string ContentType = "Content-Type";
1249 public const string Bcc = "Bcc";
1250 public const string Cc = "Cc";
1251 public const string From = "From";
1252 public const string Subject = "Subject";
1253 public const string To = "To";
1254 public const string MimeVersion = "MIME-Version";
1255 public const string MessageId = "Message-ID";
1256 public const string Priority = "Priority";
1257 public const string Importance = "Importance";
1258 public const string XPriority = "X-Priority";
1259 public const string Date = "Date";
1262 // This object encapsulates the status code and description of an SMTP response.
1263 private struct SmtpResponse {
1264 public SmtpStatusCode StatusCode;
1265 public string Description;
1267 public static SmtpResponse Parse (string line) {
1268 SmtpResponse response = new SmtpResponse ();
1270 if (line.Length < 4)
1271 throw new SmtpException ("Response is to short " +
1274 if ((line [3] != ' ') && (line [3] != '-'))
1275 throw new SmtpException ("Response format is wrong.(" +
1278 // parse the response code
1279 response.StatusCode = (SmtpStatusCode) Int32.Parse (line.Substring (0, 3));
1281 // set the raw response
1282 response.Description = line;
1289 class CCredentialsByHost : ICredentialsByHost
1291 public CCredentialsByHost (string userName, string password) {
1292 this.userName = userName;
1293 this.password = password;
1296 public NetworkCredential GetCredential (string host, int port, string authenticationType) {
1297 return new NetworkCredential (userName, password);
1300 private string userName;
1301 private string password;