SmtpServer.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // SmtpServer.cs - Dummy SMTP server used to test SmtpClient
  3. //
  4. // Author:
  5. // Raja R Harinath <[email protected]>
  6. //
  7. using System;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.Net;
  11. using System.Net.Mail;
  12. using System.Net.Sockets;
  13. using System.Text;
  14. using System.Threading;
  15. namespace MonoTests.System.Net.Mail {
  16. public class SmtpServer
  17. {
  18. public string mail_from, rcpt_to;
  19. public StringBuilder data;
  20. TcpListener server;
  21. public IPEndPoint EndPoint {
  22. get { return (IPEndPoint) server.LocalEndpoint; }
  23. }
  24. public SmtpServer ()
  25. {
  26. server = new TcpListener (0);
  27. server.Start (1);
  28. }
  29. private static void WriteNS (NetworkStream ns, string s)
  30. {
  31. Trace ("response", s);
  32. byte [] bytes = Encoding.ASCII.GetBytes (s);
  33. ns.Write (bytes, 0, bytes.Length);
  34. }
  35. public void Run ()
  36. {
  37. string s;
  38. using (TcpClient client = server.AcceptTcpClient ()) {
  39. Trace ("connection", EndPoint.Port);
  40. using (NetworkStream ns = client.GetStream ()) {
  41. WriteNS (ns, "220 localhost\r\n");
  42. using (StreamReader r = new StreamReader (ns, Encoding.UTF8)) {
  43. while ((s = r.ReadLine ()) != null && Dispatch (ns, r, s))
  44. ;
  45. }
  46. }
  47. }
  48. }
  49. // return false == terminate
  50. public bool Dispatch (NetworkStream ns, StreamReader r, string s)
  51. {
  52. Trace ("command", s);
  53. if (s.Length < 4) {
  54. WriteNS (ns, "502 Huh\r\n");
  55. return false;
  56. }
  57. bool retval = true;
  58. switch (s.Substring (0, 4)) {
  59. case "HELO":
  60. break;
  61. case "QUIT":
  62. WriteNS (ns, "221 Quit\r\n");
  63. return false;
  64. case "MAIL":
  65. mail_from = s.Substring (10);
  66. break;
  67. case "RCPT":
  68. rcpt_to = s.Substring (8);
  69. break;
  70. case "DATA":
  71. WriteNS (ns, "354 Continue\r\n");
  72. data = new StringBuilder ();
  73. while ((s = r.ReadLine ()) != null) {
  74. if (s == ".")
  75. break;
  76. data.AppendLine (s);
  77. }
  78. Trace ("end of data", s);
  79. retval = (s != null);
  80. break;
  81. default:
  82. WriteNS (ns, "502 Huh\r\n");
  83. return true;
  84. }
  85. WriteNS (ns, "250 OK\r\n");
  86. return retval;
  87. }
  88. [Conditional ("TEST")]
  89. static void Trace (string key, object value)
  90. {
  91. Console.Error.WriteLine ("{0}: {1}", key, value);
  92. }
  93. #if TEST
  94. static void DoTest (SmtpServer s, SmtpClient c, MailMessage m)
  95. {
  96. Thread t = new Thread (s.Run);
  97. t.Start ();
  98. c.Send (m);
  99. t.Join ();
  100. Console.WriteLine ("Message From: {0}", m.From);
  101. Console.WriteLine ("Message Sender: {0}", m.Sender);
  102. Console.WriteLine ("Mail From: {0}", s.mail_from);
  103. Console.WriteLine ("Rcpt To: {0}", s.rcpt_to);
  104. Console.WriteLine ("-------------------------------------");
  105. Console.Write (s.data);
  106. Console.WriteLine ("-------------------------------------");
  107. }
  108. static void Main ()
  109. {
  110. var server = new SmtpServer ();
  111. var client = new SmtpClient ("localhost", server.EndPoint.Port);
  112. var msg = new MailMessage ("[email protected]", "[email protected]", "hello", "howdydoo");
  113. DoTest (server, client, msg);
  114. msg.Sender = new MailAddress ("[email protected]");
  115. DoTest (server, client, msg);
  116. }
  117. #endif
  118. }
  119. }