Beispiel #1
0
 private void OnCommandManagerUpdateMessage(object sender, SMTP.Commands.CommandEvent.CommandEventArgs e)
 {
     MessageEventArgs mea = new MessageEventArgs(e.Message);
     mea.Type=(MessageEventType)(int)e.Type;
     this.WriteLog(sender, mea);
 }
        static void ConnectFunc(object client_obj)
        {
            string[] split;
            byte[] msg;
            DBMySQL db = new DBMySQL();
            Byte[] bytes = new Byte[256];
            String data = null;

            TcpClient client = client_obj as TcpClient;

            data = null;

            NetworkStream stream = client.GetStream();

            SMTP smtp = new SMTP();

            int i;

            while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
            {
                data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);

                //parse input data on words
                split = data.Split(new Char[] { ' ' });

                switch (split[0])//first word - command, second - argument
                {
                    case "QUIT":
                        msg = System.Text.Encoding.ASCII.GetBytes(smtp.GenerateAnswerQuit());
                        stream.Write(msg, 0, msg.Length);
                        client.Close();
                        return;

                    case "HELO":
                        msg = System.Text.Encoding.ASCII.GetBytes("250 domain name should be qualified");
                        break;

                    case "DATA":
                        if (smtp.User != "")
                        {
                            msg = System.Text.Encoding.ASCII.GetBytes("354 Enter mail, end with \".\" on a line by itself");
                            stream.Write(msg, 0, msg.Length);

                            i = stream.Read(bytes, 0, bytes.Length);
                            data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);

                            db.InsertSendMail(smtp.User, data);

                            msg = System.Text.Encoding.ASCII.GetBytes("250 message accepted for delivery");
                        }
                        else
                        {
                            msg = System.Text.Encoding.ASCII.GetBytes("No deliver. Need \"RCPT TO:\" first");
                        }
                        break;

                    case "RCPT":
                        if(split[1] == "TO:")
                        {
                            if(split[2] != "")
                            {
                                string CHECK_RIGHT_USER_NAME = "";
                                string CHECK_RIGHT_EMAIL_ADRR = "";
                                CHECK_RIGHT_EMAIL_ADRR = split[2];
                                CHECK_RIGHT_USER_NAME = db.User(CHECK_RIGHT_EMAIL_ADRR);
                                if (CHECK_RIGHT_USER_NAME != "Err" && CHECK_RIGHT_USER_NAME != "")
                                {
                                    smtp.Receiver = CHECK_RIGHT_EMAIL_ADRR;
                                    smtp.User = CHECK_RIGHT_USER_NAME;
                                    msg = System.Text.Encoding.ASCII.GetBytes(smtp.GenerateAnswerOK(smtp.Receiver));
                                }
                                else if (smtp.User == "")
                                {
                                    msg = System.Text.Encoding.ASCII.GetBytes("550 " + split[2] + " unknown user account");
                                }
                                else
                                {
                                    smtp.User = "";
                                    msg = System.Text.Encoding.ASCII.GetBytes("Command not implemented");
                                }
                            }
                            else
                                msg = System.Text.Encoding.ASCII.GetBytes(smtp.GenerateAnswerErrArgument());
                        }
                        else
                            msg = System.Text.Encoding.ASCII.GetBytes(smtp.GenerateAnswerErrCommand());
                        break;

                    case "HELLO"://handshake
                        msg = System.Text.Encoding.ASCII.GetBytes(smtp.GenerateAnswerHello());
                        break;

                    default:
                        msg = System.Text.Encoding.ASCII.GetBytes(smtp.GenerateAnswerErrCommand());
                        break;
                }
                stream.Write(msg, 0, msg.Length);
            }
            client.Close();
        }