I have a SMTP listener who works well, but is able to get only one connection. My C # code is down and I'm running it as a service, my goal is to run it on the server and parse several smtp messages sent to it.
Currently parses the first message and closes the job. How can I get it to accept 2, 3, 4th ... SMTP message and like it as before?
Here is my code: / p> < Pre>
; Using System.Collections.Generic; Using System.Linq; Using System.Text; Using System.Net.Sockets; Using System.Net; Using System.IO; Namespace SMTP_Listener {class program {static zero main (string [] ARG) {TCP listener listener = new TCPLisher (IPadation no, 8000); TcpClient Client; Networkstream Ns; Listener.Start (); Console.lightline ("Waiting for connection ..."); Client = listener.acceptTcpClient (); Console WrightLine ("Connection accepted!"); Ns = client.GetStream (); (AuthorWriteLine ("220 localhost SMTP server ready.") Using the StreamWriter author = new streamer (ns);); Writer.flush (); (StreamReader Reader = New Stream Reader (ns)) using {string response = reader.ReadLine (); If (Reactions! Stars ("Halo") & amp;; reactions with stars ("EHLO")). Weedline ("500 unknown command"); Writer.flush (); Ns.Close (); Return; } String remote = response. Replay ("Hello", string.optim). ("EHLO", String.Application). Trim (); Author. Whiteline ("250 localhost hello" + remote); Writer.flush (); Feedback = Reader Readline (); If (! Response stars ("From Mail:")) {author. Virusline ("500 unknown command"); Writer.flush (); Ns.Close (); Return; } Remote = Response Replay ("RCPT to:", string.optim). Trim (); Author. Wrightline ("250" + Remote + "I like that guy too!"); Writer.flush (); Feedback = Reader Readline (); If (the response! "Stars" ("RCPT to:")) {author. Weedline ("500 unknown command"); Writer.flush (); Ns.Close (); Return; } Remote = Response. Replay ("From Mail:", string.optim). Trim (); Author. Wrightline ("250" + Remote + "I like that guy!"); Writer.flush (); Feedback = Reader Readline (); If (response.Trim ()! = "Data") {author. WrightLine ("500 unknown command"); Writer.flush (); Ns.Close (); Return; } Author. LightLine ("Enter 354 messages. When completed, \". \ "Enter on a line manually);); Writer.flush (); Int counter = 0; StringBinder Message = New StringBuilder (); While ((feedback = reader.readline (). Trim ())! = ".") {Message.AppendLine (response); Counter ++; If (counter == 1000000) {ns.Close (); Return; // seriously? 1 million lines in a message? }} Author. White line ("250 ok"); Writer.flush (); Ns.Close (); // Enter "Message" in the DB Console. WrightLine ("Received Message:"); Console.WriteLine (message.ToString ()); }} Console. Readkey (); You can distinguish most of your code in different threads:
static void key (string [] args) {TcpListener listener = new TcpListener (IPAddress.Any, 8000); TcpClient Client; Listener.Start (); While (true) // Add your Exclusive Flag here {client = listener.AcceptTcpClient (); ThreadPool.QueueUserWorkItem (ThreadProcess, Client); }} Private Static Zero Threadproc (object obj) {var Client = (TcpClient) obj; // your work here}}
Comments
Post a Comment