GithubHelp home page GithubHelp logo

Comments (20)

kerryjiang avatar kerryjiang commented on June 29, 2024

Please try to use the AppServer's method "void ResetSessionSecurity(IAppSession session, SslProtocols security)".

from supersocket.

tinohager avatar tinohager commented on June 29, 2024

Sorry for the late answer. I was on a conference the last days.

I try with the ResetSessionSecurity method, but the mail library throws a exception.

Socket connection has been refused by remote host. InnerException message follows: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. InnerException message follows: An existing connection was forcibly closed by the remote host ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at a.ae.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)

from supersocket.

kerryjiang avatar kerryjiang commented on June 29, 2024

Where did this exception come from? Was it from SuperSocket?

from supersocket.

tinohager avatar tinohager commented on June 29, 2024

No the exception throws the mailbee library, is my test framework. (https://www.nuget.org/packages/MailBee.NET)

Here a simple Example of the Mailserver

    class Program
    {
        static void Main(string[] args)
        {
            var example = new Example();
            example.Start();

            Console.WriteLine("Mailserver started");
            Console.ReadLine();
        }   
    }
    public class Example
    {
        private MailServer _mailServer;

        public void Start()
        {
            //Only need visual studio copy the SocketEngine.dll
            var configuration = new SuperSocket.SocketEngine.Configuration.Server();

            //Configuration
            var config = new SuperSocket.SocketBase.Config.ServerConfig();
            config.Port = 25;
            config.MaxConnectionNumber = 10;
            config.ClearIdleSession = true;
            config.ClearIdleSessionInterval = 60; //seconds
            config.IdleSessionTimeOut = 60;
            config.MaxRequestLength = 2097152; //2MB
            config.ListenBacklog = 5; //Waiting Connection Pool
            config.SendBufferSize = 1024;
            config.ReceiveBufferSize = 1024;
            config.SyncSend = true;
            config.LogBasicSessionActivity = false;
            config.Certificate = new SuperSocket.SocketBase.Config.CertificateConfig() { FilePath = "test.pfx", Password = "test" };

            this._mailServer = new MailServer();
            this._mailServer.NewRequestReceived += new RequestHandler<SmtpSession, StringRequestInfo>(NewRequestReceived);

            this._mailServer.Setup(config);
            this._mailServer.Start();
        }

        private void NewRequestReceived(SmtpSession session, StringRequestInfo requestInfo)
        {
            session.ProccessRequest(requestInfo.Key, requestInfo.Body);
        }

        public class MailServer : AppServer<SmtpSession>
        {
            public MailServer() : base(new CommandLineReceiveFilterFactory(Encoding.UTF8, new MailCommandParser()))
            {
            }
        }

        public class SmtpSession : AppSession<SmtpSession>
        {
            private bool _readyForData;

            protected override void OnSessionStarted()
            {
                Thread.Sleep(20);
                this.Send("220 Mail Service ready");
            }

            public void ProccessRequest(string key, string data)
            {
                if (this._readyForData)
                {
                    if (!String.IsNullOrEmpty(key))
                    {
                        this.DataRequest(key);
                        return;
                    }
                    this.DataRequest(data);
                }
                else
                {
                    this.CommandRequest(key, data);
                }
            }

            private void CommandRequest(string key, string data)
            {
                switch (key.ToUpper())
                {
                    case "EHLO":
                        this.Send("250-Hello");
                        this.Send("250-STARTTLS");
                        this.Send("250 OK");
                        return;
                    case "HELO":
                        this.Send("250 Hello");
                        return;
                    case "RSET":
                        this.Send("250 Ok");
                        return;
                    case "NOOP":
                        this.Send("250 Ok");
                        return;
                    case "STARTTLS":
                        //Switch in TLS Session
                        this.Send("220 Ready to start TLS");
                        this.AppServer.ResetSessionSecurity(this, System.Security.Authentication.SslProtocols.Default);
                        return;
                    case "MAIL FROM":
                        this.Send("250 Sender ok");
                        return;
                    case "RCPT TO":
                        this.Send("250 Recipient ok");
                        return;
                    case "DATA":
                        this._readyForData = true;
                        this.Send("354 Go ahead, make my day");
                        return;
                    case "QUIT":
                        this.Send("221 Closing");
                        Thread.Sleep(20);
                        this.Close();
                        return;
                }

                this.Send("500 Unrecognized command");
            }

            private void DataRequest(string data)
            {
                #region Receive Data End Char
                if (data.Equals(".", StringComparison.Ordinal))
                {
                    this.Send("250 Queued");
                    this._readyForData = false;
                }
                #endregion
            }
        }

        public class MailCommandParser : IRequestInfoParser<StringRequestInfo>
        {
            public StringRequestInfo ParseRequestInfo(string source)
            {
                if (source.StartsWith("EHLO", StringComparison.OrdinalIgnoreCase))
                {
                    return ParseRequestInfo(source, " ");
                }

                if (source.StartsWith("HELO", StringComparison.OrdinalIgnoreCase))
                {
                    return ParseRequestInfo(source, " ");
                }

                if (source.StartsWith("MAIL FROM", StringComparison.OrdinalIgnoreCase))
                {
                    return ParseRequestInfo(source, ":");
                }

                if (source.StartsWith("RCPT TO", StringComparison.OrdinalIgnoreCase))
                {
                    return ParseRequestInfo(source, ":");
                }

                if (source.StartsWith("DATA", StringComparison.OrdinalIgnoreCase))
                {
                    return new StringRequestInfo(source, String.Empty, null);
                }

                if (source.StartsWith("RSET", StringComparison.OrdinalIgnoreCase))
                {
                    return new StringRequestInfo(source, String.Empty, null);
                }

                if (source.StartsWith("NOOP", StringComparison.OrdinalIgnoreCase))
                {
                    return new StringRequestInfo(source, String.Empty, null);
                }

                if (source.StartsWith("STARTTLS", StringComparison.OrdinalIgnoreCase))
                {
                    return new StringRequestInfo(source, String.Empty, null);
                }

                if (source.StartsWith("QUIT", StringComparison.OrdinalIgnoreCase))
                {
                    return new StringRequestInfo(source, "", null);
                }

                return new StringRequestInfo(String.Empty, source, null);
            }

            private StringRequestInfo ParseRequestInfo(string source, string spliter)
            {
                var pos = source.IndexOf(spliter);

                var name = String.Empty;
                var param = String.Empty;

                if (pos > 0)
                {
                    name = source.Substring(0, pos);
                    param = source.Substring(pos + 1);
                }
                else
                {
                    name = source;
                }

                return new StringRequestInfo(name, param.Trim(), null);
            }
        }
    }

from supersocket.

kerryjiang avatar kerryjiang commented on June 29, 2024

Can I have a full stack trace of that exception?

I cannot find where that exception from by the text you posted.

from supersocket.

tinohager avatar tinohager commented on June 29, 2024

This is a example client, you can change the "EnableSsl" to false it works, but with true not work

        static void Main(string[] args)
        {
            CreateTestMessage("localhost");
            Console.ReadLine();
        }

        public static void CreateTestMessage(string server)
        {
            try
            {
                var from = "[email protected]";
                var to = "[email protected]";
                var message = new MailMessage(from, to);
                message.Subject = "Using the new SMTP client.";
                message.Body = @"Using this new feature, you can send an e-mail message from an application very easily.";
                var client = new SmtpClient(server);
                client.EnableSsl = true;
                client.Send(message);
                Console.WriteLine("Send mail successful");
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }
        }

from supersocket.

tinohager avatar tinohager commented on June 29, 2024

I hope with this 2 examples you can follow my problem?

from supersocket.

kerryjiang avatar kerryjiang commented on June 29, 2024

Do you mean the exception you mentioned was thrown from the client side?

from supersocket.

tinohager avatar tinohager commented on June 29, 2024

Yes the client throw this exception. But the demo client i post here use the .net framework classes. In my private client i use the mailbee library but this one need a licence...

from supersocket.

kerryjiang avatar kerryjiang commented on June 29, 2024

Could you check the logs of the server side to see what happened?

from supersocket.

tinohager avatar tinohager commented on June 29, 2024

i have add log4net in my demo server project. but only one line is in the log

2016-06-23 18:02:32,351 [9] INFO MailServer-62476613 - The server instance MailServer-62476613 has been started!

from supersocket.

kerryjiang avatar kerryjiang commented on June 29, 2024

Reproduce this issue and then check the error log file.

from supersocket.

tinohager avatar tinohager commented on June 29, 2024

err.log is emtpy, if it possible ResetSessionSecurity close the current connection and open a new one, instead update the current connection to TLS

from supersocket.

kerryjiang avatar kerryjiang commented on June 29, 2024

Could you debug the method ResetSessionSecurity to see what happen?
Probably the client try to connect the secure port 465 of the smtp server.
https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol

from supersocket.

tinohager avatar tinohager commented on June 29, 2024

I have check this possibility with the port 465, with my reference project (https://github.com/cosullivan/SmtpServer) here also only port 25 is open and it works well.

SampleApp (SmtpServer Project)

            var cancellationTokenSource = new CancellationTokenSource();

            var options = new OptionsBuilder()
                .ServerName("SmtpServer SampleApp")
                .Port(25)
                .MessageStore(new ConsoleMessageStore())
                .MailboxFilter(new ConsoleMailboxFilter())
                .Certificate(new System.Security.Cryptography.X509Certificates.X509Certificate("test.pfx", "test"))
                .Build();

            var serverTask = RunServerAsync(options, cancellationTokenSource.Token);

from supersocket.

tinohager avatar tinohager commented on June 29, 2024

i found a exception but if not available in the log file...

AsyncStreamSocketSession -> OnSessionStarting
System.NullReferenceException: Object reference not set to an instance to an object

from supersocket.

kerryjiang avatar kerryjiang commented on June 29, 2024

Could you debug the method ResetSessionSecurity to see what happen?

from supersocket.

tinohager avatar tinohager commented on June 29, 2024

on this position i have the exception

AsyncStreamSocketSession
OnSessionStarting
m_Stream.BeginRead(m_ReadBuffer, m_Offset, m_Length, OnStreamEndRead, m_Stream);

m_Stream is null


i have also check why is not in the error log, IsIgnorableException is true

//This exception is ignored, needn't log it
if (IsIgnorableException(exception, out socketErrorCode))
   return;

from supersocket.

tinohager avatar tinohager commented on June 29, 2024

Can you reproduce the exception?

from supersocket.

kerryjiang avatar kerryjiang commented on June 29, 2024

Yes, I found it.
But it is very difficult to fix in this version.

from supersocket.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.