Esempio n. 1
0
        public virtual void GetMessages(string start, string end, bool uid, bool uidsonly, bool headersonly, bool setseen, Func <System.IO.Stream, int, NameValueCollection, MailMessage> action)
        {
            CheckMailboxSelected();
            IdlePause();

            string tag     = GetTag();
            string command = tag + (uid ? "UID " : null)
                             + "FETCH " + start + ":" + end + " ("
                             + _FetchHeaders + "UID FLAGS"
                             + (uidsonly ? null : (setseen ? " BODY[" : " BODY.PEEK[") + (headersonly ? "HEADER]" : "]"))
                             + ")";

            string response;

            SendCommand(command);
            while (true)
            {
                response = GetResponse();
                if (string.IsNullOrEmpty(response) || response.Contains(tag + "OK"))
                {
                    break;
                }

                if (response[0] != '*' || !response.Contains("FETCH ("))
                {
                    continue;
                }

                var    imapHeaders = Utilities.ParseImapHeader(response.Substring(response.IndexOf('(') + 1));
                String body        = (imapHeaders["BODY[HEADER]"] ?? imapHeaders["BODY[]"]);
                if (body == null && !uidsonly)
                {
                    System.Diagnostics.Debugger.Break();
                    RaiseWarning(null, "Expected BODY[] in stream, but received \"" + response + "\"");
                    break;
                }
                var size = (uidsonly ? 0 : body.Trim('{', '}').ToInt());
                var msg  = action(_Stream, size, imapHeaders);

                // with only uids we have no body and the closing bracket is on the same line
                if (!uidsonly)
                {
                    response = GetResponse();
                    if (response == null)
                    {
                        System.Diagnostics.Debugger.Break();
                        RaiseWarning(null, "Expected \")\" in stream, but received nothing");
                        break;
                    }
                }
                var n = response.Trim().LastOrDefault();
                if (n != ')')
                {
                    System.Diagnostics.Debugger.Break();
                    RaiseWarning(null, "Expected \")\" in stream, but received \"" + response + "\"");
                }
            }

            IdleResume();
        }
Esempio n. 2
0
        public virtual void GetMessages(string start, string end, bool uid, bool headersonly, bool setseen, Func <System.IO.Stream, int, NameValueCollection, MailMessage> action)
        {
            CheckMailboxSelected();
            IdlePause();

            string tag     = GetTag();
            string command = tag + (uid ? "UID " : null)
                             + "FETCH " + start + ":" + end + " ("
                             + _FetchHeaders + "UID FLAGS BODY"
                             + (setseen ? null : ".PEEK")
                             + "[" + (headersonly ? "HEADER" : null) + "])";

            string response;

            SendCommand(command);
            while (true)
            {
                response = GetResponse();
                if (string.IsNullOrEmpty(response) || response.Contains(tag + "OK"))
                {
                    break;
                }

                if (response[0] != '*' || !response.Contains("FETCH ("))
                {
                    continue;
                }

                var imapHeaders = Utilities.ParseImapHeader(response.Substring(response.IndexOf('(') + 1));
                var size        = (imapHeaders["BODY[HEADER]"] ?? imapHeaders["BODY[]"]).Trim('{', '}').ToInt();
                var msg         = action(this.Stream, size, imapHeaders);

                response = GetResponse();
                var n = response.Trim().LastOrDefault();
                if (n != ')')
                {
                    if (System.Diagnostics.Debugger.IsAttached)
                    {
                        System.Diagnostics.Debugger.Break();
                    }
                    else
                    {
                        throw new UnexpectedResponseException(response);
                    }
                    RaiseWarning(null, "Expected \")\" in stream, but received \"" + response + "\"");
                }
            }

            IdleResume();
        }