Пример #1
0
        private void SaveMessage(Message e)
        {
            // make sure the subject isn't to long
            string subject = TextFunctions.Remove(e.Subject, Path.GetInvalidFileNameChars());

            if (subject.Length > 100)
            {
                subject = string.Concat(subject.Substring(0, 100), " ...");
            }
            string fileName = path + Path.DirectorySeparatorChar + e.MessageId + ".eml";
            string mailData = e.XSender + Environment.NewLine + e.XReceiver + Environment.NewLine + e.Data;

            File.WriteAllText(fileName, mailData);
            Console.WriteLine("Message with ID: " + e.MessageId + " saved.");
        }
Пример #2
0
        public void Start()
        {
            listener.Start();
            status = StatusEnum.Connected;
            Message message = null;

            Console.WriteLine("Start SMTP Server");

            try
            {
                socket        = listener.AcceptSocket();
                networkStream = new NetworkStream(socket, FileAccess.ReadWrite, true);
                WriteLine(string.Format("220 Welcome {0}, SMTP Server.", ((IPEndPoint)socket.RemoteEndPoint).Address));
                while (socket.Connected)
                {
                    string data = this.Read(lineTerminator);
                    if (data == null)
                    {
                        break;
                    }
                    else
                    {
                        if (data.StartsWith("QUIT", StringComparison.InvariantCultureIgnoreCase))
                        {
                            WriteLine("221 Good bye");
                            throw new Exception("Restart SMTP Server");
                        }
                        else if (data.StartsWith("EHLO", StringComparison.InvariantCultureIgnoreCase) || data.StartsWith("HELO", StringComparison.InvariantCultureIgnoreCase))
                        {
                            this.WriteGreeting(data.Substring(4).Trim());
                            status = StatusEnum.Identified;
                        }
                        else if (status < StatusEnum.Identified)
                        {
                            this.WriteError(ErrorEnum.BadCommandSequence, "Expected HELO <Your Name>");
                        }
                        else
                        {
                            if (data.StartsWith("MAIL", StringComparison.InvariantCultureIgnoreCase))
                            {
                                if (status != StatusEnum.Identified && status != StatusEnum.Data)
                                {
                                    this.WriteError(ErrorEnum.BadCommandSequence, "Command out of sequence");
                                }
                                else
                                {
                                    // create a new message
                                    message      = new Message();
                                    message.From = TextFunctions.Tail(data, ":");
                                    status       = StatusEnum.Mail;
                                    this.WriteOk();
                                }
                            }
                            else if (data.StartsWith("RCPT", StringComparison.InvariantCultureIgnoreCase))
                            {
                                if (status != StatusEnum.Mail && status != StatusEnum.Recipient)
                                {
                                    this.WriteError(ErrorEnum.BadCommandSequence, "Command out of sequence");
                                }
                                else
                                {
                                    message.To.Add(TextFunctions.Tail(data, ":"));
                                    status = StatusEnum.Recipient;
                                    this.WriteOk();
                                }
                            }
                            else if (data.StartsWith("DATA", StringComparison.InvariantCultureIgnoreCase))
                            {
                                // request data
                                this.WriteSendData();
                                message.Data = this.Read(dataTerminator);
                                // Remove message end tag
                                message.Data = message.Data.Replace(dataTerminator, string.Empty);
                                SaveMessage(message);
                                message = null;
                                status  = StatusEnum.Data;
                                this.WriteOk();
                            }
                            else
                            {
                                this.WriteError(ErrorEnum.CommandNotImplemented, "Command not implemented");
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Debug(e.Message);
                Dispose();
                Start();
            }
        }