Exemple #1
0
        async Task <SMTPImpostorMessage> GetAsync(
            string messageId)
        {
            var content = await TryGetMessageContentAsync(messageId);

            if (content == null)
            {
                return(null);
            }

            return(SMTPImpostorMessage
                   .Parse(content, messageId));
        }
Exemple #2
0
        /// <summary>
        ///   <para>Process the current data read</para>
        /// </summary>
        void Process(string data)
        {
            var terminator = SMTPImpostorMessage.LINE_TERMINATOR;

            try
            {
                if (Status == SocketHandlerStates.Data)
                {
                    var content = _data.ToString(0, _data.Length - _terminator.Length);
                    _data.Clear();

                    var message = SMTPImpostorMessage.Parse(content);
                    // _logger.LogInformation("Session.Process Data: => {0}", data);

                    Write(ReplyCodes.Completed_250);
                    Status = SocketHandlerStates.Identified;

                    _taskCompletion.SetResult(message);

                    return;
                }

                // command expected
                var command = data.Length < 4
                                  ? string.Empty
                                  : data.Substring(0, 4).ToUpper();
                // _logger.LogInformation("Session.Process Command: => {0}", command);

                if (command.Equals(COMMAND_QUIT))
                {
                    Dispose();
                }
                else if (command.Equals(COMMAND_EHLO) ||
                         command.Equals(COMMAND_HELO))
                {
                    ClientId = data.Substring(4).Trim();
                    // _logger.LogInformation("Session.Process ClientId: => {0}", ClientId);

                    Write(ReplyCodes.Completed_250, String.Format(Resources.Hello_250, ClientId));

                    Status = SocketHandlerStates.Identified;
                    //Host.RaiseEvent(HostEventTypes.SessionIdentified, this);
                }
                else if (command.Equals(COMMAND_NOOP))
                {
                    Write(ReplyCodes.Completed_250, String.Format(Resources.Hello_250, ClientId));
                }
                else if (command.Equals(COMMAND_RSET))
                {
                    Write(ReplyCodes.Completed_250, String.Format(Resources.Hello_250, ClientId));

                    _from = null;
                    _to   = null;

                    Status = SocketHandlerStates.Identified;
                    //Host.RaiseEvent(HostEventTypes.SessionIdentified, this);
                }
                else if (Status < SocketHandlerStates.Identified)
                {
                    Write(ReplyCodes.CommandSequenceError_503, Resources.ExpectedHELO_503);
                }
                else if (command.Equals(COMMAND_MAIL))
                {
                    if (Status != SocketHandlerStates.Identified)
                    {
                        Write(ReplyCodes.CommandSequenceError_503);
                    }
                    else
                    {
                        // store the from address, in case its not in the header
                        _from = data.Tail(":").ToMailAddress();
                        // _logger.LogInformation("Session.Process From: => {0}", _from);

                        Status = SocketHandlerStates.Mail;
                        Write(ReplyCodes.Completed_250);
                    }
                }
                else if (command.Equals(COMMAND_RCPT))
                {
                    if (Status != SocketHandlerStates.Mail &&
                        Status != SocketHandlerStates.Recipient)
                    {
                        Write(ReplyCodes.CommandSequenceError_503);
                    }
                    else
                    {
                        if (_to == null)
                        {
                            _to = new List <MailAddress>();
                        }
                        var to = data.Tail(":").ToMailAddress();
                        _to.Add(to);
                        // _logger.LogInformation("Session.Process To: => {0}", to);

                        Status = SocketHandlerStates.Recipient;
                        Write(ReplyCodes.Completed_250);
                    }
                }
                else if (command.Equals(COMMAND_DATA))
                {
                    // request data
                    Status = SocketHandlerStates.Data;
                    Write(ReplyCodes.StartInput_354);

                    terminator = SMTPImpostorMessage.DATA_TERMINATOR;
                }
                else
                {
                    Write(ReplyCodes.CommandNotImplemented_502);
                }
            }
            catch (SMTPImpostorInvalidMailAddressException ex)
            {
                Write(
                    ReplyCodes.ParameterError_501,
                    string.Format("Invalid Address '{0}'", ex.Address));
            }
            finally
            {
                if (_networkStream != null)
                {
                    // next read
                    StartRead(terminator);
                }
            }
        }