예제 #1
0
        private void StartSession()
        {
            Debug.WriteLine("Connection accepted!");

            _sslStream = CreateSslStream(_networkStream);

            SendFormat("220 {0} SMTP server ready.", "localhost");
            string response = ReadResponse();

            if (!response.StartsWith("HELO") && !response.StartsWith("EHLO"))
            {
                Send("500 UNKNOWN COMMAND");
                _networkStream.Close();
                return;
            }
            string remoteServer = response.Replace("HELO", string.Empty).Replace("EHLO", string.Empty).Trim();

            Send("250-localhost");



            Send("250 AUTH LOGIN PLAIN CRAM-MD5");
            response = ReadResponse();
            string username = null;

            if (response.ToUpper() == "AUTH LOGIN")
            {
                Send("334 VXNlcm5hbWU6");

                var encodedUsername = ReadResponse();
                username = GetUsername(encodedUsername);

                Send("334 UGFzc3dvcmQ6");
                var passwordResponse = ReadResponse();
                var password         = GetUsername(passwordResponse);

                SessionMailBox = VerifyUser(username, password);

                if (SessionMailBox != null)
                {
                    Send("235 OK, GO AHEAD");
                }
                else
                {
                    Send("500 Bad Username or Password");
                    _networkStream.Close();
                    return;
                }
            }

            MailMessage emailMessage = new MailMessage()
            {
                Sent = DateTime.UtcNow
            };

            do
            {
                var senderMessage = ReadResponse();

                if (!senderMessage.StartsWith("MAIL FROM:"))
                {
                    emailMessage.From = response;
                    Send("500 UNKNOWN COMMAND");
                    _networkStream.Close();
                    return;
                }
                else
                {
                    var recipient = senderMessage.Replace("MAIL FROM:", string.Empty).Trim();
                    SendFormat("250 Go ahead with message for {0}", recipient);
                }

                var recipientMessage = ReadResponse();

                do
                {
                    if (!recipientMessage.StartsWith("RCPT TO:"))
                    {
                        emailMessage.To = response;
                        Send("500 UNKNOWN COMMAND");
                        _networkStream.Close();
                        return;
                    }
                    else
                    {
                        var sender = recipientMessage.Replace("MAIL FROM:", string.Empty).Trim();
                        SendFormat("250 {0} I like that guy too!", sender);
                    }

                    recipientMessage = ReadResponse();
                }while (recipientMessage.Trim() != "DATA");


                Send("354 Enter message. When finished, enter \".\" on a line by itself");

                int           counter = 0;
                StringBuilder message = new StringBuilder();

                while ((response = ReadResponse().Trim()) != ".")
                {
                    message.AppendLine(response);
                    counter++;

                    if (counter == 1000000)
                    {
                        _networkStream.Close();
                        return;
                    }
                }

                Console.WriteLine("Received message:");
                Send("250 OK");

                response = ReadResponse();

                var messageBody = message.ToString();

                string uri = MailStorage.Save(username, messageBody);

                using (var stream = MailStorage.GetStream(messageBody))
                {
                    var           parser        = new MimeParser(stream);
                    var           parsedMessage = parser.ParseMessage();
                    HeaderList    headers       = parsedMessage.Headers;
                    StringBuilder headerBuilder = new StringBuilder();

                    foreach (var header in headers)
                    {
                        headerBuilder.AppendLine(header.ToString());
                    }
                    var headerText  = headerBuilder.ToString();
                    var messageText = parsedMessage.GetTextBody(MimeKit.Text.TextFormat.Text);

                    if (messageText != null)
                    {
                        messageText = messageText.Substring(0, 200);
                    }

                    MailboxMessage mmbm = new MailboxMessage
                    {
                        MailboxId  = SessionMailBox.MailBoxId,
                        MessageId  = parsedMessage.MessageId,
                        Body       = messageText,
                        RawHeader  = headerText,
                        MessageUrl = uri,
                        Recieved   = DateTime.UtcNow,
                        IsRead     = false
                    };

                    SqlServerMailboxRepositry.AddMailMessage(SessionMailBox, mmbm);
                }

                if (response.ToUpper() == "RSET")
                {
                    Send("250 OK");
                }
            }while (response.ToUpper() == "RSET");
            {
                _networkStream.Close();
            }
        }
예제 #2
0
        private void ReadMailMessage()
        {
            var data = String.Empty;

            MailMessage emailMessage = new MailMessage()
            {
                Sent = DateTime.UtcNow
            };

            var senderMessage = ReadResponse();

            if (!senderMessage.StartsWith("MAIL FROM:"))
            {
                Send("500 UNKNOWN COMMAND");
                _stream.Close();
                return;
            }
            else
            {
                var recipient = senderMessage.Replace("MAIL FROM:", string.Empty).Trim();
                SendFormat("250 Go ahead with message for {0}", recipient);
            }

            var recipientMessage = ReadResponse();

            if (!recipientMessage.StartsWith("RCPT TO:"))
            {
                Send("500 UNKNOWN COMMAND");
                _stream.Close();
                return;
            }
            else
            {
                var sender = recipientMessage.Replace("MAIL FROM:", string.Empty).Trim();
                SendFormat("250 {0} I like that guy too!", sender);
            }

            data = ReadResponse();

            if (data.Trim() != "DATA")
            {
                Send("500 UNKNOWN COMMAND");
                _stream.Close();
                return;
            }

            Send("354 Enter message. When finished, enter \".\" on a line by itself");

            int           counter = 0;
            StringBuilder message = new StringBuilder();

            string splitData;

            while ((splitData = ReadResponse().Trim()) != ".")
            {
                message.AppendLine(splitData);
                counter++;
            }

            Console.WriteLine("Received message:");
            Send("250 OK");



            var messageBody = message.ToString();

            string key = MailStorage.Save(RemoteUser, messageBody);

            using (var stream = MailStorage.GetStream(messageBody))
            {
                var           parser        = new MimeParser(stream);
                HeaderList    headers       = parser.ParseHeaders();
                StringBuilder headerBuilder = new StringBuilder();
                foreach (var header in headers)
                {
                    headerBuilder.AppendLine(header.ToString());
                }
                var headerText = headerBuilder.ToString();
                MailStorage.QueueMessage(key, RemoteUser, headerText);
            }
        }