Пример #1
0
        private void SendServerReady()
        {
            var response =
                ServerStatusCodesConverter.GetTextResponseForStatus(ResponseCodes.SrvReady, _clientController.HostName);

            _clientController.Write(response);
        }
Пример #2
0
 public SmtpServerClientProcessor(ITcpClientController clientController, ILogger logger)
 {
     _clientController          = clientController;
     EmailParser                = new EmailParser();
     RequestCommandsConverter   = new RequestCommandsConverter();
     ServerStatusCodesConverter = new ServerStatusCodesConverter();
     Logger = logger;
 }
Пример #3
0
        private bool HandleHello(MailMessage message, string nextLine)
        {
            var responseStatus = _clientController.IsTlsAvailable ? ResponseCodes.SrvHello : ResponseCodes.SrvHelloNoTls;
            var response       = ServerStatusCodesConverter.GetTextResponseForStatus(responseStatus, _clientController.HostName);

            _clientController.Write(response);
            return(false);
        }
Пример #4
0
        private bool HandleMailFrom(MailMessage message, string nextLine)
        {
            MailAddress sender;

            try
            {
                sender = EmailParser.ParseEmailFromString(nextLine);
            }
            catch
            {
                _clientController.Write(ServerStatusCodesConverter.GetTextResponseForStatus(ResponseCodes.MbNameNotAllowed));
                return(true);
            }

            message.From = sender;
            _clientController.Write(ServerStatusCodesConverter.GetTextResponseForStatus(ResponseCodes.RqstActOkCompleted));
            return(false);
        }
Пример #5
0
        private bool SwitchToTls(MailMessage message, string nextLine)
        {
            _clientController.Write(ServerStatusCodesConverter.GetTextResponseForStatus(ResponseCodes.SrvReady, _clientController.HostName));
            try
            {
                _clientController.SwitchToTlsProtocol();
            }
            catch (Exception e)
            {
                Logger.LogError(string.Format("Exception occurred while switching to TLS:\n{0}", e.Message));
                if (e.InnerException != null)
                {
                    Logger.LogError(string.Format("Inner exception: {0}\n", e.InnerException.Message));
                }

                _clientController.Write(ServerStatusCodesConverter.GetTextResponseForStatus(ResponseCodes.AccessDenied));
                return(true);
            }

            return(false);
        }
Пример #6
0
        private bool HandleDataSection(MailMessage message, string nextLine)
        {
            _clientController.Write(ServerStatusCodesConverter.GetTextResponseForStatus(ResponseCodes.StrtInputEndWith));
            var messageData = new StringBuilder();

            var strMessage = _clientController.Read();

            _clientController.Write(ServerStatusCodesConverter.GetTextResponseForStatus(ResponseCodes.RqstActOkCompleted));

            while (!strMessage.Contains("\r\n.\r\n"))             //Contains because some times QUIT is added right to the body
            {
                messageData.Append(strMessage);
                strMessage = _clientController.Read();
            }

            messageData.Append(strMessage);

            var msgDataStr = messageData.ToString();

            var headers        = EmailParser.ParseHeadersFromDataSection(msgDataStr);
            var parsedCcList   = EmailParser.ParseEmailsFromDataCc(headers);
            var parsedToList   = EmailParser.ParseEmailsFromDataTo(headers);
            var parsedFromList = EmailParser.ParseEmailsFromDataFrom(headers);
            var toList         = MergeToList(message.To, parsedToList, parsedCcList);

            message.To   = toList;
            message.CC   = parsedCcList;
            message.From = GetMailAddressesByAddress(message.From, parsedFromList);

            message.Body       = EmailParser.ParseBodyFromDataSection(msgDataStr);
            message.Subject    = EmailParser.ParseSubjectFromDataSection(headers);
            message.Headers    = headers;
            message.IsBodyHtml = EmailParser.GetIsMailBodyHtml(headers);

            message.MailMessageDataSection = msgDataStr.Trim();

            _clientController.Write(ServerStatusCodesConverter.GetTextResponseForStatus(ResponseCodes.RqstActOkCompleted));
            return(ClientHasEndedSendingMail(msgDataStr));
        }
Пример #7
0
        private bool HandleMailTo(MailMessage message, string nextLine)
        {
            MailAddress recipient;

            try
            {
                recipient = EmailParser.ParseEmailFromString(nextLine);
            }
            catch
            {
                _clientController.Write(ServerStatusCodesConverter.GetTextResponseForStatus(ResponseCodes.MbNameNotAllowed));
                return(true);
            }

            if (message.To.All(a => a.Address != recipient.Address))
            {
                message.To.Add(recipient);
            }

            _clientController.Write(ServerStatusCodesConverter.GetTextResponseForStatus(ResponseCodes.RqstActOkCompleted));
            return(false);
        }