Пример #1
0
        public async Task <StartTlsResult> TryStartTls(Stream networkStream)
        {
            try
            {
                using (IStreamReader streamReader =
                           new StreamReaderWrapper(networkStream, Encoding.ASCII, true, 1024, true))
                {
                    using (IStreamWriter streamWriter =
                               new StreamWriterWrapper(networkStream, Encoding.ASCII, 1024, true)
                    {
                        AutoFlush = true,
                        NewLine = LineEnding
                    })
                    {
                        SmtpResponse response1 = await _smtpDeserializer.Deserialize(streamReader);

                        _log.Debug($"<: {response1}");

                        if (response1.Responses.FirstOrDefault()?.ResponseCode != ResponseCode.ServiceReady)
                        {
                            return(new StartTlsResult(false, response1.Responses.Select(_ => _.ToString()).ToList(),
                                                      "The server did not present a service ready response code (220)."));
                        }

                        EhloCommand ehloCommand = new EhloCommand(_mxSecurityTesterConfig.SmtpHostName);
                        _log.Debug($">: {ehloCommand.CommandString}");
                        await _smtpSerializer.Serialize(ehloCommand, streamWriter);

                        SmtpResponse response2 = await _smtpDeserializer.Deserialize(streamReader);

                        _log.Debug($"<: {response2}");
                        if (!response2.Responses.Any(_ =>
                                                     _.Value.ToLower() == Starttls && _.ResponseCode == ResponseCode.Ok))
                        {
                            return(new StartTlsResult(false, response2.Responses.Select(_ => _.ToString()).ToList(),
                                                      "The server did not present a STARTTLS command with a response code (250)."));
                        }

                        StartTlsCommand startTlsCommand = new StartTlsCommand();
                        _log.Debug($">: {startTlsCommand.CommandString}");
                        await _smtpSerializer.Serialize(startTlsCommand, streamWriter);

                        SmtpResponse response3 = await _smtpDeserializer.Deserialize(streamReader);

                        _log.Debug($"<: {response3}");

                        return(new StartTlsResult(
                                   response3.Responses.FirstOrDefault()?.ResponseCode == ResponseCode.ServiceReady,
                                   response3.Responses.Select(_ => _.Value).ToList(), string.Empty));
                    }
                }
            }
            catch (Exception e)
            {
                _log.Error(
                    $"SMTP session initalization failed with error: {e.Message} {Environment.NewLine} {e.StackTrace}");
                return(new StartTlsResult(false, null, e.Message));
            }
        }
Пример #2
0
        public async Task InitalResponseIsntServiceReadyReturnsFalse()
        {
            A.CallTo(() => _smtpDeserializer.Deserialize(A <IStreamReader> ._)).Returns(
                Task.FromResult(new SmtpResponse(new List <Response> {
                _unknownReponse
            })));

            StartTlsResult result = await _smtpClient.TryStartTls(Stream.Null);

            Assert.That(result.Success, Is.False);
        }