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;
98 class CancellationException : Exception
102 AuthMechs authMechs = AuthMechs.None;
103 Mutex mutex = new Mutex ();
114 public SmtpClient (string host)
119 public SmtpClient (string host, int port) {
120 #if CONFIGURATION_DEP
121 SmtpSection cfg = (SmtpSection) ConfigurationManager.GetSection ("system.net/mailSettings/smtp");
124 this.host = cfg.Network.Host;
125 this.port = cfg.Network.Port;
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);
140 if (!String.IsNullOrEmpty (host))
147 #endregion // Constructors
152 [MonoTODO("Client certificates not used")]
153 public X509CertificateCollection ClientCertificates {
155 if (clientCertificates == null)
156 clientCertificates = new X509CertificateCollection ();
157 return clientCertificates;
162 public ICredentialsByHost Credentials {
163 get { return credentials; }
170 public SmtpDeliveryMethod DeliveryMethod {
171 get { return deliveryMethod; }
174 deliveryMethod = value;
178 public bool EnableSsl {
179 get { return enableSsl; }
190 throw new ArgumentNullException ("value");
191 if (value.Length == 0)
192 throw new ArgumentException ("An empty string is not allowed.", "value");
198 public string PickupDirectoryLocation {
199 get { return pickupDirectoryLocation; }
200 set { pickupDirectoryLocation = value; }
207 throw new ArgumentOutOfRangeException ("value");
214 public ServicePoint ServicePoint {
215 get { throw new NotImplementedException (); }
219 get { return timeout; }
222 throw new ArgumentOutOfRangeException ("value");
228 public bool UseDefaultCredentials {
229 get { return false; }
230 [MonoNotSupported ("no DefaultCredential support in Mono")]
233 throw new NotImplementedException ("Default credentials are not supported");
238 #endregion // Properties
242 public event SendCompletedEventHandler SendCompleted;
248 private void CheckState ()
250 if (messageInProcess != null)
251 throw new InvalidOperationException ("Cannot set Timeout while Sending a message");
254 private static string EncodeAddress(MailAddress address)
256 string encodedDisplayName = ContentType.EncodeSubjectRFC2047 (address.DisplayName, Encoding.UTF8);
257 return "\"" + encodedDisplayName + "\" <" + address.Address + ">";
260 private static string EncodeAddresses(MailAddressCollection addresses)
262 StringBuilder sb = new StringBuilder();
264 foreach (MailAddress address in addresses) {
268 sb.Append(EncodeAddress(address));
271 return sb.ToString();
274 private string EncodeSubjectRFC2047 (MailMessage message)
276 return ContentType.EncodeSubjectRFC2047 (message.Subject, message.SubjectEncoding);
279 private string EncodeBody (MailMessage message)
281 string body = message.Body;
282 Encoding encoding = message.BodyEncoding;
284 switch (message.ContentTransferEncoding) {
285 case TransferEncoding.SevenBit:
287 case TransferEncoding.Base64:
288 return Convert.ToBase64String (encoding.GetBytes (body), Base64FormattingOptions.InsertLineBreaks);
290 return ToQuotedPrintable (body, encoding);
294 private string EncodeBody (AlternateView av)
296 //Encoding encoding = av.ContentType.CharSet != null ? Encoding.GetEncoding (av.ContentType.CharSet) : Encoding.UTF8;
298 byte [] bytes = new byte [av.ContentStream.Length];
299 av.ContentStream.Read (bytes, 0, bytes.Length);
302 switch (av.TransferEncoding) {
303 case TransferEncoding.SevenBit:
304 return Encoding.ASCII.GetString (bytes);
305 case TransferEncoding.Base64:
306 return Convert.ToBase64String (bytes, Base64FormattingOptions.InsertLineBreaks);
308 return ToQuotedPrintable (bytes);
313 private void EndSection (string section)
315 SendData (String.Format ("--{0}--", section));
316 SendData (string.Empty);
319 private string GenerateBoundary ()
321 string output = GenerateBoundary (boundaryIndex);
326 private static string GenerateBoundary (int index)
328 return String.Format ("--boundary_{0}_{1}", index, Guid.NewGuid ().ToString ("D"));
331 private bool IsError (SmtpResponse status)
333 return ((int) status.StatusCode) >= 400;
336 protected void OnSendCompleted (AsyncCompletedEventArgs e)
339 if (SendCompleted != null)
340 SendCompleted (this, e);
343 user_async_state = null;
347 private void CheckCancellation ()
349 if (worker != null && worker.CancellationPending)
350 throw new CancellationException ();
353 private SmtpResponse Read () {
354 byte [] buffer = new byte [512];
356 bool lastLine = false;
359 CheckCancellation ();
361 int readLength = stream.Read (buffer, position, buffer.Length - position);
362 if (readLength > 0) {
363 int available = position + readLength - 1;
364 if (available > 4 && (buffer [available] == '\n' || buffer [available] == '\r'))
365 for (int index = available - 3; ; index--) {
366 if (index < 0 || buffer [index] == '\n' || buffer [index] == '\r') {
367 lastLine = buffer [index + 4] == ' ';
373 position += readLength;
375 // check if buffer is full
376 if (position == buffer.Length) {
377 byte [] newBuffer = new byte [buffer.Length * 2];
378 Array.Copy (buffer, 0, newBuffer, 0, buffer.Length);
388 Encoding encoding = new ASCIIEncoding ();
390 string line = encoding.GetString (buffer, 0, position - 1);
392 // parse the line to the lastResponse object
393 SmtpResponse response = SmtpResponse.Parse (line);
397 throw new System.IO.IOException ("Connection closed");
401 void ResetExtensions()
403 authMechs = AuthMechs.None;
406 void ParseExtensions (string extens)
408 char[] delims = new char [1] { ' ' };
409 string[] parts = extens.Split ('\n');
411 foreach (string part in parts) {
415 string start = part.Substring (4);
416 if (start.StartsWith ("AUTH ", StringComparison.Ordinal)) {
417 string[] options = start.Split (delims);
418 for (int k = 1; k < options.Length; k++) {
419 string option = options[k].Trim();
422 authMechs |= AuthMechs.CramMD5;
425 authMechs |= AuthMechs.DigestMD5;
428 authMechs |= AuthMechs.GssAPI;
431 authMechs |= AuthMechs.Kerberos4;
434 authMechs |= AuthMechs.Login;
437 authMechs |= AuthMechs.Plain;
445 public void Send (MailMessage message)
448 throw new ArgumentNullException ("message");
450 if (deliveryMethod == SmtpDeliveryMethod.Network && (Host == null || Host.Trim ().Length == 0))
451 throw new InvalidOperationException ("The SMTP host was not specified");
452 else if (deliveryMethod == SmtpDeliveryMethod.PickupDirectoryFromIis)
453 throw new NotSupportedException("IIS delivery is not supported");
458 // Block while sending
461 messageInProcess = message;
462 if (deliveryMethod == SmtpDeliveryMethod.SpecifiedPickupDirectory)
463 SendToFile (message);
465 SendInternal (message);
466 } catch (CancellationException) {
467 // This exception is introduced for convenient cancellation process.
468 } catch (SmtpException) {
470 } catch (Exception ex) {
471 throw new SmtpException ("Message could not be sent.", ex);
473 // Release the mutex to allow other threads access
474 mutex.ReleaseMutex ();
475 messageInProcess = null;
479 private void SendInternal (MailMessage message)
481 CheckCancellation ();
484 client = new TcpClient (host, port);
485 stream = client.GetStream ();
486 // FIXME: this StreamWriter creation is bogus.
487 // It expects as if a Stream were able to switch to SSL
488 // mode (such behavior is only in Mainsoft Socket API).
489 writer = new StreamWriter (stream);
490 reader = new StreamReader (stream);
505 // FIXME: simple implementation, could be brushed up.
506 private void SendToFile (MailMessage message)
508 if (!Path.IsPathRooted (pickupDirectoryLocation))
509 throw new SmtpException("Only absolute directories are allowed for pickup directory.");
511 string filename = Path.Combine (pickupDirectoryLocation,
512 Guid.NewGuid() + ".eml");
515 writer = new StreamWriter(filename);
517 MailAddress from = message.From;
522 SendHeader (HeaderName.Date, DateTime.Now.ToString ("ddd, dd MMM yyyy HH':'mm':'ss zzz", DateTimeFormatInfo.InvariantInfo));
523 SendHeader (HeaderName.From, from.ToString ());
524 SendHeader (HeaderName.To, message.To.ToString ());
525 if (message.CC.Count > 0)
526 SendHeader (HeaderName.Cc, message.CC.ToString ());
527 SendHeader (HeaderName.Subject, EncodeSubjectRFC2047 (message));
529 foreach (string s in message.Headers.AllKeys)
530 SendHeader (s, message.Headers [s]);
532 AddPriorityHeader (message);
535 if (message.Attachments.Count > 0)
536 SendWithAttachments (message);
538 SendWithoutAttachments (message, null, false);
542 if (writer != null) writer.Close(); writer = null;
546 private void SendCore (MailMessage message)
551 if (IsError (status))
552 throw new SmtpException (status.StatusCode, status.Description);
556 // FIXME: parse the list of extensions so we don't bother wasting
557 // our time trying commands if they aren't supported.
558 status = SendCommand ("EHLO " + Dns.GetHostName ());
560 if (IsError (status)) {
561 status = SendCommand ("HELO " + Dns.GetHostName ());
563 if (IsError (status))
564 throw new SmtpException (status.StatusCode, status.Description);
566 // Parse ESMTP extensions
567 string extens = status.Description;
570 ParseExtensions (extens);
574 InitiateSecureConnection ();
576 writer = new StreamWriter (stream);
577 reader = new StreamReader (stream);
578 status = SendCommand ("EHLO " + Dns.GetHostName ());
580 if (IsError (status)) {
581 status = SendCommand ("HELO " + Dns.GetHostName ());
583 if (IsError (status))
584 throw new SmtpException (status.StatusCode, status.Description);
586 // Parse ESMTP extensions
587 string extens = status.Description;
589 ParseExtensions (extens);
593 if (authMechs != AuthMechs.None)
596 MailAddress from = message.From;
602 status = SendCommand ("MAIL FROM:<" + from.Address + '>');
603 if (IsError (status)) {
604 throw new SmtpException (status.StatusCode, status.Description);
607 // Send RCPT TO: for all recipients
608 List<SmtpFailedRecipientException> sfre = new List<SmtpFailedRecipientException> ();
610 for (int i = 0; i < message.To.Count; i ++) {
611 status = SendCommand ("RCPT TO:<" + message.To [i].Address + '>');
612 if (IsError (status))
613 sfre.Add (new SmtpFailedRecipientException (status.StatusCode, message.To [i].Address));
615 for (int i = 0; i < message.CC.Count; i ++) {
616 status = SendCommand ("RCPT TO:<" + message.CC [i].Address + '>');
617 if (IsError (status))
618 sfre.Add (new SmtpFailedRecipientException (status.StatusCode, message.CC [i].Address));
620 for (int i = 0; i < message.Bcc.Count; i ++) {
621 status = SendCommand ("RCPT TO:<" + message.Bcc [i].Address + '>');
622 if (IsError (status))
623 sfre.Add (new SmtpFailedRecipientException (status.StatusCode, message.Bcc [i].Address));
626 #if TARGET_JVM // List<T>.ToArray () is not supported
627 if (sfre.Count > 0) {
628 SmtpFailedRecipientException[] xs = new SmtpFailedRecipientException[sfre.Count];
630 throw new SmtpFailedRecipientsException ("failed recipients", xs);
634 throw new SmtpFailedRecipientsException ("failed recipients", sfre.ToArray ());
638 status = SendCommand ("DATA");
639 if (IsError (status))
640 throw new SmtpException (status.StatusCode, status.Description);
642 // Send message headers
643 string dt = DateTime.Now.ToString ("ddd, dd MMM yyyy HH':'mm':'ss zzz", DateTimeFormatInfo.InvariantInfo);
644 // remove ':' from time zone offset (e.g. from "+01:00")
645 dt = dt.Remove (dt.Length - 3, 1);
646 SendHeader (HeaderName.Date, dt);
648 SendHeader (HeaderName.From, EncodeAddress (from));
649 SendHeader (HeaderName.To, EncodeAddresses (message.To));
650 if (message.CC.Count > 0)
651 SendHeader (HeaderName.Cc, EncodeAddresses (message.CC));
652 SendHeader (HeaderName.Subject, EncodeSubjectRFC2047 (message));
656 switch (message.Priority){
657 case MailPriority.Normal:
661 case MailPriority.Low:
665 case MailPriority.High:
669 SendHeader ("Priority", v);
670 if (message.Sender != null)
671 SendHeader ("Sender", EncodeAddress (message.Sender));
672 if (message.ReplyToList.Count > 0)
673 SendHeader ("ReplyTo", EncodeAddresses (message.ReplyToList));
676 foreach (string s in message.Headers.AllKeys)
677 SendHeader (s, ContentType.EncodeSubjectRFC2047 (message.Headers [s], message.HeadersEncoding));
679 foreach (string s in message.Headers.AllKeys)
680 SendHeader (s, message.Headers [s]);
683 AddPriorityHeader (message);
686 if (message.Attachments.Count > 0)
687 SendWithAttachments (message);
689 SendWithoutAttachments (message, null, false);
694 if (IsError (status))
695 throw new SmtpException (status.StatusCode, status.Description);
698 status = SendCommand ("QUIT");
699 } catch (System.IO.IOException) {
700 // We excuse server for the rude connection closing as a response to QUIT
704 public void Send (string from, string to, string subject, string body)
706 Send (new MailMessage (from, to, subject, body));
709 private void SendDot()
711 writer.Write(".\r\n");
715 private void SendData (string data)
717 if (String.IsNullOrEmpty (data)) {
718 writer.Write("\r\n");
723 StringReader sr = new StringReader (data);
725 bool escapeDots = deliveryMethod == SmtpDeliveryMethod.Network;
726 while ((line = sr.ReadLine ()) != null) {
727 CheckCancellation ();
731 for (i = 0; i < line.Length; i++) {
735 if (i > 0 && i == line.Length) {
740 writer.Write ("\r\n");
745 public void SendAsync (MailMessage message, object userToken)
748 throw new InvalidOperationException ("Another SendAsync operation is in progress");
750 worker = new BackgroundWorker ();
751 worker.DoWork += delegate (object o, DoWorkEventArgs ea) {
753 user_async_state = ea.Argument;
755 } catch (Exception ex) {
759 worker.WorkerSupportsCancellation = true;
760 worker.RunWorkerCompleted += delegate (object o, RunWorkerCompletedEventArgs ea) {
761 // Note that RunWorkerCompletedEventArgs.UserState cannot be used (LAMESPEC)
762 OnSendCompleted (new AsyncCompletedEventArgs (ea.Error, ea.Cancelled, user_async_state));
764 worker.RunWorkerAsync (userToken);
767 public void SendAsync (string from, string to, string subject, string body, object userToken)
769 SendAsync (new MailMessage (from, to, subject, body), userToken);
772 public void SendAsyncCancel ()
775 throw new InvalidOperationException ("SendAsync operation is not in progress");
776 worker.CancelAsync ();
779 private void AddPriorityHeader (MailMessage message) {
780 switch (message.Priority) {
781 case MailPriority.High:
782 SendHeader (HeaderName.Priority, "Urgent");
783 SendHeader (HeaderName.Importance, "high");
784 SendHeader (HeaderName.XPriority, "1");
786 case MailPriority.Low:
787 SendHeader (HeaderName.Priority, "Non-Urgent");
788 SendHeader (HeaderName.Importance, "low");
789 SendHeader (HeaderName.XPriority, "5");
794 private void SendSimpleBody (MailMessage message) {
795 SendHeader (HeaderName.ContentType, message.BodyContentType.ToString ());
796 if (message.ContentTransferEncoding != TransferEncoding.SevenBit)
797 SendHeader (HeaderName.ContentTransferEncoding, GetTransferEncodingName (message.ContentTransferEncoding));
798 SendData (string.Empty);
800 SendData (EncodeBody (message));
803 private void SendBodylessSingleAlternate (AlternateView av) {
804 SendHeader (HeaderName.ContentType, av.ContentType.ToString ());
805 if (av.TransferEncoding != TransferEncoding.SevenBit)
806 SendHeader (HeaderName.ContentTransferEncoding, GetTransferEncodingName (av.TransferEncoding));
807 SendData (string.Empty);
809 SendData (EncodeBody (av));
812 private void SendWithoutAttachments (MailMessage message, string boundary, bool attachmentExists)
814 if (message.Body == null && message.AlternateViews.Count == 1)
815 SendBodylessSingleAlternate (message.AlternateViews [0]);
816 else if (message.AlternateViews.Count > 0)
817 SendBodyWithAlternateViews (message, boundary, attachmentExists);
819 SendSimpleBody (message);
823 private void SendWithAttachments (MailMessage message) {
824 string boundary = GenerateBoundary ();
826 // first "multipart/mixed"
827 ContentType messageContentType = new ContentType ();
828 messageContentType.Boundary = boundary;
829 messageContentType.MediaType = "multipart/mixed";
830 messageContentType.CharSet = null;
832 SendHeader (HeaderName.ContentType, messageContentType.ToString ());
833 SendData (String.Empty);
836 Attachment body = null;
838 if (message.AlternateViews.Count > 0)
839 SendWithoutAttachments (message, boundary, true);
841 body = Attachment.CreateAttachmentFromString (message.Body, null, message.BodyEncoding, message.IsBodyHtml ? "text/html" : "text/plain");
842 message.Attachments.Insert (0, body);
846 SendAttachments (message, body, boundary);
849 message.Attachments.Remove (body);
852 EndSection (boundary);
855 private void SendBodyWithAlternateViews (MailMessage message, string boundary, bool attachmentExists)
857 AlternateViewCollection alternateViews = message.AlternateViews;
859 string inner_boundary = GenerateBoundary ();
861 ContentType messageContentType = new ContentType ();
862 messageContentType.Boundary = inner_boundary;
863 messageContentType.MediaType = "multipart/alternative";
865 if (!attachmentExists) {
866 SendHeader (HeaderName.ContentType, messageContentType.ToString ());
867 SendData (String.Empty);
871 AlternateView body = null;
872 if (message.Body != null) {
873 body = AlternateView.CreateAlternateViewFromString (message.Body, message.BodyEncoding, message.IsBodyHtml ? "text/html" : "text/plain");
874 alternateViews.Insert (0, body);
875 StartSection (boundary, messageContentType);
879 // alternate view sections
880 foreach (AlternateView av in alternateViews) {
882 string alt_boundary = null;
883 ContentType contentType;
884 if (av.LinkedResources.Count > 0) {
885 alt_boundary = GenerateBoundary ();
886 contentType = new ContentType ("multipart/related");
887 contentType.Boundary = alt_boundary;
888 contentType.Parameters ["type"] = "application/octet-stream";
889 StartSection (inner_boundary, contentType);
891 contentType = new ContentType (av.ContentType.ToString ());
892 StartSection (inner_boundary, contentType, av.TransferEncoding);
895 switch (av.TransferEncoding) {
896 case TransferEncoding.Base64:
897 byte [] content = new byte [av.ContentStream.Length];
898 av.ContentStream.Read (content, 0, content.Length);
900 SendData (Convert.ToBase64String (content));
902 SendData (Convert.ToBase64String (content, Base64FormattingOptions.InsertLineBreaks));
905 case TransferEncoding.QuotedPrintable:
906 byte [] bytes = new byte [av.ContentStream.Length];
907 av.ContentStream.Read (bytes, 0, bytes.Length);
908 SendData (ToQuotedPrintable (bytes));
910 case TransferEncoding.SevenBit:
911 case TransferEncoding.Unknown:
912 content = new byte [av.ContentStream.Length];
913 av.ContentStream.Read (content, 0, content.Length);
914 SendData (Encoding.ASCII.GetString (content));
918 if (av.LinkedResources.Count > 0) {
919 SendLinkedResources (message, av.LinkedResources, alt_boundary);
920 EndSection (alt_boundary);
923 if (!attachmentExists)
924 SendData (string.Empty);
929 alternateViews.Remove (body);
931 EndSection (inner_boundary);
934 private void SendLinkedResources (MailMessage message, LinkedResourceCollection resources, string boundary)
936 foreach (LinkedResource lr in resources) {
937 ContentType contentType = new ContentType (lr.ContentType.ToString ());
938 StartSection (boundary, contentType, lr.TransferEncoding);
940 switch (lr.TransferEncoding) {
941 case TransferEncoding.Base64:
942 byte [] content = new byte [lr.ContentStream.Length];
943 lr.ContentStream.Read (content, 0, content.Length);
945 SendData (Convert.ToBase64String (content));
947 SendData (Convert.ToBase64String (content, Base64FormattingOptions.InsertLineBreaks));
950 case TransferEncoding.QuotedPrintable:
951 byte [] bytes = new byte [lr.ContentStream.Length];
952 lr.ContentStream.Read (bytes, 0, bytes.Length);
953 SendData (ToQuotedPrintable (bytes));
955 case TransferEncoding.SevenBit:
956 case TransferEncoding.Unknown:
957 content = new byte [lr.ContentStream.Length];
958 lr.ContentStream.Read (content, 0, content.Length);
959 SendData (Encoding.ASCII.GetString (content));
965 private void SendAttachments (MailMessage message, Attachment body, string boundary) {
966 foreach (Attachment att in message.Attachments) {
967 ContentType contentType = new ContentType (att.ContentType.ToString ());
968 if (att.Name != null) {
969 contentType.Name = att.Name;
970 if (att.NameEncoding != null)
971 contentType.CharSet = att.NameEncoding.HeaderName;
972 att.ContentDisposition.FileName = att.Name;
974 StartSection (boundary, contentType, att.TransferEncoding, att == body ? null : att.ContentDisposition);
976 byte [] content = new byte [att.ContentStream.Length];
977 att.ContentStream.Read (content, 0, content.Length);
978 switch (att.TransferEncoding) {
979 case TransferEncoding.Base64:
981 SendData (Convert.ToBase64String (content));
983 SendData (Convert.ToBase64String (content, Base64FormattingOptions.InsertLineBreaks));
986 case TransferEncoding.QuotedPrintable:
987 SendData (ToQuotedPrintable (content));
989 case TransferEncoding.SevenBit:
990 case TransferEncoding.Unknown:
991 SendData (Encoding.ASCII.GetString (content));
995 SendData (string.Empty);
999 private SmtpResponse SendCommand (string command)
1001 writer.Write (command);
1002 // Certain SMTP servers will reject mail sent with unix line-endings; see http://cr.yp.to/docs/smtplf.html
1003 writer.Write ("\r\n");
1008 private void SendHeader (string name, string value)
1010 SendData (String.Format ("{0}: {1}", name, value));
1013 private void StartSection (string section, ContentType sectionContentType)
1015 SendData (String.Format ("--{0}", section));
1016 SendHeader ("content-type", sectionContentType.ToString ());
1017 SendData (string.Empty);
1020 private void StartSection (string section, ContentType sectionContentType,TransferEncoding transferEncoding)
1022 SendData (String.Format ("--{0}", section));
1023 SendHeader ("content-type", sectionContentType.ToString ());
1024 SendHeader ("content-transfer-encoding", GetTransferEncodingName (transferEncoding));
1025 SendData (string.Empty);
1028 private void StartSection (string section, ContentType sectionContentType, TransferEncoding transferEncoding, ContentDisposition contentDisposition) {
1029 SendData (String.Format ("--{0}", section));
1030 SendHeader ("content-type", sectionContentType.ToString ());
1031 SendHeader ("content-transfer-encoding", GetTransferEncodingName (transferEncoding));
1032 if (contentDisposition != null)
1033 SendHeader ("content-disposition", contentDisposition.ToString ());
1034 SendData (string.Empty);
1037 // use proper encoding to escape input
1038 private string ToQuotedPrintable (string input, Encoding enc)
1040 byte [] bytes = enc.GetBytes (input);
1041 return ToQuotedPrintable (bytes);
1044 private string ToQuotedPrintable (byte [] bytes)
1046 StringWriter writer = new StringWriter ();
1047 int charsInLine = 0;
1049 StringBuilder sb = new StringBuilder("=", 3);
1050 byte equalSign = (byte)'=';
1053 foreach (byte i in bytes) {
1054 if (i > 127 || i == equalSign) {
1056 sb.Append(Convert.ToString (i, 16).ToUpperInvariant ());
1059 c = Convert.ToChar (i);
1060 if (c == '\r' || c == '\n') {
1068 charsInLine += curLen;
1069 if (charsInLine > 75) {
1070 writer.Write ("=\r\n");
1071 charsInLine = curLen;
1076 writer.Write (sb.ToString ());
1079 return writer.ToString ();
1081 private static string GetTransferEncodingName (TransferEncoding encoding)
1084 case TransferEncoding.QuotedPrintable:
1085 return "quoted-printable";
1086 case TransferEncoding.SevenBit:
1088 case TransferEncoding.Base64:
1095 RemoteCertificateValidationCallback callback = delegate (object sender,
1096 X509Certificate certificate,
1098 SslPolicyErrors sslPolicyErrors) {
1099 // honor any exciting callback defined on ServicePointManager
1100 if (ServicePointManager.ServerCertificateValidationCallback != null)
1101 return ServicePointManager.ServerCertificateValidationCallback (sender, certificate, chain, sslPolicyErrors);
1102 // otherwise provide our own
1103 if (sslPolicyErrors != SslPolicyErrors.None)
1104 throw new InvalidOperationException ("SSL authentication error: " + sslPolicyErrors);
1109 private void InitiateSecureConnection () {
1110 SmtpResponse response = SendCommand ("STARTTLS");
1112 if (IsError (response)) {
1113 throw new SmtpException (SmtpStatusCode.GeneralFailure, "Server does not support secure connections.");
1117 ((NetworkStream) stream).ChangeToSSLSocket ();
1119 SslStream sslStream = new SslStream (stream, false, callback, null);
1120 CheckCancellation ();
1121 sslStream.AuthenticateAsClient (Host, this.ClientCertificates, SslProtocols.Default, false);
1125 throw new SystemException ("You are using an incomplete System.dll build");
1129 void Authenticate ()
1131 string user = null, pass = null;
1133 if (UseDefaultCredentials) {
1134 user = CredentialCache.DefaultCredentials.GetCredential (new System.Uri ("smtp://" + host), "basic").UserName;
1135 pass = CredentialCache.DefaultCredentials.GetCredential (new System.Uri ("smtp://" + host), "basic").Password;
1136 } else if (Credentials != null) {
1137 user = Credentials.GetCredential (host, port, "smtp").UserName;
1138 pass = Credentials.GetCredential (host, port, "smtp").Password;
1143 Authenticate (user, pass);
1146 void Authenticate (string Username, string Password)
1148 // FIXME: use the proper AuthMech
1149 SmtpResponse status = SendCommand ("AUTH LOGIN");
1150 if (((int) status.StatusCode) != 334) {
1151 throw new SmtpException (status.StatusCode, status.Description);
1154 status = SendCommand (Convert.ToBase64String (Encoding.ASCII.GetBytes (Username)));
1155 if (((int) status.StatusCode) != 334) {
1156 throw new SmtpException (status.StatusCode, status.Description);
1159 status = SendCommand (Convert.ToBase64String (Encoding.ASCII.GetBytes (Password)));
1160 if (IsError (status)) {
1161 throw new SmtpException (status.StatusCode, status.Description);
1165 #endregion // Methods
1167 // The HeaderName struct is used to store constant string values representing mail headers.
1168 private struct HeaderName {
1169 public const string ContentTransferEncoding = "Content-Transfer-Encoding";
1170 public const string ContentType = "Content-Type";
1171 public const string Bcc = "Bcc";
1172 public const string Cc = "Cc";
1173 public const string From = "From";
1174 public const string Subject = "Subject";
1175 public const string To = "To";
1176 public const string MimeVersion = "MIME-Version";
1177 public const string MessageId = "Message-ID";
1178 public const string Priority = "Priority";
1179 public const string Importance = "Importance";
1180 public const string XPriority = "X-Priority";
1181 public const string Date = "Date";
1184 // This object encapsulates the status code and description of an SMTP response.
1185 private struct SmtpResponse {
1186 public SmtpStatusCode StatusCode;
1187 public string Description;
1189 public static SmtpResponse Parse (string line) {
1190 SmtpResponse response = new SmtpResponse ();
1192 if (line.Length < 4)
1193 throw new SmtpException ("Response is to short " +
1196 if ((line [3] != ' ') && (line [3] != '-'))
1197 throw new SmtpException ("Response format is wrong.(" +
1200 // parse the response code
1201 response.StatusCode = (SmtpStatusCode) Int32.Parse (line.Substring (0, 3));
1203 // set the raw response
1204 response.Description = line;
1211 class CCredentialsByHost : ICredentialsByHost
1213 public CCredentialsByHost (string userName, string password) {
1214 this.userName = userName;
1215 this.password = password;
1218 public NetworkCredential GetCredential (string host, int port, string authenticationType) {
1219 return new NetworkCredential (userName, password);
1222 private string userName;
1223 private string password;