Exemplo n.º 1
0
        /// <summary>
        /// Handle the MAIL FROM:&lt;address&gt; command.
        /// </summary>
        private void Mail(SMTPContext context, string argument)
        {
            bool addressValid = false;

            if (context.LastCommand == COMMAND_HELO)
            {
                string address = ParseAddress(argument);
                if (address != null)
                {
                    try
                    {
                        context.Message.From.Add(new MailBox(address));
                        context.LastCommand = COMMAND_MAIL;
                        addressValid        = true;
                        context.WriteLine(MESSAGE_OK);
                        Trace.WriteLine(string.Format("Connection {0}: MailFrom address: {1} accepted.", context.ConnectionId, address));
                    }
                    catch (InvalidEmailAddressException)
                    {
                        // This is fine, just fall through.
                    }
                }

                // If the address is invalid, inform the client.
                if (!addressValid)
                {
                    Trace.WriteLine(string.Format("Connection {0}: MailFrom argument: {1} rejected.  Should be from:<*****@*****.**>", context.ConnectionId, argument));
                    context.WriteLine(MESSAGE_INVALID_ADDRESS);
                }
            }
            else
            {
                context.WriteLine(MESSAGE_INVALID_COMMAND_ORDER);
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Reset the connection state.
 /// </summary>
 private void Rset(SMTPContext context)
 {
     if (context.LastCommand != -1)
     {
         // Dump the message and reset the context.
         context.Reset();
         context.WriteLine(MESSAGE_OK);
     }
     else
     {
         context.WriteLine(MESSAGE_INVALID_COMMAND_ORDER);
     }
 }
Exemplo n.º 3
0
        private void Data(SMTPContext context)
        {
            context.WriteLine(MESSAGE_START_DATA);

            IPEndPoint    clientEndPoint = (IPEndPoint)context.Socket.RemoteEndPoint;
            StringBuilder data           = new StringBuilder();

            data.Append(String.Format("Received: from {0} ({0} [{1}])", context.ClientDomain, clientEndPoint.Address));
            data.Append("\r\n");
            data.Append(String.Format("     by {0} (Age of Wonders Email Wrapper)", domain));
            data.Append("\r\n");
            data.Append("     " + System.DateTime.Now);
            data.Append("\r\n");

            String line = context.ReadLine();

            while (!line.Equals("."))
            {
                data.Append(line);
                data.Append("\r\n");
                line = context.ReadLine();
            }

            context.Message = new MailBuilder().CreateFromEml(data.ToString());

            // Spool the message
            if (messageSpool == null)
            {
                //If no spooler set
                context.WriteLine(MESSAGE_OK);
            }
            else if (messageSpool.SpoolMessage(context.Message))
            {
                context.WriteLine(MESSAGE_OK);
            }
            else
            {
                context.WriteLine(MESSAGE_SYSTEM_ERROR);
            }

            // Reset the connection.
            context.Reset();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Handle the RCPT TO:&lt;address&gt; command.
        /// </summary>
        private void Rcpt(SMTPContext context, string argument)
        {
            if (context.LastCommand == COMMAND_MAIL || context.LastCommand == COMMAND_RCPT)
            {
                string address = ParseAddress(argument);
                if (address != null)
                {
                    try
                    {
                        EmailAddress emailAddress = new EmailAddress(address);

                        // Check to make sure we want to accept this message.
                        if (recipientFilter.AcceptRecipient(context, emailAddress))
                        {
                            context.Message.To.Add(new MailBox(address));
                            context.LastCommand = COMMAND_RCPT;
                            context.WriteLine(MESSAGE_OK);
                            Trace.WriteLine(string.Format("Connection {0}: RcptTo address: {1} accepted.", context.ConnectionId, address));
                        }
                        else
                        {
                            context.WriteLine(MESSAGE_UNKNOWN_USER);
                            Trace.WriteLine(string.Format("Connection {0}: RcptTo address: {1} rejected.  Did not pass Address Filter.", context.ConnectionId, address));
                        }
                    }
                    catch (InvalidEmailAddressException)
                    {
                        Trace.WriteLine(string.Format("Connection {0}: RcptTo argument: {1} rejected.  Should be from:<*****@*****.**>", context.ConnectionId, argument));
                        context.WriteLine(MESSAGE_INVALID_ADDRESS);
                    }
                }
                else
                {
                    Trace.WriteLine(string.Format("Connection {0}: RcptTo argument: {1} rejected.  Should be from:<*****@*****.**>", context.ConnectionId, argument));
                    context.WriteLine(MESSAGE_INVALID_ADDRESS);
                }
            }
            else
            {
                context.WriteLine(MESSAGE_INVALID_COMMAND_ORDER);
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// Handles the HELO command.
 /// </summary>
 private void Helo(SMTPContext context, String[] inputs)
 {
     if (context.LastCommand == -1)
     {
         if (inputs.Length == 2)
         {
             context.ClientDomain = inputs[1];
             context.LastCommand  = COMMAND_HELO;
             context.WriteLine(HeloResponse);
         }
         else
         {
             context.WriteLine(MESSAGE_INVALID_ARGUMENT_COUNT);
         }
     }
     else
     {
         context.WriteLine(MESSAGE_INVALID_COMMAND_ORDER);
     }
 }
Exemplo n.º 6
0
        /// <summary>
        /// Handles the command input from the client.  This
        /// message returns when the client issues the quit command.
        /// </summary>
        private void ProcessCommands(SMTPContext context)
        {
            bool   isRunning = true;
            String inputLine;

            // Loop until the client quits.
            while (isRunning)
            {
                try
                {
                    inputLine = context.ReadLine();
                    if (inputLine == null)
                    {
                        isRunning = false;
                        context.Close();
                        continue;
                    }

                    Trace.WriteLine("ProcessCommands Read: " + inputLine);
                    String[] inputs = inputLine.Split(" ".ToCharArray());

                    switch (inputs[0].ToLower())
                    {
                    case "ehlo":
                    case "helo":
                        Helo(context, inputs);
                        break;

                    case "rset":
                        Rset(context);
                        break;

                    case "noop":
                        context.WriteLine(MESSAGE_OK);
                        break;

                    case "quit":
                        isRunning = false;
                        context.WriteLine(MESSAGE_GOODBYE);
                        context.Close();
                        break;

                    case "mail":
                        if (inputs[1].ToLower().StartsWith("from"))
                        {
                            Mail(context, inputLine.Substring(inputLine.IndexOf(" ")));
                            break;
                        }
                        context.WriteLine(MESSAGE_UNKNOWN_COMMAND);
                        break;

                    case "rcpt":
                        if (inputs[1].ToLower().StartsWith("to"))
                        {
                            Rcpt(context, inputLine.Substring(inputLine.IndexOf(" ")));
                            break;
                        }
                        context.WriteLine(MESSAGE_UNKNOWN_COMMAND);
                        break;

                    case "data":
                        Data(context);
                        break;

                    default:
                        context.WriteLine(MESSAGE_UNKNOWN_COMMAND);
                        break;
                    }
                }
                catch (Exception exception)
                {
                    Trace.WriteLine(string.Format("Connection {0}: Exception occured while processing commands: {1}", context.ConnectionId, exception.Message));
                    context.WriteLine(MESSAGE_SYSTEM_ERROR);
                    throw exception;
                }
            }
        }
Exemplo n.º 7
0
 /// <summary>
 /// Sends the welcome greeting to the client.
 /// </summary>
 private void SendWelcomeMessage(SMTPContext context)
 {
     context.WriteLine(WelcomeMessage);
 }