'Server Socket Communicate to Email Client Application

I'd simply doing some C# socket programming to create a simple POP + SMTP server for communicate with Email Client Application such as Eudora, but I'd no idea to implement it in ideal way with such a following code or maybe any other better way around.

The Email Client is just infinitely connect under status "Logging into POP server" until it was timeout and terminated.

There is no response in Server side.

Appreciate Thanful to anyone could provide a handful help.

using System.Threading;
using System.Net.Sockets;
using System.Net;

public class EmailSettings
{
        public String MailFromAddress = "[email protected]";
        public String ServerName = "mail.domain.com";
        public int ServerPort = 993;
        public int SmtpPort = 465;
}

public class StateObject
{
        public Socket WorkSocket = null;
        public const int BUFFER_SIZE = 1024;
        public byte[] buffer = new byte[BUFFER_SIZE];
        public StringBuilder sb = new StringBuilder();
}

class Program
{
        static ManualResetEvent AllDone = new ManualResetEvent(false);

        static void Accept_CallBack(IAsyncResult ar)
        {
            AllDone.Set();

            Socket Listener = (Socket)ar.AsyncState;
            Socket Handle = Listener.EndAccept(ar);

            StateObject State = new StateObject() { WorkSocket = Handle };
            Handle.BeginReceive(State.buffer, 0, StateObject.BUFFER_SIZE, 0, new AsyncCallback(Receive_CallBack), State);
        }

        static void Receive_CallBack(IAsyncResult ar)
        {
   
            StateObject State = (StateObject)ar.AsyncState;
            Socket Handle = State.WorkSocket;

            int ByteReceive_Count = Handle.EndReceive(ar);
            /* Doing Some Rest */
        }

    static void Main(string[] args)
        {

         using (Socket Server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP))
                 {
                        Server.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 110));
                        Server.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.HeaderIncluded, true);
                        Server.Listen(10);
                        
                        while (true)
                        {
                            AllDone.Reset();
                            
                            Server.BeginAccept(new AsyncCallback(Accept_CallBack), Server);
                            
                            AllDone.WaitOne();   
                        }
                 }
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source