public SmtpSession CreateSession(ISmtpResponderFactory responderFactory)
        {
            var session = new SmtpSession(this, responderFactory);

            session.OnSessionDisconnected +=
                (sender, args) => ClientDisconnected(this, new SmtpConnectionEventArgs(this));
            Session = session;

            SessionCreated(this, new SmtpSessionEventArgs(Session));
            return(session);
        }
Exemplo n.º 2
0
        private async Task SetupSessionThenProcessCommands(SmtpConnection connection, SmtpSession session)
        {
            await SendGreetingAsync(connection, Greeting ?? Configuration.DefaultGreeting + " " + System.DateTime.Now.ToString("R"));

            var sessionInfoParseResponder = new SmtpSessionInfoResponder(session.ResponderFactory, session.SessionInfo);

            var rawLineDecoder = new RawLineDecoder(connection);

            rawLineDecoder.RequestDisconnection += (s, e) =>
            {
                if (!e.DisconnectionExpected)
                {
                    MailServerLogger.Instance.Warn(String.Format("Connection unexpectedly lost {0}", connection.RemoteEndPoint));
                }

                rawLineDecoder.Cancel();
                session.Disconnect();
            };
            rawLineDecoder.DetectedActivity   += (s, e) => session.UpdateActivity();
            rawLineDecoder.ProcessLineCommand += async(s, e) =>
            {
                var response = sessionInfoParseResponder.ProcessLineCommand(e.Buffer);
                if (response == null || !response.HasValue)
                {
                    return;
                }

                if (response.ResponseCode == SmtpResponses.DisconnectResponseCode)
                {
                    await SendResponseAsync(connection, response);

                    MailServerLogger.Instance.Debug(String.Format("Remote connection disconnected {0}", connection.RemoteEndPoint));
                    rawLineDecoder.Cancel();
                    await Task.Delay(100).ContinueWith(t => session.Disconnect());

                    return;
                }
                if (response == SmtpResponse.StartTLS)
                {
                    await SendResponseAsync(connection, response);

                    //session.starttls();
                }
                // ReSharper restore PossibleUnintendedReferenceComparison

                await SendResponseAsync(connection, response);
            };

#pragma warning disable 4014
            rawLineDecoder.ProcessCommandsAsync();
#pragma warning restore 4014
        }
 public SmtpSessionEventArgs(SmtpSession session)
 {
     Session = session;
 }