예제 #1
0
        private void Data(SmtpContext context)
        {
            context.WriteLine(MESSAGE_START_DATA);

            MailMessage message = context.Message;

            //IPEndPoint clientEndPoint = (IPEndPoint) context.Socket.RemoteEndPoint;
            //IPEndPoint localEndPoint = (IPEndPoint) context.Socket.LocalEndPoint;

            StringBuilder header = new StringBuilder();

            header.Append("Received: from " + context.ClientDomain + " (" + context.ClientDomain + " [" + context.RemoteEndPoint.Address + "])");
            header.Append("\r\n");
            header.Append("     by " + context.LocalEndPoint.Address);
            header.Append("\r\n");
            header.Append("     " + System.DateTime.Now);
            header.Append("\r\n");

            message.AddData(header.ToString());

            String line = context.ReadLine();

            while (!line.Equals("."))
            {
                message.AddData(line);
                message.AddData("\r\n");

                if (line.Length == 0)
                {
                    message.SetEndOfHeader();
                }

                line = context.ReadLine();
            }

            string spoolError;

            if (message == null || _storage.SpoolMessage(context.RemoteEndPoint, message.ToAddress, message.Message, out spoolError))
            {
                context.WriteLine(MESSAGE_OK);
            }
            else
            {
                if (spoolError != null && spoolError.Length > 0)
                {
                    context.Write("554");
                    context.WriteLine(spoolError);
                }
                else
                {
                    context.WriteLine(MESSAGE_SYSTEM_ERROR);
                }
            }

            context.Reset();
        }
예제 #2
0
        private void ProcessCommands(SmtpContext context)
        {
            bool   isRunning = true;
            String inputLine;

            while (isRunning)
            {
                try
                {
                    inputLine = context.ReadLine();

                    // TODO: remove this if gmail google is running
                    if (inputLine == null)
                    {
                        isRunning = false;
                        context.WriteLine(MESSAGE_GOODBYE);
                        context.Close();
                        break;
                    }

                    String[] inputs = inputLine.Split(' ');

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

                    case "ehlo":
                        Ehlo(context, inputs);
                        break;

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

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

                    case "vrfy":
                        Vrfy(context, inputLine.Substring(5));
                        break;

                    case "quit":
                        isRunning = false;

                        context.WriteLine(MESSAGE_GOODBYE);
                        context.Close();
                        break;

                    case "mail":
                        // TODO: move the input line to 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 ex)
                {
                    SocketException sx = ex as SocketException;

                    if (sx != null && sx.ErrorCode == 10060)
                    {
                        context.WriteLine(MESSAGE_GOODBYE);
                    }
                    else
                    {
                        context.WriteLine(MESSAGE_SYSTEM_ERROR);
                    }

                    isRunning = false;
                    context.Close();
                }
            }
        }