示例#1
0
        public string Command(string command, string stamp, string checkStamp, CommandOptions options)
        {
            if (options == null)
                options = new CommandOptions();

            OnTcpWriting(command.Length < 200
                ? new TcpWritingEventArgs(stamp + ((stamp.Length > 0) ? " " : "") + command + "\r\n")
                : new TcpWritingEventArgs("long command data"));

            GetStream()
                .Write(
                    Encoding.GetEncoding("iso-8859-1")
                            .GetBytes(stamp + ((stamp.Length > 0) ? " " : "") + command + "\r\n"),
                    0, stamp.Length + ((stamp.Length > 0) ? 1 : 0) + command.Length + 2);

            OnTcpWritten(command.Length < 200
                             ? new TcpWrittenEventArgs(stamp + ((stamp.Length > 0) ? " " : "") +
                                                       command + "\r\n")
                             : new TcpWrittenEventArgs("long command data"));
            OnTcpReading();

            var buffer = new StringBuilder();

            string temp;
            string lastline;

            using (
                var sr = new System.IO.StreamReader(GetStream(), Encoding.GetEncoding("iso-8859-1"), false,
                                                    Client.ReceiveBufferSize, true))
            {
                sr.BaseStream.ReadTimeout = Client.ReceiveTimeout;

                while (true)
                {
                    temp = sr.ReadLine();
                    buffer.Append(temp + "\r\n");
                    if (temp != null && (temp.StartsWith("+ ") && options.IsPlusCmdAllowed))
                    {
                        lastline = temp;
                        break;
                    }

                    if (temp == null ||
                        (!temp.StartsWith(checkStamp) &&
                         !temp.ToLower().StartsWith("* " + command.Split(' ')[0].ToLower()))) continue;
                    lastline = temp;
                    break;
                }
            }

            var bufferString = buffer.ToString();

            OnTcpRead(bufferString.Length < 200
                          ? new TcpReadEventArgs(bufferString)
                          : new TcpReadEventArgs("long data"));

            if (lastline.StartsWith(checkStamp + " OK") ||
                temp.ToLower().StartsWith("* " + command.Split(' ')[0].ToLower()) || temp.StartsWith("+ "))
                return bufferString;

            var failedString = string.Format("Command \"{0}\" failed : {1}",
                                             command.StartsWith("login") ? "LOGIN *****" : command, bufferString);

            throw new Imap4Exception(failedString);
        }
示例#2
0
 public IAsyncResult BeginCommand(string command, string stamp, string checkStamp, AsyncCallback callback,
                                  CommandOptions options)
 {
     this._delegateCommandStringStringString = this.Command;
     return this._delegateCommandStringStringString.BeginInvoke(command, stamp, checkStamp, options, callback,
                                                                this._delegateCommandStringStringString);
 }
示例#3
0
        public string Command(string command, string stamp, CommandOptions options)
        {
            if (options == null)
                options = new CommandOptions();

            /*if (command.Length < 200) this.OnTcpWriting(new ActiveUp.Net.Mail.TcpWritingEventArgs(stamp + ((stamp.Length > 0) ? " " : "") + command + "\r\n"));
            else this.OnTcpWriting(new ActiveUp.Net.Mail.TcpWritingEventArgs("long command data"));*/
            //base.GetStream().Write(System.Text.Encoding.ASCII.GetBytes(stamp + ((stamp.Length > 0) ? " " : "") + command + "\r\n\r\n"), 0, stamp.Length + ((stamp.Length > 0) ? 1 : 0) + command.Length + 2);

            // Although I have still not read all the relevant code but here it looks that you are
            // directly trying to write to the network stream which is incorrect. I have commented your
            // line above writing directly to network stream and have slightly changed it to write to
            // sslstream. I am unable to biuld this solution as 200+ missing file errors are shown. But
            // I have run the NUnit test twice and it is passing succesfully therefore I have not checked
            // the reading portion from ssl stream. Theoreticaly decrytpion exception should only get generated
            // when there is a problem with reading from ssl stream but may be because direct attempt was made
            // to write to Network stream so some how it threw decryption exception.
            // please see it run and test it--------Atif

            // Complement the Atif changes. Use the flag for !PocketPC config for avoid build errors.

            GetStream()
                .Write(
                    Encoding.GetEncoding("iso-8859-1")
                            .GetBytes(stamp + ((stamp.Length > 0) ? " " : "") + command + "\r\n\r\n"), 0,
                    stamp.Length + ((stamp.Length > 0) ? 1 : 0) + command.Length + 2);

            OnTcpWritten(command.Length < 200
                             ? new TcpWrittenEventArgs(stamp + ((stamp.Length > 0) ? " " : "") +
                                                       command + "\r\n")
                             : new TcpWrittenEventArgs("long command data"));
            OnTcpReading();

            var buffer = new StringBuilder();

            var commandAsUpper = command.ToUpper();
            string temp;
            string lastline;
            using (
                var sr = new System.IO.StreamReader(GetStream(), Encoding.GetEncoding("iso-8859-1"), false,
                                                    Client.ReceiveBufferSize, true))
            {
                sr.BaseStream.ReadTimeout = Client.ReceiveTimeout;
                while (true)
                {
                    temp = sr.ReadLine();
                    Logger.AddEntry("bordel : " + temp);
                    buffer.Append(temp + "\r\n");
                    if (commandAsUpper.StartsWith("LIST") || commandAsUpper.StartsWith("XLIST") ||
                        commandAsUpper.StartsWith("APPEND"))
                    {
                        if (temp != null &&
                            (temp.StartsWith(stamp) || (temp.StartsWith("+ ") && options.IsPlusCmdAllowed)))
                        {
                            lastline = temp;
                            break;
                        }
                    }

                    else if (commandAsUpper.StartsWith("DONE"))
                    {
                        lastline = temp;
                        if (lastline != null) stamp = lastline.Split(' ')[0];
                        break;
                    }
                    else if (temp != null)
                    {
                        //Please add comments as to why here, and reimplement differently
                        if (temp.StartsWith(stamp) || temp.ToLower().StartsWith("* " + command.Split(' ')[0].ToLower()))
                        {
                            lastline = temp;
                            break;
                        }
                    }
                    else
                        throw new EndOfStreamException("Unexpected end of stream");

                }
            }
            var bufferString = buffer.ToString();

            OnTcpRead(buffer.Length < 200
                          ? new TcpReadEventArgs(bufferString)
                          : new TcpReadEventArgs("long data"));
            if (lastline != null &&
                (lastline.StartsWith(stamp + " OK") || temp.ToLower().StartsWith("* " + command.Split(' ')[0].ToLower()) ||
                 temp.StartsWith("+ ")))
                return bufferString;

            var failedString = string.Format("Command \"{0}\" failed : {1}",
                                             command.StartsWith("login") ? "LOGIN *****" : command, bufferString);

            throw new Imap4Exception(failedString);
        }
示例#4
0
 public string Command(string command, CommandOptions options)
 {
     return this.Command(command,
                         System.DateTime.Now.ToString("yyMMddhhmmss" + System.DateTime.Now.Millisecond.ToString()),
                         options);
 }
示例#5
0
        public string Command(string command, string stamp, string checkStamp, CommandOptions options = null)
        {
            if (options == null)
                options = new CommandOptions();

            if (command.Length < 200) this.OnTcpWriting(new ActiveUp.Net.Mail.TcpWritingEventArgs(stamp + ((stamp.Length > 0) ? " " : "") + command + "\r\n"));
            else this.OnTcpWriting(new ActiveUp.Net.Mail.TcpWritingEventArgs("long command data"));
            base.GetStream().Write(System.Text.Encoding.ASCII.GetBytes(stamp + ((stamp.Length > 0) ? " " : "") + command + "\r\n"), 0, stamp.Length + ((stamp.Length > 0) ? 1 : 0) + command.Length + 2);
            if (command.Length < 200) this.OnTcpWritten(new ActiveUp.Net.Mail.TcpWrittenEventArgs(stamp + ((stamp.Length > 0) ? " " : "") + command + "\r\n"));
            else this.OnTcpWritten(new ActiveUp.Net.Mail.TcpWrittenEventArgs("long command data"));
            this.OnTcpReading();
            System.IO.StreamReader sr = new System.IO.StreamReader(base.GetStream(), true);
            System.Text.StringBuilder buffer = new System.Text.StringBuilder();
            string temp = "";
            string lastline = "";
            while (true)
            {
                temp = sr.ReadLine();
                buffer.Append(temp + "\r\n");
                if (temp.StartsWith("+ ") && options.IsPlusCmdAllowed)
                {
                    lastline = temp;
                    break;
                }

                if (temp.StartsWith(checkStamp) || temp.ToLower().StartsWith("* " + command.Split(' ')[0].ToLower()))
                {
                    lastline = temp;
                    break;
                }
            }
            if (buffer.Length < 200) this.OnTcpRead(new ActiveUp.Net.Mail.TcpReadEventArgs(buffer.ToString()));
            else this.OnTcpRead(new ActiveUp.Net.Mail.TcpReadEventArgs("long data"));
            if (lastline.StartsWith(checkStamp + " OK") || temp.ToLower().StartsWith("* " + command.Split(' ')[0].ToLower()) || temp.StartsWith("+ ")) return buffer.ToString();
            else throw new ActiveUp.Net.Mail.Imap4Exception("Command \"" + command + "\" failed : " + buffer.ToString());
        }
示例#6
0
 public IAsyncResult BeginCommand(string command, string stamp, AsyncCallback callback, CommandOptions options = null)
 {
     this._delegateCommand = this.Command;
     return this._delegateCommand.BeginInvoke(command, stamp, options, callback, this._delegateCommand);
 }
示例#7
0
        public string Command(string command, string stamp, CommandOptions options = null)
        {
            if (options == null)
                options = new CommandOptions();

            if (command.Length < 200) this.OnTcpWriting(new ActiveUp.Net.Mail.TcpWritingEventArgs(stamp + ((stamp.Length > 0) ? " " : "") + command + "\r\n"));
            else this.OnTcpWriting(new ActiveUp.Net.Mail.TcpWritingEventArgs("long command data"));
            //base.GetStream().Write(System.Text.Encoding.ASCII.GetBytes(stamp + ((stamp.Length > 0) ? " " : "") + command + "\r\n\r\n"), 0, stamp.Length + ((stamp.Length > 0) ? 1 : 0) + command.Length + 2);

            // Although I have still not read all the relevant code but here it looks that you are
            // directly trying to write to the network stream which is incorrect. I have commented your
            // line above writing directly to network stream and have slightly changed it to write to
            // sslstream. I am unable to biuld this solution as 200+ missing file errors are shown. But
            // I have run the NUnit test twice and it is passing succesfully therefore I have not checked
            // the reading portion from ssl stream. Theoreticaly decrytpion exception should only get generated
            // when there is a problem with reading from ssl stream but may be because direct attempt was made
            // to write to Network stream so some how it threw decryption exception.
            // please see it run and test it--------Atif

            // Complement the Atif changes. Use the flag for !PocketPC config for avoid build errors.

#if !PocketPC
            if (this._sslStream != null)
            {
                this._sslStream.Write(System.Text.Encoding.ASCII.GetBytes(stamp + ((stamp.Length > 0) ? " " : "") + command + "\r\n\r\n"), 0, stamp.Length + ((stamp.Length > 0) ? 1 : 0) + command.Length + 2);
            }
            else
            {
                base.GetStream().Write(System.Text.Encoding.ASCII.GetBytes(stamp + ((stamp.Length > 0) ? " " : "") + command + "\r\n\r\n"), 0, stamp.Length + ((stamp.Length > 0) ? 1 : 0) + command.Length + 2);
            }
#endif

#if PocketPC
                        base.GetStream().Write(System.Text.Encoding.ASCII.GetBytes(stamp + ((stamp.Length > 0) ? " " : "") + command + "\r\n\r\n"), 0, stamp.Length + ((stamp.Length > 0) ? 1 : 0) + command.Length + 2);
#endif

            if (command.Length < 200) this.OnTcpWritten(new ActiveUp.Net.Mail.TcpWrittenEventArgs(stamp + ((stamp.Length > 0) ? " " : "") + command + "\r\n"));
            else this.OnTcpWritten(new ActiveUp.Net.Mail.TcpWrittenEventArgs("long command data"));
            this.OnTcpReading();
            System.IO.StreamReader sr = new System.IO.StreamReader(this.GetStream());
            System.Text.StringBuilder buffer = new System.Text.StringBuilder();

            var commandAsUpper = command.ToUpper();
            string temp = "";
            string lastline = "";
            while (true)
            {
                temp = sr.ReadLine();
                ActiveUp.Net.Mail.Logger.AddEntry("bordel : " + temp);
                buffer.Append(temp + "\r\n");
                if (commandAsUpper.StartsWith("LIST") == true)
                {
                    if (temp.StartsWith(stamp) || (temp.StartsWith("+ ") && options.IsPlusCmdAllowed))
                    {
                        lastline = temp;
                        break;
                    }
                }

                else if (commandAsUpper.StartsWith("DONE") == true)
                {
                    lastline = temp;
                    stamp = lastline.Split(' ')[0];
                    break;
                }
                else if (temp != null)
                {
                    //Had to remove + check - this was failing when the email contained a line with + 
                    //Please add comments as to why here, and reimplement differently
                    if (temp.StartsWith(stamp) || temp.ToLower().StartsWith("* " + command.Split(' ')[0].ToLower()) || (temp.StartsWith("+ ") && options.IsPlusCmdAllowed))
                    {
                        lastline = temp;
                        break;
                    }
                }
            }
            var bufferString = buffer.ToString();

            if (!sr.CurrentEncoding.Equals(Encoding.UTF8))
            {
                var utf8Bytes = Encoding.Convert(sr.CurrentEncoding, Encoding.UTF8, sr.CurrentEncoding.GetBytes(bufferString));
                bufferString = Encoding.UTF8.GetString(utf8Bytes);
            }
                
            if (buffer.Length < 200)
                this.OnTcpRead(new ActiveUp.Net.Mail.TcpReadEventArgs(bufferString));
            else 
                this.OnTcpRead(new ActiveUp.Net.Mail.TcpReadEventArgs("long data"));
            if (lastline.StartsWith(stamp + " OK") || temp.ToLower().StartsWith("* " + command.Split(' ')[0].ToLower()) || temp.StartsWith("+ "))
                return bufferString;
            else
                throw new ActiveUp.Net.Mail.Imap4Exception("Command \"" + command + "\" failed : " + bufferString);
        }