Example #1
0
        internal SmtpResponse HeloCommand(string CommandLine)
        {
            var response = new SmtpResponse();
            var command  = SmtpCommand.Parse(CommandLine);

            // TODO validate the incoming domain...
            if (command.Name == SmtpCommandName.HELO)
            {
                if (!string.IsNullOrEmpty(command.Value))
                {
                    response.Code       = 250;
                    response.Message    = "Hello " + command.Value + " Kooboo Smtp Server";
                    this.State          = CommandState.Body;
                    this.ClientHostName = command.Value;
                }
                else
                {
                    response.Code    = 501;
                    response.Message = "Hostname required";
                }
            }
            else if (command.Name == SmtpCommandName.EHLO)
            {
                if (!string.IsNullOrEmpty(command.Value))
                {
                    response.Code       = 250;
                    response.Seperator  = '-';
                    response.Message    = "Hello " + command.Value + "\r\n250-SIZE 20480000\r\n250-AUTH LOGIN\r\n250 OK";
                    this.State          = CommandState.Body;
                    this.ClientHostName = command.Value;
                }
                else
                {
                    response.Code    = 501;
                    response.Message = "Hostname required";
                }
            }
            else if (command.Name == SmtpCommandName.UNKNOWN)
            {
                response.Code    = 550;
                response.Message = "bad command";
            }
            else if (command.Name == SmtpCommandName.QUIT)
            {
                response.Code    = 221;
                response.Message = "Goodbye";
                response.Close   = true;
            }
            else
            {
                response.Code    = 502;
                response.Message = "HELO or EHLO first";
            }
            this.Log.Add(command, response);
            return(response);
        }
Example #2
0
        internal SmtpResponse BodyCommand(string CommandLine)
        {
            var response = new SmtpResponse();
            var command  = SmtpCommand.Parse(CommandLine);

            if (command.Name == SmtpCommandName.AUTHLOGIN)
            {
                if (string.IsNullOrEmpty(command.Value))
                {
                    this.State       = CommandState.Auth;
                    response.Code    = 334;
                    response.Message = Convert.ToBase64String(Encoding.ASCII.GetBytes("Username:"******"Password:"******"Sender address must be specified";
                }
                else if (!ValidMailFrom(command.Value))
                {
                    response.Code    = 550;
                    response.Message = "Invalid Sender address";
                }
                else
                {
                    response.Code    = 250;
                    response.Message = "Sender OK";
                }
            }
            else if (command.Name == SmtpCommandName.RCPTTO)
            {
                if (string.IsNullOrEmpty(command.Value))
                {
                    response.Code    = 550;
                    response.Message = "Recipient address must be specified";
                }
                else
                {
                    var validateResult = ValidateRecipient(command.Value);
                    if (validateResult.IsOkToSend)
                    {
                        response.Code    = 250;
                        response.Message = "Recipient OK";
                    }
                    else
                    {
                        response.Code    = 550;
                        response.Message = validateResult.ErrorMessage;
                    }
                }
            }

            else if (command.Name == SmtpCommandName.DATA)
            {
                //public static string DataStart = "354 ";
                this.State       = CommandState.Data;
                response.Code    = 354;
                response.Message = "End data with <CLRF>.<CLRF>";
            }
            else if (command.Name == SmtpCommandName.QUIT)
            {
                response.Code    = 221;
                response.Message = "Goodbye";
                response.Close   = true;
            }
            else
            {
                response.Code    = 550;
                response.Message = "Invalid command";
            }
            this.Log.Add(command, response);
            return(response);
        }
Example #3
0
        public static SmtpCommand Parse(string CommandLine)
        {
            if (string.IsNullOrWhiteSpace(CommandLine))
            {
                return(new SmtpCommand()
                {
                    Name = SmtpCommandName.UNKNOWN, CommandLine = CommandLine
                });
            }

            var scanner = new CommandScanner(CommandLine);
            var token   = scanner.ConsumeNext();

            if (token == null)
            {
                return(new SmtpCommand()
                {
                    Name = SmtpCommandName.UNKNOWN, CommandLine = CommandLine
                });
            }
            token = token.ToUpper();

            var result = new SmtpCommand()
            {
                CommandLine = CommandLine, Name = SmtpCommandName.UNKNOWN
            };

            if (token == "HELO")
            {
                result.Name  = SmtpCommandName.HELO;
                result.Value = scanner.ConsumeRest();
            }
            else if (token == "EHLO")
            {
                result.Name  = SmtpCommandName.EHLO;
                result.Value = scanner.ConsumeRest();
            }
            else if (token == "AUTH")
            {
                var next = scanner.ConsumeNext();
                if (next != null && next.ToUpper() == "LOGIN")
                {
                    result.Name  = SmtpCommandName.AUTHLOGIN;
                    result.Value = scanner.ConsumeRest();
                }
            }
            else if (token == "MAIL")
            {
                var next = scanner.ConsumeNext();
                if (next != null && next.ToUpper() == "FROM")
                {
                    result.Name  = SmtpCommandName.MAILFROM;
                    result.Value = scanner.ConsumeRestAfterComma();
                }
            }
            else if (token == "RCPT")
            {
                var next = scanner.ConsumeNext();
                if (next != null && next.ToUpper() == "TO")
                {
                    result.Name  = SmtpCommandName.RCPTTO;
                    result.Value = scanner.ConsumeRestAfterComma();
                }
            }
            else if (token == "DATA")
            {
                result.Name  = SmtpCommandName.DATA;
                result.Value = scanner.ConsumeRest();
            }
            else if (token == "QUIT")
            {
                result.Name  = SmtpCommandName.QUIT;
                result.Value = scanner.ConsumeRest();
            }
            else if (token == "RSET")
            {
                result.Name  = SmtpCommandName.RSET;
                result.Value = scanner.ConsumeRest();
            }
            else if (token == "HELP")
            {
                result.Name  = SmtpCommandName.HELP;
                result.Value = scanner.ConsumeRest();
            }
            else if (token == "VRFY")
            {
                result.Name  = SmtpCommandName.VRFY;
                result.Value = scanner.ConsumeRest();
            }

            return(result);
        }