Example #1
0
 public EmailReadedEventArgs(StreamInterceptor streamInterceptor)
 {
     StreamInterceptor = streamInterceptor;
 }
Example #2
0
        public 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]" : "]"))
                    + ")";

            SendCommand(command);
            while (true)
            {
                string 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)
                {
                    RaiseWarning(null, "Expected BODY[] in stream, but received \"" + response + "\"");
                    break;
                }
                var size = (uidsonly ? 0 : body.Trim('{', '}').ToInt());
                MailMessage msg;
                using (StreamInterceptor streamInterceptor = new StreamInterceptor(Stream, size))
                {
                    msg = action(streamInterceptor.Stream, size, imapHeaders);
                    //msg = action(_Stream, size, imapHeaders);
                    if (!headersonly)
                    {
                        OnRfc822Readed(new EmailReadedEventArgs(streamInterceptor));
                    }
                }

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

            IdleResume();
        }