示例#1
0
        private string ReadResponse()
        {
            try
            {
                System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\d{3}\s.+");
                string response = String.Empty;
                System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
                byte[]        serverbuff      = new Byte[1024];
                NetworkStream stream          = GetStream();

                if (!stream.CanRead)
                {
                    throw new MailException("Stream could not be read.");
                }

                int count = 0;
                // read until last response has been received
                // (indicated by whitespace after the numerical response code)
                do
                {
                    int LoopTimeout = 0;
                    if (stream.DataAvailable)
                    {
                        count    = stream.Read(serverbuff, 0, serverbuff.Length);
                        response = String.Concat(response, enc.GetString(serverbuff, 0, count));
                    }

                    if ((LoopTimeout += 100) > this._smtpserver.ServerTimeout)
                    {
                        throw new MailException("Multiline server response timed out");
                    }
                    Thread.Sleep(100);
                }while(!regex.IsMatch(response));

                _smtpserver.OnLogReceiveSmtp(this, response);
                if (_captureConversation)
                {
                    this._smtpserver.AppendConversation(SmtpProxy.SERVER_LOG_PROMPT + response);
                }
                return(response);
            }
            catch (Exception ex)
            {
                //LogError(ex.Message);
                throw new MailException("Error while receiving data from server: " + ex.Message, ex);
            }
        }