Example #1
0
        public void TestNonDataModeError()
        {
            using (ShimsContext.Create())
            {
                var server      = new ShimSMTPServer();
                var settings    = new StubIReceiveSettings();
                var transaction = new SMTPTransaction(server, settings);

                Assert.False(transaction.InDataMode);
                Assert.Throws <InvalidOperationException>(() => transaction.HandleData(""));
                Assert.Throws <InvalidOperationException>(() => transaction.HandleDataLine("", new StringBuilder()));
            }
        }
Example #2
0
        public void TestDataHandling(bool expectedResult)
        {
            using (ShimsContext.Create())
            {
                var server      = new ShimSMTPServer();
                var settings    = new StubIReceiveSettings();
                var transaction = new SMTPTransaction(server, settings);

                Assert.False(transaction.InDataMode);

                const string  expectedData          = "Some data";
                string        actualData            = null;
                const string  expectedLine          = "Some line";
                string        actualLine            = null;
                var           expectedStringBuilder = new StringBuilder();
                StringBuilder actualStringBuilder   = null;
                var           expectedResponse      = new SMTPResponse(SMTPStatusCode.Okay);

                transaction.StartDataMode((line, builder) =>
                {
                    actualLine          = line;
                    actualStringBuilder = builder;
                    return(expectedResult);
                }, data =>
                {
                    actualData = data;
                    return(expectedResponse);
                });

                var actualResult   = transaction.HandleDataLine(expectedLine, expectedStringBuilder);
                var actualResponse = transaction.HandleData(expectedData);

                Assert.Equal(expectedResult, actualResult);
                Assert.Same(expectedResponse, actualResponse);
                Assert.Same(expectedStringBuilder, actualStringBuilder);
                Assert.Equal(expectedData, actualData);
                Assert.Equal(expectedLine, actualLine);
            }
        }
Example #3
0
        public async Task Process()
        {
            try
            {
                Log(LogEventType.Connect);

                SMTPResponse response;
                _transaction = _smtpServer.SMTPServer.StartTransaction(_remoteEndpoint.Address, _smtpServer.Settings,
                                                                       out response);

                if (_tlsConnector.Settings.Mode == TLSMode.FullTunnel)
                {
                    await StartTLS();
                }

                RefreshReaderAndWriter();

                _transaction.OnStartTLS += OnStartTLS;
                _transaction.OnClose    += OnCloseHandler;

                await Write(response);

                try
                {
                    while (!_reader.EndOfStream && !_transaction.Closed)
                    {
                        await Write(_transaction.ExecuteCommand(await Read()));

                        while (_transaction.InDataMode)
                        {
                            string line;
                            var    data = new StringBuilder();

                            do
                            {
                                line = await _reader.ReadLineAsync();
                            } while (_transaction.HandleDataLine(line, data));

                            await Write(_transaction.HandleData(data.ToString()));
                        }

                        if (_startTLS)
                        {
                            await StartTLS();

                            RefreshReaderAndWriter();
                            RefreshTransaction();

                            _startTLS = false;
                        }
                    }
                }
                catch (IOException)
                {
                }
            }
            catch (Exception e)
            {
                Logger.Error("Error while handling client connection.", e);
            }
            finally
            {
                Close();
            }
        }