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.
34 extern alias PrebuiltSystem;
38 using System.Collections.Generic;
39 using System.ComponentModel;
40 using System.Globalization;
43 using System.Net.Mime;
44 using System.Net.Sockets;
45 using System.Security.Cryptography.X509Certificates;
47 using System.Threading;
48 using System.Reflection;
49 using System.Net.Configuration;
50 using System.Configuration;
51 using System.Net.Security;
52 using System.Security.Authentication;
55 using X509CertificateCollection = PrebuiltSystem::System.Security.Cryptography.X509Certificates.X509CertificateCollection;
58 namespace System.Net.Mail {
59 public class SmtpClient
66 ICredentialsByHost credentials;
67 string pickupDirectoryLocation;
68 SmtpDeliveryMethod deliveryMethod;
71 X509CertificateCollection clientCertificates;
79 MailAddress defaultFrom;
81 MailMessage messageInProcess;
83 BackgroundWorker worker;
84 object user_async_state;
93 class CancellationException : Exception
98 Mutex mutex = new Mutex ();
109 public SmtpClient (string host)
114 public SmtpClient (string host, int port) {
115 #if CONFIGURATION_DEP
116 SmtpSection cfg = (SmtpSection) ConfigurationManager.GetSection ("system.net/mailSettings/smtp");
119 this.host = cfg.Network.Host;
120 this.port = cfg.Network.Port;
121 TargetName = cfg.Network.TargetName;
122 if (this.TargetName == null)
123 TargetName = "SMTPSVC/" + (host != null ? host : "");
126 if (cfg.Network.UserName != null) {
127 string password = String.Empty;
129 if (cfg.Network.Password != null)
130 password = cfg.Network.Password;
132 Credentials = new CCredentialsByHost (cfg.Network.UserName, password);
135 if (cfg.From != null)
136 defaultFrom = new MailAddress (cfg.From);
139 // Just to eliminate the warning, this codepath does not end up in production.
143 if (!String.IsNullOrEmpty (host))
150 #endregion // Constructors
155 [MonoTODO("Client certificates not used")]
156 public X509CertificateCollection ClientCertificates {
158 if (clientCertificates == null)
159 clientCertificates = new X509CertificateCollection ();
160 return clientCertificates;
168 string TargetName { get; set; }
170 public ICredentialsByHost Credentials {
171 get { return credentials; }
178 public SmtpDeliveryMethod DeliveryMethod {
179 get { return deliveryMethod; }
182 deliveryMethod = value;
186 public bool EnableSsl {
187 get { return enableSsl; }
198 throw new ArgumentNullException ("value");
199 if (value.Length == 0)
200 throw new ArgumentException ("An empty string is not allowed.", "value");
206 public string PickupDirectoryLocation {
207 get { return pickupDirectoryLocation; }
208 set { pickupDirectoryLocation = value; }
215 throw new ArgumentOutOfRangeException ("value");
222 public ServicePoint ServicePoint {
223 get { throw new NotImplementedException (); }
227 get { return timeout; }
230 throw new ArgumentOutOfRangeException ("value");
236 public bool UseDefaultCredentials {
237 get { return false; }
238 [MonoNotSupported ("no DefaultCredential support in Mono")]
241 throw new NotImplementedException ("Default credentials are not supported");
246 #endregion // Properties
250 public event SendCompletedEventHandler SendCompleted;
256 private void CheckState ()
258 if (messageInProcess != null)
259 throw new InvalidOperationException ("Cannot set Timeout while Sending a message");
262 private static string EncodeAddress(MailAddress address)
264 string encodedDisplayName = ContentType.EncodeSubjectRFC2047 (address.DisplayName, Encoding.UTF8);
265 return "\"" + encodedDisplayName + "\" <" + address.Address + ">";
268 private static string EncodeAddresses(MailAddressCollection addresses)
270 StringBuilder sb = new StringBuilder();
272 foreach (MailAddress address in addresses) {
276 sb.Append(EncodeAddress(address));
279 return sb.ToString();
282 private string EncodeSubjectRFC2047 (MailMessage message)
284 return ContentType.EncodeSubjectRFC2047 (message.Subject, message.SubjectEncoding);
287 private string EncodeBody (MailMessage message)
289 string body = message.Body;
290 Encoding encoding = message.BodyEncoding;
292 switch (message.ContentTransferEncoding) {
293 case TransferEncoding.SevenBit:
295 case TransferEncoding.Base64:
296 return Convert.ToBase64String (encoding.GetBytes (body), Base64FormattingOptions.InsertLineBreaks);
298 return ToQuotedPrintable (body, encoding);
302 private string EncodeBody (AlternateView av)
304 //Encoding encoding = av.ContentType.CharSet != null ? Encoding.GetEncoding (av.ContentType.CharSet) : Encoding.UTF8;
306 byte [] bytes = new byte [av.ContentStream.Length];
307 av.ContentStream.Read (bytes, 0, bytes.Length);
310 switch (av.TransferEncoding) {
311 case TransferEncoding.SevenBit:
312 return Encoding.ASCII.GetString (bytes);
313 case TransferEncoding.Base64:
314 return Convert.ToBase64String (bytes, Base64FormattingOptions.InsertLineBreaks);
316 return ToQuotedPrintable (bytes);
321 private void EndSection (string section)
323 SendData (String.Format ("--{0}--", section));
324 SendData (string.Empty);
327 private string GenerateBoundary ()
329 string output = GenerateBoundary (boundaryIndex);
334 private static string GenerateBoundary (int index)
336 return String.Format ("--boundary_{0}_{1}", index, Guid.NewGuid ().ToString ("D"));
339 private bool IsError (SmtpResponse status)
341 return ((int) status.StatusCode) >= 400;
344 protected void OnSendCompleted (AsyncCompletedEventArgs e)
347 if (SendCompleted != null)
348 SendCompleted (this, e);
351 user_async_state = null;
355 private void CheckCancellation ()
357 if (worker != null && worker.CancellationPending)
358 throw new CancellationException ();
361 private SmtpResponse Read () {
362 byte [] buffer = new byte [512];
364 bool lastLine = false;
367 CheckCancellation ();
369 int readLength = stream.Read (buffer, position, buffer.Length - position);
370 if (readLength > 0) {
371 int available = position + readLength - 1;
372 if (available > 4 && (buffer [available] == '\n' || buffer [available] == '\r'))
373 for (int index = available - 3; ; index--) {
374 if (index < 0 || buffer [index] == '\n' || buffer [index] == '\r') {
375 lastLine = buffer [index + 4] == ' ';
381 position += readLength;
383 // check if buffer is full
384 if (position == buffer.Length) {
385 byte [] newBuffer = new byte [buffer.Length * 2];
386 Array.Copy (buffer, 0, newBuffer, 0, buffer.Length);
396 Encoding encoding = new ASCIIEncoding ();
398 string line = encoding.GetString (buffer, 0, position - 1);
400 // parse the line to the lastResponse object
401 SmtpResponse response = SmtpResponse.Parse (line);
405 throw new System.IO.IOException ("Connection closed");
409 void ResetExtensions()
411 authMechs = AuthMechs.None;
414 void ParseExtensions (string extens)
416 string[] parts = extens.Split ('\n');
418 foreach (string part in parts) {
422 string start = part.Substring (4);
423 if (start.StartsWith ("AUTH ", StringComparison.Ordinal)) {
424 string[] options = start.Split (' ');
425 for (int k = 1; k < options.Length; k++) {
426 string option = options[k].Trim();
427 // GSSAPI, KERBEROS_V4, NTLM not supported
431 authMechs |= AuthMechs.CramMD5;
434 authMechs |= AuthMechs.DigestMD5;
438 authMechs |= AuthMechs.Login;
441 authMechs |= AuthMechs.Plain;
449 public void Send (MailMessage message)
452 throw new ArgumentNullException ("message");
454 if (deliveryMethod == SmtpDeliveryMethod.Network && (Host == null || Host.Trim ().Length == 0))
455 throw new InvalidOperationException ("The SMTP host was not specified");
456 else if (deliveryMethod == SmtpDeliveryMethod.PickupDirectoryFromIis)
457 throw new NotSupportedException("IIS delivery is not supported");
462 // Block while sending
465 messageInProcess = message;
466 if (deliveryMethod == SmtpDeliveryMethod.SpecifiedPickupDirectory)
467 SendToFile (message);
469 SendInternal (message);
470 } catch (CancellationException) {
471 // This exception is introduced for convenient cancellation process.
472 } catch (SmtpException) {
474 } catch (Exception ex) {
475 throw new SmtpException ("Message could not be sent.", ex);
477 // Release the mutex to allow other threads access
478 mutex.ReleaseMutex ();
479 messageInProcess = null;
483 private void SendInternal (MailMessage message)
485 CheckCancellation ();
488 client = new TcpClient (host, port);
489 stream = client.GetStream ();
490 // FIXME: this StreamWriter creation is bogus.
491 // It expects as if a Stream were able to switch to SSL
492 // mode (such behavior is only in Mainsoft Socket API).
493 writer = new StreamWriter (stream);
494 reader = new StreamReader (stream);
509 // FIXME: simple implementation, could be brushed up.
510 private void SendToFile (MailMessage message)
512 if (!Path.IsPathRooted (pickupDirectoryLocation))
513 throw new SmtpException("Only absolute directories are allowed for pickup directory.");
515 string filename = Path.Combine (pickupDirectoryLocation,
516 Guid.NewGuid() + ".eml");
519 writer = new StreamWriter(filename);
521 // FIXME: See how Microsoft fixed the bug about envelope senders, and how it actually represents the info in .eml file headers
522 // For all we know, defaultFrom may be the envelope sender
523 // For now, we are no worse than some versions of .NET
524 MailAddress from = message.From;
528 SendHeader (HeaderName.Date, DateTime.Now.ToString ("ddd, dd MMM yyyy HH':'mm':'ss zzz", DateTimeFormatInfo.InvariantInfo));
529 SendHeader (HeaderName.From, EncodeAddress(from));
530 SendHeader (HeaderName.To, EncodeAddresses(message.To));
531 if (message.CC.Count > 0)
532 SendHeader (HeaderName.Cc, EncodeAddresses(message.CC));
533 SendHeader (HeaderName.Subject, EncodeSubjectRFC2047 (message));
535 foreach (string s in message.Headers.AllKeys)
536 SendHeader (s, message.Headers [s]);
538 AddPriorityHeader (message);
541 if (message.Attachments.Count > 0)
542 SendWithAttachments (message);
544 SendWithoutAttachments (message, null, false);
548 if (writer != null) writer.Close(); writer = null;
552 private void SendCore (MailMessage message)
557 if (IsError (status))
558 throw new SmtpException (status.StatusCode, status.Description);
562 // FIXME: parse the list of extensions so we don't bother wasting
563 // our time trying commands if they aren't supported.
564 status = SendCommand ("EHLO " + Dns.GetHostName ());
566 if (IsError (status)) {
567 status = SendCommand ("HELO " + Dns.GetHostName ());
569 if (IsError (status))
570 throw new SmtpException (status.StatusCode, status.Description);
572 // Parse ESMTP extensions
573 string extens = status.Description;
576 ParseExtensions (extens);
580 InitiateSecureConnection ();
582 writer = new StreamWriter (stream);
583 reader = new StreamReader (stream);
584 status = SendCommand ("EHLO " + Dns.GetHostName ());
586 if (IsError (status)) {
587 status = SendCommand ("HELO " + Dns.GetHostName ());
589 if (IsError (status))
590 throw new SmtpException (status.StatusCode, status.Description);
592 // Parse ESMTP extensions
593 string extens = status.Description;
595 ParseExtensions (extens);
599 if (authMechs != AuthMechs.None)
602 // The envelope sender: use 'Sender:' in preference of 'From:'
603 MailAddress sender = message.Sender;
605 sender = message.From;
607 sender = defaultFrom;
610 status = SendCommand ("MAIL FROM:<" + sender.Address + '>');
611 if (IsError (status)) {
612 throw new SmtpException (status.StatusCode, status.Description);
615 // Send RCPT TO: for all recipients
616 List<SmtpFailedRecipientException> sfre = new List<SmtpFailedRecipientException> ();
618 for (int i = 0; i < message.To.Count; i ++) {
619 status = SendCommand ("RCPT TO:<" + message.To [i].Address + '>');
620 if (IsError (status))
621 sfre.Add (new SmtpFailedRecipientException (status.StatusCode, message.To [i].Address));
623 for (int i = 0; i < message.CC.Count; i ++) {
624 status = SendCommand ("RCPT TO:<" + message.CC [i].Address + '>');
625 if (IsError (status))
626 sfre.Add (new SmtpFailedRecipientException (status.StatusCode, message.CC [i].Address));
628 for (int i = 0; i < message.Bcc.Count; i ++) {
629 status = SendCommand ("RCPT TO:<" + message.Bcc [i].Address + '>');
630 if (IsError (status))
631 sfre.Add (new SmtpFailedRecipientException (status.StatusCode, message.Bcc [i].Address));
634 #if TARGET_JVM // List<T>.ToArray () is not supported
635 if (sfre.Count > 0) {
636 SmtpFailedRecipientException[] xs = new SmtpFailedRecipientException[sfre.Count];
638 throw new SmtpFailedRecipientsException ("failed recipients", xs);
642 throw new SmtpFailedRecipientsException ("failed recipients", sfre.ToArray ());
646 status = SendCommand ("DATA");
647 if (IsError (status))
648 throw new SmtpException (status.StatusCode, status.Description);
650 // Send message headers
651 string dt = DateTime.Now.ToString ("ddd, dd MMM yyyy HH':'mm':'ss zzz", DateTimeFormatInfo.InvariantInfo);
652 // remove ':' from time zone offset (e.g. from "+01:00")
653 dt = dt.Remove (dt.Length - 3, 1);
654 SendHeader (HeaderName.Date, dt);
656 MailAddress from = message.From;
660 SendHeader (HeaderName.From, EncodeAddress (from));
661 SendHeader (HeaderName.To, EncodeAddresses (message.To));
662 if (message.CC.Count > 0)
663 SendHeader (HeaderName.Cc, EncodeAddresses (message.CC));
664 SendHeader (HeaderName.Subject, EncodeSubjectRFC2047 (message));
668 switch (message.Priority){
669 case MailPriority.Normal:
673 case MailPriority.Low:
677 case MailPriority.High:
681 SendHeader ("Priority", v);
682 if (message.Sender != null)
683 SendHeader ("Sender", EncodeAddress (message.Sender));
684 if (message.ReplyToList.Count > 0)
685 SendHeader ("Reply-To", EncodeAddresses (message.ReplyToList));
688 foreach (string s in message.Headers.AllKeys)
689 SendHeader (s, ContentType.EncodeSubjectRFC2047 (message.Headers [s], message.HeadersEncoding));
691 foreach (string s in message.Headers.AllKeys)
692 SendHeader (s, message.Headers [s]);
695 AddPriorityHeader (message);
698 if (message.Attachments.Count > 0)
699 SendWithAttachments (message);
701 SendWithoutAttachments (message, null, false);
706 if (IsError (status))
707 throw new SmtpException (status.StatusCode, status.Description);
710 status = SendCommand ("QUIT");
711 } catch (System.IO.IOException) {
712 // We excuse server for the rude connection closing as a response to QUIT
716 public void Send (string from, string to, string subject, string body)
718 Send (new MailMessage (from, to, subject, body));
721 private void SendDot()
723 writer.Write(".\r\n");
727 private void SendData (string data)
729 if (String.IsNullOrEmpty (data)) {
730 writer.Write("\r\n");
735 StringReader sr = new StringReader (data);
737 bool escapeDots = deliveryMethod == SmtpDeliveryMethod.Network;
738 while ((line = sr.ReadLine ()) != null) {
739 CheckCancellation ();
743 for (i = 0; i < line.Length; i++) {
747 if (i > 0 && i == line.Length) {
752 writer.Write ("\r\n");
757 public void SendAsync (MailMessage message, object userToken)
760 throw new InvalidOperationException ("Another SendAsync operation is in progress");
762 worker = new BackgroundWorker ();
763 worker.DoWork += delegate (object o, DoWorkEventArgs ea) {
765 user_async_state = ea.Argument;
767 } catch (Exception ex) {
772 worker.WorkerSupportsCancellation = true;
773 worker.RunWorkerCompleted += delegate (object o, RunWorkerCompletedEventArgs ea) {
774 // Note that RunWorkerCompletedEventArgs.UserState cannot be used (LAMESPEC)
775 OnSendCompleted (new AsyncCompletedEventArgs (ea.Error, ea.Cancelled, user_async_state));
777 worker.RunWorkerAsync (userToken);
780 public void SendAsync (string from, string to, string subject, string body, object userToken)
782 SendAsync (new MailMessage (from, to, subject, body), userToken);
785 public void SendAsyncCancel ()
788 throw new InvalidOperationException ("SendAsync operation is not in progress");
789 worker.CancelAsync ();
792 private void AddPriorityHeader (MailMessage message) {
793 switch (message.Priority) {
794 case MailPriority.High:
795 SendHeader (HeaderName.Priority, "Urgent");
796 SendHeader (HeaderName.Importance, "high");
797 SendHeader (HeaderName.XPriority, "1");
799 case MailPriority.Low:
800 SendHeader (HeaderName.Priority, "Non-Urgent");
801 SendHeader (HeaderName.Importance, "low");
802 SendHeader (HeaderName.XPriority, "5");
807 private void SendSimpleBody (MailMessage message) {
808 SendHeader (HeaderName.ContentType, message.BodyContentType.ToString ());
809 if (message.ContentTransferEncoding != TransferEncoding.SevenBit)
810 SendHeader (HeaderName.ContentTransferEncoding, GetTransferEncodingName (message.ContentTransferEncoding));
811 SendData (string.Empty);
813 SendData (EncodeBody (message));
816 private void SendBodylessSingleAlternate (AlternateView av) {
817 SendHeader (HeaderName.ContentType, av.ContentType.ToString ());
818 if (av.TransferEncoding != TransferEncoding.SevenBit)
819 SendHeader (HeaderName.ContentTransferEncoding, GetTransferEncodingName (av.TransferEncoding));
820 SendData (string.Empty);
822 SendData (EncodeBody (av));
825 private void SendWithoutAttachments (MailMessage message, string boundary, bool attachmentExists)
827 if (message.Body == null && message.AlternateViews.Count == 1)
828 SendBodylessSingleAlternate (message.AlternateViews [0]);
829 else if (message.AlternateViews.Count > 0)
830 SendBodyWithAlternateViews (message, boundary, attachmentExists);
832 SendSimpleBody (message);
836 private void SendWithAttachments (MailMessage message) {
837 string boundary = GenerateBoundary ();
839 // first "multipart/mixed"
840 ContentType messageContentType = new ContentType ();
841 messageContentType.Boundary = boundary;
842 messageContentType.MediaType = "multipart/mixed";
843 messageContentType.CharSet = null;
845 SendHeader (HeaderName.ContentType, messageContentType.ToString ());
846 SendData (String.Empty);
849 Attachment body = null;
851 if (message.AlternateViews.Count > 0)
852 SendWithoutAttachments (message, boundary, true);
854 body = Attachment.CreateAttachmentFromString (message.Body, null, message.BodyEncoding, message.IsBodyHtml ? "text/html" : "text/plain");
855 message.Attachments.Insert (0, body);
859 SendAttachments (message, body, boundary);
862 message.Attachments.Remove (body);
865 EndSection (boundary);
868 private void SendBodyWithAlternateViews (MailMessage message, string boundary, bool attachmentExists)
870 AlternateViewCollection alternateViews = message.AlternateViews;
872 string inner_boundary = GenerateBoundary ();
874 ContentType messageContentType = new ContentType ();
875 messageContentType.Boundary = inner_boundary;
876 messageContentType.MediaType = "multipart/alternative";
878 if (!attachmentExists) {
879 SendHeader (HeaderName.ContentType, messageContentType.ToString ());
880 SendData (String.Empty);
884 AlternateView body = null;
885 if (message.Body != null) {
886 body = AlternateView.CreateAlternateViewFromString (message.Body, message.BodyEncoding, message.IsBodyHtml ? "text/html" : "text/plain");
887 alternateViews.Insert (0, body);
888 StartSection (boundary, messageContentType);
892 // alternate view sections
893 foreach (AlternateView av in alternateViews) {
895 string alt_boundary = null;
896 ContentType contentType;
897 if (av.LinkedResources.Count > 0) {
898 alt_boundary = GenerateBoundary ();
899 contentType = new ContentType ("multipart/related");
900 contentType.Boundary = alt_boundary;
902 contentType.Parameters ["type"] = av.ContentType.ToString ();
903 StartSection (inner_boundary, contentType);
904 StartSection (alt_boundary, av.ContentType, av.TransferEncoding);
906 contentType = new ContentType (av.ContentType.ToString ());
907 StartSection (inner_boundary, contentType, av.TransferEncoding);
910 switch (av.TransferEncoding) {
911 case TransferEncoding.Base64:
912 byte [] content = new byte [av.ContentStream.Length];
913 av.ContentStream.Read (content, 0, content.Length);
915 SendData (Convert.ToBase64String (content));
917 SendData (Convert.ToBase64String (content, Base64FormattingOptions.InsertLineBreaks));
920 case TransferEncoding.QuotedPrintable:
921 byte [] bytes = new byte [av.ContentStream.Length];
922 av.ContentStream.Read (bytes, 0, bytes.Length);
923 SendData (ToQuotedPrintable (bytes));
925 case TransferEncoding.SevenBit:
926 case TransferEncoding.Unknown:
927 content = new byte [av.ContentStream.Length];
928 av.ContentStream.Read (content, 0, content.Length);
929 SendData (Encoding.ASCII.GetString (content));
933 if (av.LinkedResources.Count > 0) {
934 SendLinkedResources (message, av.LinkedResources, alt_boundary);
935 EndSection (alt_boundary);
938 if (!attachmentExists)
939 SendData (string.Empty);
944 alternateViews.Remove (body);
946 EndSection (inner_boundary);
949 private void SendLinkedResources (MailMessage message, LinkedResourceCollection resources, string boundary)
951 foreach (LinkedResource lr in resources) {
952 StartSection (boundary, lr.ContentType, lr.TransferEncoding, lr);
954 switch (lr.TransferEncoding) {
955 case TransferEncoding.Base64:
956 byte [] content = new byte [lr.ContentStream.Length];
957 lr.ContentStream.Read (content, 0, content.Length);
959 SendData (Convert.ToBase64String (content));
961 SendData (Convert.ToBase64String (content, Base64FormattingOptions.InsertLineBreaks));
964 case TransferEncoding.QuotedPrintable:
965 byte [] bytes = new byte [lr.ContentStream.Length];
966 lr.ContentStream.Read (bytes, 0, bytes.Length);
967 SendData (ToQuotedPrintable (bytes));
969 case TransferEncoding.SevenBit:
970 case TransferEncoding.Unknown:
971 content = new byte [lr.ContentStream.Length];
972 lr.ContentStream.Read (content, 0, content.Length);
973 SendData (Encoding.ASCII.GetString (content));
979 private void SendAttachments (MailMessage message, Attachment body, string boundary) {
980 foreach (Attachment att in message.Attachments) {
981 ContentType contentType = new ContentType (att.ContentType.ToString ());
982 if (att.Name != null) {
983 contentType.Name = att.Name;
984 if (att.NameEncoding != null)
985 contentType.CharSet = att.NameEncoding.HeaderName;
986 att.ContentDisposition.FileName = att.Name;
988 StartSection (boundary, contentType, att.TransferEncoding, att == body ? null : att.ContentDisposition);
990 byte [] content = new byte [att.ContentStream.Length];
991 att.ContentStream.Read (content, 0, content.Length);
992 switch (att.TransferEncoding) {
993 case TransferEncoding.Base64:
995 SendData (Convert.ToBase64String (content));
997 SendData (Convert.ToBase64String (content, Base64FormattingOptions.InsertLineBreaks));
1000 case TransferEncoding.QuotedPrintable:
1001 SendData (ToQuotedPrintable (content));
1003 case TransferEncoding.SevenBit:
1004 case TransferEncoding.Unknown:
1005 SendData (Encoding.ASCII.GetString (content));
1009 SendData (string.Empty);
1013 private SmtpResponse SendCommand (string command)
1015 writer.Write (command);
1016 // Certain SMTP servers will reject mail sent with unix line-endings; see http://cr.yp.to/docs/smtplf.html
1017 writer.Write ("\r\n");
1022 private void SendHeader (string name, string value)
1024 SendData (String.Format ("{0}: {1}", name, value));
1027 private void StartSection (string section, ContentType sectionContentType)
1029 SendData (String.Format ("--{0}", section));
1030 SendHeader ("content-type", sectionContentType.ToString ());
1031 SendData (string.Empty);
1034 private void StartSection (string section, ContentType sectionContentType,TransferEncoding transferEncoding)
1036 SendData (String.Format ("--{0}", section));
1037 SendHeader ("content-type", sectionContentType.ToString ());
1038 SendHeader ("content-transfer-encoding", GetTransferEncodingName (transferEncoding));
1039 SendData (string.Empty);
1042 private void StartSection(string section, ContentType sectionContentType, TransferEncoding transferEncoding, LinkedResource lr)
1044 SendData (String.Format("--{0}", section));
1045 SendHeader ("content-type", sectionContentType.ToString ());
1046 SendHeader ("content-transfer-encoding", GetTransferEncodingName (transferEncoding));
1048 if (lr.ContentId != null && lr.ContentId.Length > 0)
1049 SendHeader("content-ID", "<" + lr.ContentId + ">");
1051 SendData (string.Empty);
1054 private void StartSection (string section, ContentType sectionContentType, TransferEncoding transferEncoding, ContentDisposition contentDisposition) {
1055 SendData (String.Format ("--{0}", section));
1056 SendHeader ("content-type", sectionContentType.ToString ());
1057 SendHeader ("content-transfer-encoding", GetTransferEncodingName (transferEncoding));
1058 if (contentDisposition != null)
1059 SendHeader ("content-disposition", contentDisposition.ToString ());
1060 SendData (string.Empty);
1063 // use proper encoding to escape input
1064 private string ToQuotedPrintable (string input, Encoding enc)
1066 byte [] bytes = enc.GetBytes (input);
1067 return ToQuotedPrintable (bytes);
1070 private string ToQuotedPrintable (byte [] bytes)
1072 StringWriter writer = new StringWriter ();
1073 int charsInLine = 0;
1075 StringBuilder sb = new StringBuilder("=", 3);
1076 byte equalSign = (byte)'=';
1079 foreach (byte i in bytes) {
1080 if (i > 127 || i == equalSign) {
1082 sb.Append(Convert.ToString (i, 16).ToUpperInvariant ());
1085 c = Convert.ToChar (i);
1086 if (c == '\r' || c == '\n') {
1094 charsInLine += curLen;
1095 if (charsInLine > 75) {
1096 writer.Write ("=\r\n");
1097 charsInLine = curLen;
1102 writer.Write (sb.ToString ());
1105 return writer.ToString ();
1107 private static string GetTransferEncodingName (TransferEncoding encoding)
1110 case TransferEncoding.QuotedPrintable:
1111 return "quoted-printable";
1112 case TransferEncoding.SevenBit:
1114 case TransferEncoding.Base64:
1121 RemoteCertificateValidationCallback callback = delegate (object sender,
1122 X509Certificate certificate,
1124 SslPolicyErrors sslPolicyErrors) {
1125 // honor any exciting callback defined on ServicePointManager
1126 if (ServicePointManager.ServerCertificateValidationCallback != null)
1127 return ServicePointManager.ServerCertificateValidationCallback (sender, certificate, chain, sslPolicyErrors);
1128 // otherwise provide our own
1129 if (sslPolicyErrors != SslPolicyErrors.None)
1130 throw new InvalidOperationException ("SSL authentication error: " + sslPolicyErrors);
1135 private void InitiateSecureConnection () {
1136 SmtpResponse response = SendCommand ("STARTTLS");
1138 if (IsError (response)) {
1139 throw new SmtpException (SmtpStatusCode.GeneralFailure, "Server does not support secure connections.");
1143 ((NetworkStream) stream).ChangeToSSLSocket ();
1145 SslStream sslStream = new SslStream (stream, false, callback, null);
1146 CheckCancellation ();
1147 sslStream.AuthenticateAsClient (Host, this.ClientCertificates, SslProtocols.Default, false);
1151 throw new SystemException ("You are using an incomplete System.dll build");
1155 void Authenticate ()
1157 string user = null, pass = null;
1159 if (UseDefaultCredentials) {
1160 user = CredentialCache.DefaultCredentials.GetCredential (new System.Uri ("smtp://" + host), "basic").UserName;
1161 pass = CredentialCache.DefaultCredentials.GetCredential (new System.Uri ("smtp://" + host), "basic").Password;
1162 } else if (Credentials != null) {
1163 user = Credentials.GetCredential (host, port, "smtp").UserName;
1164 pass = Credentials.GetCredential (host, port, "smtp").Password;
1169 Authenticate (user, pass);
1172 void CheckStatus (SmtpResponse status, int i)
1174 if (((int) status.StatusCode) != i)
1175 throw new SmtpException (status.StatusCode, status.Description);
1178 void ThrowIfError (SmtpResponse status)
1180 if (IsError (status))
1181 throw new SmtpException (status.StatusCode, status.Description);
1184 void Authenticate (string user, string password)
1186 if (authMechs == AuthMechs.None)
1189 SmtpResponse status;
1191 if ((authMechs & AuthMechs.DigestMD5) != 0) {
1192 status = SendCommand ("AUTH DIGEST-MD5");
1193 CheckStatus (status, 334);
1194 string challenge = Encoding.ASCII.GetString (Convert.FromBase64String (status.Description.Substring (4)));
1195 Console.WriteLine ("CHALLENGE: {0}", challenge);
1196 DigestSession session = new DigestSession ();
1197 session.Parse (false, challenge);
1198 string response = session.Authenticate (this, user, password);
1199 status = SendCommand (Convert.ToBase64String (Encoding.UTF8.GetBytes (response)));
1200 CheckStatus (status, 235);
1202 if ((authMechs & AuthMechs.Login) != 0) {
1203 status = SendCommand ("AUTH LOGIN");
1204 CheckStatus (status, 334);
1205 status = SendCommand (Convert.ToBase64String (Encoding.UTF8.GetBytes (user)));
1206 CheckStatus (status, 334);
1207 status = SendCommand (Convert.ToBase64String (Encoding.UTF8.GetBytes (password)));
1208 CheckStatus (status, 235);
1209 } else if ((authMechs & AuthMechs.Plain) != 0) {
1210 string s = String.Format ("\0{0}\0{1}", user, password);
1211 s = Convert.ToBase64String (Encoding.UTF8.GetBytes (s));
1212 status = SendCommand ("AUTH PLAIN " + s);
1213 CheckStatus (status, 235);
1215 throw new SmtpException ("AUTH types PLAIN, LOGIN not supported by the server");
1219 #endregion // Methods
1221 // The HeaderName struct is used to store constant string values representing mail headers.
1222 private struct HeaderName {
1223 public const string ContentTransferEncoding = "Content-Transfer-Encoding";
1224 public const string ContentType = "Content-Type";
1225 public const string Bcc = "Bcc";
1226 public const string Cc = "Cc";
1227 public const string From = "From";
1228 public const string Subject = "Subject";
1229 public const string To = "To";
1230 public const string MimeVersion = "MIME-Version";
1231 public const string MessageId = "Message-ID";
1232 public const string Priority = "Priority";
1233 public const string Importance = "Importance";
1234 public const string XPriority = "X-Priority";
1235 public const string Date = "Date";
1238 // This object encapsulates the status code and description of an SMTP response.
1239 private struct SmtpResponse {
1240 public SmtpStatusCode StatusCode;
1241 public string Description;
1243 public static SmtpResponse Parse (string line) {
1244 SmtpResponse response = new SmtpResponse ();
1246 if (line.Length < 4)
1247 throw new SmtpException ("Response is to short " +
1250 if ((line [3] != ' ') && (line [3] != '-'))
1251 throw new SmtpException ("Response format is wrong.(" +
1254 // parse the response code
1255 response.StatusCode = (SmtpStatusCode) Int32.Parse (line.Substring (0, 3));
1257 // set the raw response
1258 response.Description = line;
1265 class CCredentialsByHost : ICredentialsByHost
1267 public CCredentialsByHost (string userName, string password) {
1268 this.userName = userName;
1269 this.password = password;
1272 public NetworkCredential GetCredential (string host, int port, string authenticationType) {
1273 return new NetworkCredential (userName, password);
1276 private string userName;
1277 private string password;