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 bool useDefaultCredentials = false;
68 string pickupDirectoryLocation;
69 SmtpDeliveryMethod deliveryMethod;
72 X509CertificateCollection clientCertificates;
80 MailAddress defaultFrom;
82 MailMessage messageInProcess;
84 BackgroundWorker worker;
85 object user_async_state;
99 class CancellationException : Exception
103 AuthMechs authMechs = AuthMechs.None;
106 Mutex mutex = new Mutex ();
117 public SmtpClient (string host)
122 public SmtpClient (string host, int port) {
123 #if CONFIGURATION_DEP
124 SmtpSection cfg = (SmtpSection) ConfigurationManager.GetSection ("system.net/mailSettings/smtp");
127 this.host = cfg.Network.Host;
128 this.port = cfg.Network.Port;
129 if (cfg.Network.UserName != null) {
130 string password = String.Empty;
132 if (cfg.Network.Password != null)
133 password = cfg.Network.Password;
135 Credentials = new CCredentialsByHost (cfg.Network.UserName, password);
138 if (cfg.From != null)
139 defaultFrom = new MailAddress (cfg.From);
143 if (!String.IsNullOrEmpty (host))
150 #endregion // Constructors
155 [MonoTODO("STARTTLS is not supported yet")]
156 public X509CertificateCollection ClientCertificates {
158 if (clientCertificates == null)
159 clientCertificates = new X509CertificateCollection ();
160 return clientCertificates;
165 public ICredentialsByHost Credentials {
166 get { return credentials; }
173 public SmtpDeliveryMethod DeliveryMethod {
174 get { return deliveryMethod; }
177 deliveryMethod = value;
181 [MonoTODO("STARTTLS is not supported yet")]
182 public bool EnableSsl {
183 // FIXME: So... is this supposed to enable SSL port functionality? or STARTTLS? Or both?
184 get { return enableSsl; }
195 throw new ArgumentNullException ("value");
196 if (value.Length == 0)
197 throw new ArgumentException ("An empty string is not allowed.", "value");
203 public string PickupDirectoryLocation {
204 get { return pickupDirectoryLocation; }
205 set { pickupDirectoryLocation = value; }
212 throw new ArgumentOutOfRangeException ("value");
219 public ServicePoint ServicePoint {
220 get { throw new NotImplementedException (); }
224 get { return timeout; }
227 throw new ArgumentOutOfRangeException ("value");
233 public bool UseDefaultCredentials {
234 get { return useDefaultCredentials; }
235 [MonoNotSupported ("no DefaultCredential support in Mono")]
238 throw new NotImplementedException ("Default credentials are not supported");
243 #endregion // Properties
247 public event SendCompletedEventHandler SendCompleted;
253 private void CheckState ()
255 if (messageInProcess != null)
256 throw new InvalidOperationException ("Cannot set Timeout while Sending a message");
259 private static string EncodeAddress(MailAddress address)
261 string encodedDisplayName = ContentType.EncodeSubjectRFC2047 (address.DisplayName, Encoding.UTF8);
262 return "\"" + encodedDisplayName + "\" <" + address.Address + ">";
265 private static string EncodeAddresses(MailAddressCollection addresses)
267 StringBuilder sb = new StringBuilder();
269 foreach (MailAddress address in addresses) {
273 sb.Append(EncodeAddress(address));
276 return sb.ToString();
279 private string EncodeSubjectRFC2047 (MailMessage message)
281 return ContentType.EncodeSubjectRFC2047 (message.Subject, message.SubjectEncoding);
284 private string EncodeBody (MailMessage message)
286 string body = message.Body;
287 Encoding encoding = message.BodyEncoding;
289 switch (message.ContentTransferEncoding) {
290 case TransferEncoding.SevenBit:
292 case TransferEncoding.Base64:
293 return Convert.ToBase64String (encoding.GetBytes (body), Base64FormattingOptions.InsertLineBreaks);
295 return ToQuotedPrintable (body, encoding);
299 private string EncodeBody (AlternateView av)
301 //Encoding encoding = av.ContentType.CharSet != null ? Encoding.GetEncoding (av.ContentType.CharSet) : Encoding.UTF8;
303 byte [] bytes = new byte [av.ContentStream.Length];
304 av.ContentStream.Read (bytes, 0, bytes.Length);
307 switch (av.TransferEncoding) {
308 case TransferEncoding.SevenBit:
309 return Encoding.ASCII.GetString (bytes);
310 case TransferEncoding.Base64:
311 return Convert.ToBase64String (bytes, Base64FormattingOptions.InsertLineBreaks);
313 return ToQuotedPrintable (bytes);
318 private void EndSection (string section)
320 SendData (String.Format ("--{0}--", section));
321 SendData (string.Empty);
324 private string GenerateBoundary ()
326 string output = GenerateBoundary (boundaryIndex);
331 private static string GenerateBoundary (int index)
333 return String.Format ("--boundary_{0}_{1}", index, Guid.NewGuid ().ToString ("D"));
336 private bool IsError (SmtpResponse status)
338 return ((int) status.StatusCode) >= 400;
341 protected void OnSendCompleted (AsyncCompletedEventArgs e)
344 if (SendCompleted != null)
345 SendCompleted (this, e);
348 user_async_state = null;
352 private void CheckCancellation ()
354 if (worker != null && worker.CancellationPending)
355 throw new CancellationException ();
358 private SmtpResponse Read () {
359 byte [] buffer = new byte [512];
361 bool lastLine = false;
364 CheckCancellation ();
366 int readLength = stream.Read (buffer, position, buffer.Length - position);
367 if (readLength > 0) {
368 int available = position + readLength - 1;
369 if (available > 4 && (buffer [available] == '\n' || buffer [available] == '\r'))
370 for (int index = available - 3; ; index--) {
371 if (index < 0 || buffer [index] == '\n' || buffer [index] == '\r') {
372 lastLine = buffer [index + 4] == ' ';
378 position += readLength;
380 // check if buffer is full
381 if (position == buffer.Length) {
382 byte [] newBuffer = new byte [buffer.Length * 2];
383 Array.Copy (buffer, 0, newBuffer, 0, buffer.Length);
393 Encoding encoding = new ASCIIEncoding ();
395 string line = encoding.GetString (buffer, 0, position - 1);
397 // parse the line to the lastResponse object
398 SmtpResponse response = SmtpResponse.Parse (line);
402 throw new System.IO.IOException ("Connection closed");
406 void ResetExtensions()
408 // canStartTLS = false;
409 authMechs = AuthMechs.None;
412 void ParseExtensions (string extens)
414 char[] delims = new char [1] { ' ' };
415 string[] parts = extens.Split ('\n');
417 foreach (string part in parts) {
421 string start = part.Substring (4);
422 if (start.StartsWith ("AUTH ", StringComparison.Ordinal)) {
423 string[] options = start.Split (delims);
424 for (int k = 1; k < options.Length; k++) {
425 string option = options[k].Trim();
428 authMechs |= AuthMechs.CramMD5;
431 authMechs |= AuthMechs.DigestMD5;
434 authMechs |= AuthMechs.GssAPI;
437 authMechs |= AuthMechs.Kerberos4;
440 authMechs |= AuthMechs.Login;
443 authMechs |= AuthMechs.Plain;
447 } else if (start.StartsWith ("STARTTLS", StringComparison.Ordinal)) {
448 // canStartTLS = true;
453 public void Send (MailMessage message)
456 throw new ArgumentNullException ("message");
458 if (deliveryMethod == SmtpDeliveryMethod.Network && (Host == null || Host.Trim ().Length == 0))
459 throw new InvalidOperationException ("The SMTP host was not specified");
460 else if (deliveryMethod == SmtpDeliveryMethod.PickupDirectoryFromIis)
461 throw new NotSupportedException("IIS delivery is not supported");
466 // Block while sending
469 messageInProcess = message;
470 if (deliveryMethod == SmtpDeliveryMethod.SpecifiedPickupDirectory)
471 SendToFile (message);
473 SendInternal (message);
474 } catch (CancellationException) {
475 // This exception is introduced for convenient cancellation process.
476 } catch (SmtpException) {
478 } catch (Exception ex) {
479 throw new SmtpException ("Message could not be sent.", ex);
481 // Release the mutex to allow other threads access
482 mutex.ReleaseMutex ();
483 messageInProcess = null;
487 private void SendInternal (MailMessage message)
489 CheckCancellation ();
492 client = new TcpClient (host, port);
493 stream = client.GetStream ();
494 // FIXME: this StreamWriter creation is bogus.
495 // It expects as if a Stream were able to switch to SSL
496 // mode (such behavior is only in Mainsoft Socket API).
497 writer = new StreamWriter (stream);
498 reader = new StreamReader (stream);
513 // FIXME: simple implementation, could be brushed up.
514 private void SendToFile (MailMessage message)
516 if (!Path.IsPathRooted (pickupDirectoryLocation))
517 throw new SmtpException("Only absolute directories are allowed for pickup directory.");
519 string filename = Path.Combine (pickupDirectoryLocation,
520 Guid.NewGuid() + ".eml");
523 writer = new StreamWriter(filename);
525 MailAddress from = message.From;
530 SendHeader (HeaderName.Date, DateTime.Now.ToString ("ddd, dd MMM yyyy HH':'mm':'ss zzz", DateTimeFormatInfo.InvariantInfo));
531 SendHeader (HeaderName.From, from.ToString ());
532 SendHeader (HeaderName.To, message.To.ToString ());
533 if (message.CC.Count > 0)
534 SendHeader (HeaderName.Cc, message.CC.ToString ());
535 SendHeader (HeaderName.Subject, EncodeSubjectRFC2047 (message));
537 foreach (string s in message.Headers.AllKeys)
538 SendHeader (s, message.Headers [s]);
540 AddPriorityHeader (message);
543 if (message.Attachments.Count > 0)
544 SendWithAttachments (message);
546 SendWithoutAttachments (message, null, false);
550 if (writer != null) writer.Close(); writer = null;
554 private void SendCore (MailMessage message)
559 if (IsError (status))
560 throw new SmtpException (status.StatusCode, status.Description);
564 // FIXME: parse the list of extensions so we don't bother wasting
565 // our time trying commands if they aren't supported.
566 status = SendCommand ("EHLO " + Dns.GetHostName ());
568 if (IsError (status)) {
569 status = SendCommand ("HELO " + Dns.GetHostName ());
571 if (IsError (status))
572 throw new SmtpException (status.StatusCode, status.Description);
574 // Parse ESMTP extensions
575 string extens = status.Description;
578 ParseExtensions (extens);
583 // FIXME: I get the feeling from the docs that EnableSsl is meant
584 // for using the SSL-port and not STARTTLS (or, if it includes
585 // STARTTLS... only use STARTTLS if the SSL-type in the certificate
586 // is TLS and not SSLv2 or SSLv3)
588 // FIXME: even tho we have a canStartTLS flag... ignore it for now
589 // so that the STARTTLS command can throw the appropriate
590 // SmtpException if STARTTLS is unavailable
592 // SmtpClient implements STARTTLS support.
594 InitiateSecureConnection ();
596 writer = new StreamWriter (stream);
597 reader = new StreamReader (stream);
598 status = SendCommand ("EHLO " + Dns.GetHostName ());
600 if (IsError (status)) {
601 status = SendCommand ("HELO " + Dns.GetHostName ());
603 if (IsError (status))
604 throw new SmtpException (status.StatusCode, status.Description);
606 // Parse ESMTP extensions
607 string extens = status.Description;
609 ParseExtensions (extens);
613 if (authMechs != AuthMechs.None)
616 MailAddress from = message.From;
622 status = SendCommand ("MAIL FROM:<" + from.Address + '>');
623 if (IsError (status)) {
624 throw new SmtpException (status.StatusCode, status.Description);
627 // Send RCPT TO: for all recipients
628 List<SmtpFailedRecipientException> sfre = new List<SmtpFailedRecipientException> ();
630 for (int i = 0; i < message.To.Count; i ++) {
631 status = SendCommand ("RCPT TO:<" + message.To [i].Address + '>');
632 if (IsError (status))
633 sfre.Add (new SmtpFailedRecipientException (status.StatusCode, message.To [i].Address));
635 for (int i = 0; i < message.CC.Count; i ++) {
636 status = SendCommand ("RCPT TO:<" + message.CC [i].Address + '>');
637 if (IsError (status))
638 sfre.Add (new SmtpFailedRecipientException (status.StatusCode, message.CC [i].Address));
640 for (int i = 0; i < message.Bcc.Count; i ++) {
641 status = SendCommand ("RCPT TO:<" + message.Bcc [i].Address + '>');
642 if (IsError (status))
643 sfre.Add (new SmtpFailedRecipientException (status.StatusCode, message.Bcc [i].Address));
646 #if TARGET_JVM // List<T>.ToArray () is not supported
647 if (sfre.Count > 0) {
648 SmtpFailedRecipientException[] xs = new SmtpFailedRecipientException[sfre.Count];
650 throw new SmtpFailedRecipientsException ("failed recipients", xs);
654 throw new SmtpFailedRecipientsException ("failed recipients", sfre.ToArray ());
658 status = SendCommand ("DATA");
659 if (IsError (status))
660 throw new SmtpException (status.StatusCode, status.Description);
662 // Send message headers
663 string dt = DateTime.Now.ToString ("ddd, dd MMM yyyy HH':'mm':'ss zzz", DateTimeFormatInfo.InvariantInfo);
664 // remove ':' from time zone offset (e.g. from "+01:00")
665 dt = dt.Remove (dt.Length - 3, 1);
666 SendHeader (HeaderName.Date, dt);
668 SendHeader (HeaderName.From, EncodeAddress (from));
669 SendHeader (HeaderName.To, EncodeAddresses (message.To));
670 if (message.CC.Count > 0)
671 SendHeader (HeaderName.Cc, EncodeAddresses (message.CC));
672 SendHeader (HeaderName.Subject, EncodeSubjectRFC2047 (message));
676 switch (message.Priority){
677 case MailPriority.Normal:
681 case MailPriority.Low:
685 case MailPriority.High:
689 SendHeader ("Priority", v);
690 if (message.Sender != null)
691 SendHeader ("Sender", EncodeAddress (message.Sender));
692 if (message.ReplyTo != null)
693 SendHeader ("ReplyTo", EncodeAddress (message.ReplyTo));
695 foreach (string s in message.Headers.AllKeys)
696 SendHeader (s, message.Headers [s]);
698 AddPriorityHeader (message);
701 if (message.Attachments.Count > 0)
702 SendWithAttachments (message);
704 SendWithoutAttachments (message, null, false);
709 if (IsError (status))
710 throw new SmtpException (status.StatusCode, status.Description);
713 status = SendCommand ("QUIT");
714 } catch (System.IO.IOException) {
715 // We excuse server for the rude connection closing as a response to QUIT
719 public void Send (string from, string to, string subject, string body)
721 Send (new MailMessage (from, to, subject, body));
724 private void SendData (string data)
726 CheckCancellation ();
728 // Certain SMTP servers will reject mail sent with unix line-endings; see http://cr.yp.to/docs/smtplf.html
729 StringBuilder sb = new StringBuilder (data);
730 sb.Replace ("\r\n", "\n");
731 sb.Replace ('\r', '\n');
732 sb.Replace ("\n", "\r\n");
733 writer.Write (sb.ToString ());
734 writer.Write ("\r\n");
738 public void SendAsync (MailMessage message, object userToken)
741 throw new InvalidOperationException ("Another SendAsync operation is in progress");
743 worker = new BackgroundWorker ();
744 worker.DoWork += delegate (object o, DoWorkEventArgs ea) {
746 user_async_state = ea.Argument;
748 } catch (Exception ex) {
752 worker.WorkerSupportsCancellation = true;
753 worker.RunWorkerCompleted += delegate (object o, RunWorkerCompletedEventArgs ea) {
754 // Note that RunWorkerCompletedEventArgs.UserState cannot be used (LAMESPEC)
755 OnSendCompleted (new AsyncCompletedEventArgs (ea.Error, ea.Cancelled, user_async_state));
757 worker.RunWorkerAsync (userToken);
760 public void SendAsync (string from, string to, string subject, string body, object userToken)
762 SendAsync (new MailMessage (from, to, subject, body), userToken);
765 public void SendAsyncCancel ()
768 throw new InvalidOperationException ("SendAsync operation is not in progress");
769 worker.CancelAsync ();
772 private void AddPriorityHeader (MailMessage message) {
773 switch (message.Priority) {
774 case MailPriority.High:
775 SendHeader (HeaderName.Priority, "Urgent");
776 SendHeader (HeaderName.Importance, "high");
777 SendHeader (HeaderName.XPriority, "1");
779 case MailPriority.Low:
780 SendHeader (HeaderName.Priority, "Non-Urgent");
781 SendHeader (HeaderName.Importance, "low");
782 SendHeader (HeaderName.XPriority, "5");
787 private void SendSimpleBody (MailMessage message) {
788 SendHeader (HeaderName.ContentType, message.BodyContentType.ToString ());
789 if (message.ContentTransferEncoding != TransferEncoding.SevenBit)
790 SendHeader (HeaderName.ContentTransferEncoding, GetTransferEncodingName (message.ContentTransferEncoding));
791 SendData (string.Empty);
793 SendData (EncodeBody (message));
796 private void SendBodylessSingleAlternate (AlternateView av) {
797 SendHeader (HeaderName.ContentType, av.ContentType.ToString ());
798 if (av.TransferEncoding != TransferEncoding.SevenBit)
799 SendHeader (HeaderName.ContentTransferEncoding, GetTransferEncodingName (av.TransferEncoding));
800 SendData (string.Empty);
802 SendData (EncodeBody (av));
805 private void SendWithoutAttachments (MailMessage message, string boundary, bool attachmentExists)
807 if (message.Body == null && message.AlternateViews.Count == 1)
808 SendBodylessSingleAlternate (message.AlternateViews [0]);
809 else if (message.AlternateViews.Count > 0)
810 SendBodyWithAlternateViews (message, boundary, attachmentExists);
812 SendSimpleBody (message);
816 private void SendWithAttachments (MailMessage message) {
817 string boundary = GenerateBoundary ();
819 // first "multipart/mixed"
820 ContentType messageContentType = new ContentType ();
821 messageContentType.Boundary = boundary;
822 messageContentType.MediaType = "multipart/mixed";
823 messageContentType.CharSet = null;
825 SendHeader (HeaderName.ContentType, messageContentType.ToString ());
826 SendData (String.Empty);
829 Attachment body = null;
831 if (message.AlternateViews.Count > 0)
832 SendWithoutAttachments (message, boundary, true);
834 body = Attachment.CreateAttachmentFromString (message.Body, null, message.BodyEncoding, message.IsBodyHtml ? "text/html" : "text/plain");
835 message.Attachments.Insert (0, body);
839 SendAttachments (message, body, boundary);
842 message.Attachments.Remove (body);
845 EndSection (boundary);
848 private void SendBodyWithAlternateViews (MailMessage message, string boundary, bool attachmentExists)
850 AlternateViewCollection alternateViews = message.AlternateViews;
852 string inner_boundary = GenerateBoundary ();
854 ContentType messageContentType = new ContentType ();
855 messageContentType.Boundary = inner_boundary;
856 messageContentType.MediaType = "multipart/alternative";
858 if (!attachmentExists) {
859 SendHeader (HeaderName.ContentType, messageContentType.ToString ());
860 SendData (String.Empty);
864 AlternateView body = null;
865 if (message.Body != null) {
866 body = AlternateView.CreateAlternateViewFromString (message.Body, message.BodyEncoding, message.IsBodyHtml ? "text/html" : "text/plain");
867 alternateViews.Insert (0, body);
868 StartSection (boundary, messageContentType);
872 // alternate view sections
873 foreach (AlternateView av in alternateViews) {
875 string alt_boundary = null;
876 ContentType contentType;
877 if (av.LinkedResources.Count > 0) {
878 alt_boundary = GenerateBoundary ();
879 contentType = new ContentType ("multipart/related");
880 contentType.Boundary = alt_boundary;
881 contentType.Parameters ["type"] = "application/octet-stream";
882 StartSection (inner_boundary, contentType);
884 contentType = new ContentType (av.ContentType.ToString ());
885 StartSection (inner_boundary, contentType, av.TransferEncoding);
888 switch (av.TransferEncoding) {
889 case TransferEncoding.Base64:
890 byte [] content = new byte [av.ContentStream.Length];
891 av.ContentStream.Read (content, 0, content.Length);
893 SendData (Convert.ToBase64String (content));
895 SendData (Convert.ToBase64String (content, Base64FormattingOptions.InsertLineBreaks));
898 case TransferEncoding.QuotedPrintable:
899 byte [] bytes = new byte [av.ContentStream.Length];
900 av.ContentStream.Read (bytes, 0, bytes.Length);
901 SendData (ToQuotedPrintable (bytes));
903 case TransferEncoding.SevenBit:
904 case TransferEncoding.Unknown:
905 content = new byte [av.ContentStream.Length];
906 av.ContentStream.Read (content, 0, content.Length);
907 SendData (Encoding.ASCII.GetString (content));
911 if (av.LinkedResources.Count > 0) {
912 SendLinkedResources (message, av.LinkedResources, alt_boundary);
913 EndSection (alt_boundary);
916 if (!attachmentExists)
917 SendData (string.Empty);
922 alternateViews.Remove (body);
924 EndSection (inner_boundary);
927 private void SendLinkedResources (MailMessage message, LinkedResourceCollection resources, string boundary)
929 foreach (LinkedResource lr in resources) {
930 ContentType contentType = new ContentType (lr.ContentType.ToString ());
931 StartSection (boundary, contentType, lr.TransferEncoding);
933 switch (lr.TransferEncoding) {
934 case TransferEncoding.Base64:
935 byte [] content = new byte [lr.ContentStream.Length];
936 lr.ContentStream.Read (content, 0, content.Length);
938 SendData (Convert.ToBase64String (content));
940 SendData (Convert.ToBase64String (content, Base64FormattingOptions.InsertLineBreaks));
943 case TransferEncoding.QuotedPrintable:
944 byte [] bytes = new byte [lr.ContentStream.Length];
945 lr.ContentStream.Read (bytes, 0, bytes.Length);
946 SendData (ToQuotedPrintable (bytes));
948 case TransferEncoding.SevenBit:
949 case TransferEncoding.Unknown:
950 content = new byte [lr.ContentStream.Length];
951 lr.ContentStream.Read (content, 0, content.Length);
952 SendData (Encoding.ASCII.GetString (content));
958 private void SendAttachments (MailMessage message, Attachment body, string boundary) {
959 foreach (Attachment att in message.Attachments) {
960 ContentType contentType = new ContentType (att.ContentType.ToString ());
961 if (att.Name != null) {
962 contentType.Name = att.Name;
963 if (att.NameEncoding != null)
964 contentType.CharSet = att.NameEncoding.HeaderName;
965 att.ContentDisposition.FileName = att.Name;
967 StartSection (boundary, contentType, att.TransferEncoding, att == body ? null : att.ContentDisposition);
969 byte [] content = new byte [att.ContentStream.Length];
970 att.ContentStream.Read (content, 0, content.Length);
971 switch (att.TransferEncoding) {
972 case TransferEncoding.Base64:
974 SendData (Convert.ToBase64String (content));
976 SendData (Convert.ToBase64String (content, Base64FormattingOptions.InsertLineBreaks));
979 case TransferEncoding.QuotedPrintable:
980 SendData (ToQuotedPrintable (content));
982 case TransferEncoding.SevenBit:
983 case TransferEncoding.Unknown:
984 SendData (Encoding.ASCII.GetString (content));
988 SendData (string.Empty);
992 private SmtpResponse SendCommand (string command)
994 writer.Write (command);
995 // Certain SMTP servers will reject mail sent with unix line-endings; see http://cr.yp.to/docs/smtplf.html
996 writer.Write ("\r\n");
1001 private void SendHeader (string name, string value)
1003 SendData (String.Format ("{0}: {1}", name, value));
1006 private void StartSection (string section, ContentType sectionContentType)
1008 SendData (String.Format ("--{0}", section));
1009 SendHeader ("content-type", sectionContentType.ToString ());
1010 SendData (string.Empty);
1013 private void StartSection (string section, ContentType sectionContentType,TransferEncoding transferEncoding)
1015 SendData (String.Format ("--{0}", section));
1016 SendHeader ("content-type", sectionContentType.ToString ());
1017 SendHeader ("content-transfer-encoding", GetTransferEncodingName (transferEncoding));
1018 SendData (string.Empty);
1021 private void StartSection (string section, ContentType sectionContentType, TransferEncoding transferEncoding, ContentDisposition contentDisposition) {
1022 SendData (String.Format ("--{0}", section));
1023 SendHeader ("content-type", sectionContentType.ToString ());
1024 SendHeader ("content-transfer-encoding", GetTransferEncodingName (transferEncoding));
1025 if (contentDisposition != null)
1026 SendHeader ("content-disposition", contentDisposition.ToString ());
1027 SendData (string.Empty);
1030 // use proper encoding to escape input
1031 private string ToQuotedPrintable (string input, Encoding enc)
1033 byte [] bytes = enc.GetBytes (input);
1034 return ToQuotedPrintable (bytes);
1037 private string ToQuotedPrintable (byte [] bytes)
1039 StringWriter writer = new StringWriter ();
1040 int charsInLine = 0;
1042 StringBuilder sb = new StringBuilder("=", 3);
1043 byte equalSign = (byte)'=';
1046 foreach (byte i in bytes) {
1047 if (i > 127 || i == equalSign) {
1049 sb.Append(Convert.ToString (i, 16).ToUpperInvariant ());
1052 c = Convert.ToChar (i);
1053 if (c == '\r' || c == '\n') {
1061 charsInLine += curLen;
1062 if (charsInLine > 75) {
1063 writer.Write ("=\r\n");
1064 charsInLine = curLen;
1069 writer.Write (sb.ToString ());
1072 return writer.ToString ();
1074 private static string GetTransferEncodingName (TransferEncoding encoding)
1077 case TransferEncoding.QuotedPrintable:
1078 return "quoted-printable";
1079 case TransferEncoding.SevenBit:
1081 case TransferEncoding.Base64:
1088 RemoteCertificateValidationCallback callback = delegate (object sender,
1089 X509Certificate certificate,
1091 SslPolicyErrors sslPolicyErrors) {
1092 if (sslPolicyErrors != SslPolicyErrors.None)
1093 throw new InvalidOperationException ("SSL authentication error: " + sslPolicyErrors);
1098 private void InitiateSecureConnection () {
1099 SmtpResponse response = SendCommand ("STARTTLS");
1101 if (IsError (response)) {
1102 throw new SmtpException (SmtpStatusCode.GeneralFailure, "Server does not support secure connections.");
1106 ((NetworkStream) stream).ChangeToSSLSocket ();
1108 SslStream sslStream = new SslStream (stream, false, callback, null);
1109 CheckCancellation ();
1110 sslStream.AuthenticateAsClient (Host, this.ClientCertificates, SslProtocols.Default, false);
1114 throw new SystemException ("You are using an incomplete System.dll build");
1118 void Authenticate ()
1120 string user = null, pass = null;
1122 if (UseDefaultCredentials) {
1123 user = CredentialCache.DefaultCredentials.GetCredential (new System.Uri ("smtp://" + host), "basic").UserName;
1124 pass = CredentialCache.DefaultCredentials.GetCredential (new System.Uri ("smtp://" + host), "basic").Password;
1125 } else if (Credentials != null) {
1126 user = Credentials.GetCredential (host, port, "smtp").UserName;
1127 pass = Credentials.GetCredential (host, port, "smtp").Password;
1132 Authenticate (user, pass);
1135 void Authenticate (string Username, string Password)
1137 // FIXME: use the proper AuthMech
1138 SmtpResponse status = SendCommand ("AUTH LOGIN");
1139 if (((int) status.StatusCode) != 334) {
1140 throw new SmtpException (status.StatusCode, status.Description);
1143 status = SendCommand (Convert.ToBase64String (Encoding.ASCII.GetBytes (Username)));
1144 if (((int) status.StatusCode) != 334) {
1145 throw new SmtpException (status.StatusCode, status.Description);
1148 status = SendCommand (Convert.ToBase64String (Encoding.ASCII.GetBytes (Password)));
1149 if (IsError (status)) {
1150 throw new SmtpException (status.StatusCode, status.Description);
1154 #endregion // Methods
1156 // The HeaderName struct is used to store constant string values representing mail headers.
1157 private struct HeaderName {
1158 public const string ContentTransferEncoding = "Content-Transfer-Encoding";
1159 public const string ContentType = "Content-Type";
1160 public const string Bcc = "Bcc";
1161 public const string Cc = "Cc";
1162 public const string From = "From";
1163 public const string Subject = "Subject";
1164 public const string To = "To";
1165 public const string MimeVersion = "MIME-Version";
1166 public const string MessageId = "Message-ID";
1167 public const string Priority = "Priority";
1168 public const string Importance = "Importance";
1169 public const string XPriority = "X-Priority";
1170 public const string Date = "Date";
1173 // This object encapsulates the status code and description of an SMTP response.
1174 private struct SmtpResponse {
1175 public SmtpStatusCode StatusCode;
1176 public string Description;
1178 public static SmtpResponse Parse (string line) {
1179 SmtpResponse response = new SmtpResponse ();
1181 if (line.Length < 4)
1182 throw new SmtpException ("Response is to short " +
1185 if ((line [3] != ' ') && (line [3] != '-'))
1186 throw new SmtpException ("Response format is wrong.(" +
1189 // parse the response code
1190 response.StatusCode = (SmtpStatusCode) Int32.Parse (line.Substring (0, 3));
1192 // set the raw response
1193 response.Description = line;
1200 class CCredentialsByHost : ICredentialsByHost
1202 public CCredentialsByHost (string userName, string password) {
1203 this.userName = userName;
1204 this.password = password;
1207 public NetworkCredential GetCredential (string host, int port, string authenticationType) {
1208 return new NetworkCredential (userName, password);
1211 private string userName;
1212 private string password;