コード例 #1
0
        public virtual MailMessage[] GetMessages(string start, string end, bool uid, bool headersonly, bool setseen)
        {
            var x = new List <MailMessage>();

            GetMessages(start, end, uid, headersonly, setseen, (stream, size, imapHeaders) => {
                var mail = new MailMessage {
                    Encoding = Encoding
                };
                mail.Size = size;

                if (imapHeaders["UID"] != null)
                {
                    mail.Uid = imapHeaders["UID"];
                }

                if (imapHeaders["Flags"] != null)
                {
                    mail.SetFlags(imapHeaders["Flags"]);
                }

                mail.Load(_Stream, headersonly, mail.Size);

                foreach (var key in imapHeaders.AllKeys.Except(new[] { "UID", "Flags", "BODY[]", "BODY[HEADER]" }, StringComparer.OrdinalIgnoreCase))
                {
                    mail.Headers.Add(key, new HeaderValue(imapHeaders[key]));
                }

                x.Add(mail);

                return(mail);
            });

            return(x.ToArray());
        }
コード例 #2
0
        public virtual void GetMessages(string start, string end, bool uid, bool uidsonly, bool headersonly, bool setseen, Action <MailMessage> processCallback)
        {
            GetMessages(start, end, uid, uidsonly, headersonly, setseen, (stream, size, imapHeaders) =>
            {
                var mail = new MailMessage {
                    Encoding = Encoding
                };
                mail.Size = size;

                if (imapHeaders["UID"] != null)
                {
                    mail.Uid = imapHeaders["UID"];
                }

                if (imapHeaders["Flags"] != null)
                {
                    mail.SetFlags(imapHeaders["Flags"]);
                }

                mail.Load(_Stream, headersonly, mail.Size);

                foreach (var key in imapHeaders.AllKeys.Except(new[] { "UID", "Flags", "BODY[]", "BODY[HEADER]" }, StringComparer.OrdinalIgnoreCase))
                {
                    mail.Headers.Add(key, new HeaderValue(imapHeaders[key]));
                }

                processCallback?.Invoke(mail);

                return(mail);
            });
        }
コード例 #3
0
ファイル: ImapClient.cs プロジェクト: Comp-Ex/aenetmail
        public virtual MailMessage[] GetMessages(string start, string end, bool uid, bool headersonly, bool setseen)
        {
            var x = new List<MailMessage>();

            GetMessages(start, end, uid, headersonly, setseen, (stream, size, imapHeaders) => {
                var mail = new MailMessage { Encoding = Encoding };
                mail.Size = size;

                if (imapHeaders["UID"] != null)
                    mail.Uid = imapHeaders["UID"];

                if (imapHeaders["Flags"] != null)
                    mail.SetFlags(imapHeaders["Flags"]);

                mail.Load(_Stream, headersonly, mail.Size);

                foreach (var key in imapHeaders.AllKeys.Except(new[] { "UID", "Flags", "BODY[]", "BODY[HEADER]" }, StringComparer.OrdinalIgnoreCase))
                    mail.Headers.Add(key, new HeaderValue(imapHeaders[key]));

                x.Add(mail);

                return mail;
            });

            return x.ToArray();
        }
コード例 #4
0
ファイル: ImapClient.cs プロジェクト: ewrpi/HomeApps
        public MailMessage[] GetMessages(string start, string end, bool uid, bool headersonly, bool setseen)
        {
            CheckMailboxSelected();
            IdlePause();

            string UID, HEADERS, SETSEEN;

            UID = HEADERS = SETSEEN = String.Empty;
            if (uid)
            {
                UID = "UID ";
            }
            if (headersonly)
            {
                HEADERS = "HEADER";
            }
            if (setseen)
            {
                SETSEEN = ".PEEK";
            }
            string tag      = GetTag();
            string command  = tag + UID + "FETCH " + start + ":" + end + " (UID RFC822.SIZE FLAGS BODY" + SETSEEN + "[" + HEADERS + "])";
            string response = SendCommandGetResponse(command);
            var    x        = new List <MailMessage>();
            string reg      = @"\* \d+ FETCH.*?BODY.*?\{(\d+)\}";
            Match  m        = Regex.Match(response, reg);

            while (m.Groups.Count > 1)
            {
                int         bodylen = Convert.ToInt32(m.Groups[1].ToString());
                MailMessage mail    = new MailMessage();
                char[]      body    = new char[bodylen];
                int         total   = 0;
                while (total < bodylen)
                {
                    int read = _Reader.Read(body, total, bodylen - total);
                    total += read;
                }

                string message = new string(body);

                Match m2 = Regex.Match(response, @"UID (\d+)");
                if (m2.Groups[1] != null)
                {
                    mail.Uid = m2.Groups[1].ToString();
                }
                m2 = Regex.Match(response, @"FLAGS \((.*?)\)");
                if (m2.Groups[1] != null)
                {
                    mail.SetFlags(m2.Groups[1].ToString());
                }
                m2 = Regex.Match(response, @"RFC822\.SIZE (\d+)");
                if (m2.Groups[1] != null)
                {
                    mail.Size = Convert.ToInt32(m2.Groups[1].ToString());
                }
                mail.Load(new string(body), headersonly);
                x.Add(mail);
                response = _Reader.ReadLine(); // read last line terminated by )
                response = _Reader.ReadLine(); // read next line
                m        = Regex.Match(response, reg);
            }

            IdleResume();
            return(x.ToArray());
        }
コード例 #5
0
        public virtual MailMessage[] GetMessages(string start, string end, bool uid, bool headersonly, bool setseen)
        {
            CheckMailboxSelected();
            IdlePause();

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

            string response;
            var    x = new List <MailMessage>();

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

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

                var mail = new MailMessage {
                    Encoding = Encoding
                };
                var imapHeaders = ParseImapHeader(response.Substring(response.IndexOf('(') + 1));
                mail.Size = (imapHeaders["BODY[HEADER]"] ?? imapHeaders["BODY[]"]).Trim('{', '}').ToInt();

                if (imapHeaders["UID"] != null)
                {
                    mail.Uid = imapHeaders["UID"];
                }

                if (imapHeaders["Flags"] != null)
                {
                    mail.SetFlags(imapHeaders["Flags"]);
                }


                foreach (var key in imapHeaders.AllKeys.Except(new[] { "UID", "Flags", "BODY[]", "BODY[HEADER]" }, StringComparer.OrdinalIgnoreCase))
                {
                    mail.Headers.Add(key, new HeaderValue(imapHeaders[key]));
                }

                //using (var body = new System.IO.MemoryStream()) {
                //  int remaining = mail.Size;
                //  var buffer = new byte[8192];
                //  int read;
                //  while (remaining > 0) {
                //    read = _Stream.Read(buffer, 0, Math.Min(remaining, buffer.Length));
                //    body.Write(buffer, 0, read);
                //    remaining -= read;
                //  }

                //  var next = Convert.ToChar(_Stream.ReadByte());
                //  System.Diagnostics.Debug.Assert(next == ')');

                //  body.Position = 0;
                //  mail.Load(body, headersonly);
                //}

                mail.Load(_Stream, headersonly, mail.Size);

                var n = Convert.ToChar(_Stream.ReadByte());
                System.Diagnostics.Debug.Assert(n == ')');

                x.Add(mail);
            }

            IdleResume();
            return(x.ToArray());
        }
コード例 #6
0
ファイル: ImapClient.cs プロジェクト: miguelerm/aenetmail
        public MailMessage[] GetMessages(string start, string end, bool uid, bool headersonly, bool setseen)
        {
            CheckMailboxSelected();
            IdlePause();

            string UID, HEADERS, SETSEEN;

            UID = HEADERS = SETSEEN = String.Empty;
            if (uid)
            {
                UID = "UID ";
            }
            if (headersonly)
            {
                HEADERS = "HEADER";
            }
            if (setseen)
            {
                SETSEEN = ".PEEK";
            }
            string tag      = GetTag();
            string command  = tag + UID + "FETCH " + start + ":" + end + " (UID RFC822.SIZE FLAGS BODY" + SETSEEN + "[" + HEADERS + "])";
            string response = SendCommandGetResponse(command);
            var    x        = new List <MailMessage>();
            string reg      = @"\* \d+ FETCH.*?BODY.*?\{(\d+)\}";
            Match  m        = Regex.Match(response, reg);
            string bodies   = String.Empty;

            while (m.Groups.Count > 1)
            {
                int         bodyremaininglen = Convert.ToInt32(m.Groups[1].ToString());
                MailMessage mail             = new MailMessage();
                //char[] body = new char[bodylen];
                string body = String.Empty;
                while (bodyremaininglen > 0)
                {
                    bodies += GetResponse();
                    if (bodyremaininglen < bodies.Length)
                    {
                        body            += bodies.Substring(0, bodyremaininglen);
                        bodyremaininglen = 0;
                        bodies           = bodies.Remove(0);
                    }
                    else
                    {
                        body             += bodies + Environment.NewLine;
                        bodyremaininglen -= bodies.Length + 2; //extra 2 for CRLF
                        bodies            = "";
                    }
                }

                Match m2 = Regex.Match(response, @"UID (\d+)");
                if (m2.Groups[1] != null)
                {
                    mail.Uid = m2.Groups[1].ToString();
                }
                m2 = Regex.Match(response, @"FLAGS \((.*?)\)");
                if (m2.Groups[1] != null)
                {
                    mail.SetFlags(m2.Groups[1].ToString());
                }
                m2 = Regex.Match(response, @"RFC822\.SIZE (\d+)");
                if (m2.Groups[1] != null)
                {
                    mail.Size = Convert.ToInt32(m2.Groups[1].ToString());
                }
                mail.Load(body, headersonly);
                x.Add(mail);
                response = GetResponse(); // read last line terminated by )
                response = GetResponse(); // read next line
                m        = Regex.Match(response, reg);
            }

            IdleResume();
            return(x.ToArray());
        }
コード例 #7
0
ファイル: ImapClient.cs プロジェクト: ogazitt/aenetmail
        public MailMessage[] GetMessages(string start, string end, bool uid, bool headersonly, bool setseen)
        {
            CheckMailboxSelected();
              IdlePause();

              string UID, HEADERS, SETSEEN;
              UID = HEADERS = SETSEEN = String.Empty;
              if (uid) UID = "UID ";
              if (headersonly) HEADERS = "HEADER";
              if (setseen) SETSEEN = ".PEEK";
              string tag = GetTag();
              string command = tag + UID + "FETCH " + start + ":" + end + " (UID RFC822.SIZE FLAGS BODY" + SETSEEN + "[" + HEADERS + "])";
              string response = SendCommandGetResponse(command);
              var x = new List<MailMessage>();
              string reg = @"\* \d+ FETCH.*?BODY.*?\{(\d+)\}";
              Match m = Regex.Match(response, reg);
              while (m.Groups.Count > 1) {
            int bodylen = Convert.ToInt32(m.Groups[1].ToString());
            MailMessage mail = new MailMessage();
            char[] body = new char[bodylen];
            int total = 0;
            while (total < bodylen) {
              int read = _Reader.Read(body, total, bodylen - total);
              total += read;
            }

            string message = new string(body);

            Match m2 = Regex.Match(response, @"UID (\d+)");
            if (m2.Groups[1] != null) mail.Uid = m2.Groups[1].ToString();
            m2 = Regex.Match(response, @"FLAGS \((.*?)\)");
            if (m2.Groups[1] != null) mail.SetFlags(m2.Groups[1].ToString());
            m2 = Regex.Match(response, @"RFC822\.SIZE (\d+)");
            if (m2.Groups[1] != null) mail.Size = Convert.ToInt32(m2.Groups[1].ToString());
            mail.Load(new string(body), headersonly);
            x.Add(mail);
            response = _Reader.ReadLine(); // read last line terminated by )
            response = _Reader.ReadLine(); // read next line
            m = Regex.Match(response, reg);
              }

              IdleResume();
              return x.ToArray();
        }
コード例 #8
0
ファイル: ImapClient.cs プロジェクト: meehi/aenetmail
        public MailMessage[] GetMessages(string start, string end, bool uid, bool headersonly, bool setseen)
        {
            CheckMailboxSelected();
            IdlePause();

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

            string response;
            var x = new List<MailMessage>();

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

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

                var mail = new MailMessage { Encoding = Encoding };
                var imapHeaders = ParseImapHeader(response.Substring(response.IndexOf('(') + 1));
                mail.Size = (imapHeaders["BODY[HEADER]"] ?? imapHeaders["BODY[]"]).Trim('{', '}').ToInt();

                if (imapHeaders["UID"] != null)
                    mail.Uid = imapHeaders["UID"];

                if (imapHeaders["Flags"] != null)
                    mail.SetFlags(imapHeaders["Flags"]);

                foreach (var key in imapHeaders.AllKeys.Except(new[] { "UID", "Flags", "BODY[]", "BODY[HEADER]" }, StringComparer.OrdinalIgnoreCase))
                    mail.Headers.Add(key, new HeaderValue(imapHeaders[key]));

                using (var body = new System.IO.MemoryStream())
                {
                    int remaining = mail.Size;
                    var buffer = new byte[8192];
                    int read;
                    string pattern = "charset=";
                    string charset = "";
                    string temp_charset = Encoding.BodyName;
                    while (remaining > 0)
                    {
                        read = _Stream.Read(buffer, 0, Math.Min(remaining, buffer.Length));
                        string temp_body = Encoding.GetString(buffer, 0, read);
                        temp_body = temp_body.Replace("\"", "").Replace(" ", "");
                        if (temp_body.ToLower().Contains(pattern))
                        {
                            int start_pos = temp_body.IndexOf(pattern) + pattern.Length;
                            int end_pos = temp_body.IndexOf("\r\n", start_pos);
                            if (end_pos == -1)
                            {
                                end_pos = temp_body.IndexOf("\r", start_pos);
                                if (end_pos == -1)
                                    end_pos = temp_body.IndexOf(";", start_pos);
                            }
                            if (end_pos != -1)
                            {
                                int length = end_pos - start_pos;
                                if (length > 0)
                                {
                                    charset = temp_body.Substring(start_pos, length).Replace(";", "");
                                    if (charset != "" && !charset.Contains("binaryname") && charset != temp_charset)
                                    {
                                        Encoding = Utilities.ParseCharsetToEncoding(charset, Encoding);
                                        temp_charset = Encoding.BodyName;
                                    }
                                }
                            }
                        }

                        body.Write(buffer, 0, read);
                        remaining -= read;
                    }

                    var next = Convert.ToChar(_Stream.ReadByte());
                    System.Diagnostics.Debug.Assert(next == ')');

                    body.Position = 0;
                    using (var rdr = new System.IO.StreamReader(body, Encoding))
                    {
                        mail.Load(rdr, headersonly);
                    }
                }

                x.Add(mail);
            }

            IdleResume();
            return x.ToArray();
        }
コード例 #9
0
ファイル: ImapClient.cs プロジェクト: piher/aenetmail
        public MailMessage[] GetMessages(string start, string end, bool uid, bool headersonly, bool setseen)
        {
            CheckMailboxSelected();
              IdlePause();

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

              string response;
              var x = new List<MailMessage>();

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

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

            var mail = new MailMessage();
            var imapHeaders = ParseImapHeader(response.Substring(response.IndexOf('(') + 1));
            if (imapHeaders["UID"] != null)
              mail.Uid = imapHeaders["UID"];

            if (imapHeaders["Flags"] != null)
              mail.SetFlags(imapHeaders["Flags"]);

            if (imapHeaders["RFC822.SIZE"] != null)
              mail.Size = imapHeaders["RFC822.SIZE"].ToInt();

            foreach (var key in imapHeaders.AllKeys.Except(new[] { "UID", "Flags", "RFC822.SIZE", "BODY[]", "BODY[HEADER]" }, StringComparer.OrdinalIgnoreCase))
              mail.Headers.Add(key, new HeaderValue(imapHeaders[key]));

            int bodySize = (imapHeaders["BODY[HEADER]"] ?? imapHeaders["BODY[]"]).Trim('{', '}').ToInt();

            int size = Math.Min(bodySize, mail.Size > 0 ? mail.Size : bodySize);
            var body = new StringBuilder();
            var buffer = new char[8192];
            int read;
            while (size > 0) {
              read = _Reader.Read(buffer, 0, Math.Min(size, buffer.Length));
              body.Append(buffer, 0, read);
              size -= read;
              if (size == 0) {
            if (_Reader.Peek() != ')') {
              size = Math.Max(bodySize, mail.Size) - Math.Min(bodySize, mail.Size > 0 ? mail.Size : bodySize);
              bodySize = Math.Max(bodySize, mail.Size);
            }
              }
            }

            mail.Load(body.ToString(), headersonly);

            x.Add(mail);
              }

              IdleResume();
              return x.ToArray();
        }
コード例 #10
0
ファイル: ImapClient.cs プロジェクト: seanwm/aenetmail
        public MailMessage[] GetMessages(string start, string end, bool uid, bool headersonly, bool setseen)
        {
            CheckMailboxSelected();
              IdlePause();

              string UID, HEADERS, SETSEEN;
              UID = HEADERS = SETSEEN = String.Empty;
              if (uid) UID = "UID ";
              if (headersonly) HEADERS = "HEADER";
              if (setseen) SETSEEN = ".PEEK";
              string tag = GetTag();
              string command = tag + UID + "FETCH " + start + ":" + end + " (UID RFC822.SIZE FLAGS BODY" + SETSEEN + "[" + HEADERS + "])";
              string response = SendCommandGetResponse(command);
              var x = new List<MailMessage>();
              string reg = @"\* \d+ FETCH.*?BODY.*?\{(\d+)\}";
              Match m = Regex.Match(response, reg);
              string bodies = String.Empty;
              while (m.Groups.Count > 1) {
            int bodyremaininglen = Convert.ToInt32(m.Groups[1].ToString());
            MailMessage mail = new MailMessage();
            //char[] body = new char[bodylen];
            string body = String.Empty;
            while (bodyremaininglen > 0) {
              bodies += GetResponse();
              if (bodyremaininglen < bodies.Length) {
            body += bodies.Substring(0, bodyremaininglen);
            bodyremaininglen = 0;
            bodies = bodies.Remove(0);
              } else {
            body += bodies + Environment.NewLine;
            bodyremaininglen -= bodies.Length + 2;  //extra 2 for CRLF
            bodies = "";
              }
            }

            Match m2 = Regex.Match(response, @"UID (\d+)");
            if (m2.Groups[1] != null) mail.Uid = m2.Groups[1].ToString();
            m2 = Regex.Match(response, @"FLAGS \((.*?)\)");
            if (m2.Groups[1] != null) mail.SetFlags(m2.Groups[1].ToString());
            m2 = Regex.Match(response, @"RFC822\.SIZE (\d+)");
            if (m2.Groups[1] != null) mail.Size = Convert.ToInt32(m2.Groups[1].ToString());
            mail.Load(body, headersonly);
            x.Add(mail);
            response = GetResponse(); // read last line terminated by )
            m = Regex.Match(response, reg);
              }

              IdleResume();
              return x.ToArray();
        }
コード例 #11
0
ファイル: ImapClient.cs プロジェクト: harold4/aenetmail
        public void GetMessages(string start, string end, bool uid, bool uidsonly, bool headersonly, bool setseen, Action<MailMessage> processCallback)
        {
            GetMessages(start, end, uid, uidsonly, headersonly, setseen, (stream, size, imapHeaders) =>
            {
                var mail = new MailMessage { Encoding = Encoding };
                mail.Size = size;

                if (imapHeaders["UID"] != null)
                    mail.Uid = imapHeaders["UID"];

                if (imapHeaders["Flags"] != null)
                    mail.SetFlags(imapHeaders["Flags"]);

                mail.Load(stream, headersonly, mail.Size);

                foreach (var key in imapHeaders.AllKeys.Except(new[] { "UID", "Flags", "BODY[]", "BODY[HEADER]" }, StringComparer.OrdinalIgnoreCase))
                    mail.Headers.Add(key, new HeaderValue(imapHeaders[key]));

                processCallback(mail);

                return mail;
            });
        }
コード例 #12
0
        public MailMessage[] GetMessages(string start, string end, bool uid, bool headersonly, bool setseen)
        {
            CheckMailboxSelected();
            IdlePause();

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

            string response;
            var    x = new List <MailMessage>();

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

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

                var mail        = new MailMessage();
                var imapHeaders = ParseImapHeader(response.Substring(response.IndexOf('(') + 1));
                if (imapHeaders["UID"] != null)
                {
                    mail.Uid = imapHeaders["UID"];
                }

                if (imapHeaders["Flags"] != null)
                {
                    mail.SetFlags(imapHeaders["Flags"]);
                }

                if (imapHeaders["RFC822.SIZE"] != null)
                {
                    mail.Size = imapHeaders["RFC822.SIZE"].ToInt();
                }

                foreach (var key in imapHeaders.AllKeys.Except(new[] { "UID", "Flags", "RFC822.SIZE", "BODY[]", "BODY[HEADER]" }, StringComparer.OrdinalIgnoreCase))
                {
                    mail.Headers.Add(key, new HeaderValue(imapHeaders[key]));
                }

                int bodySize = (imapHeaders["BODY[HEADER]"] ?? imapHeaders["BODY[]"]).Trim('{', '}').ToInt();

                int size   = Math.Min(bodySize, mail.Size > 0 ? mail.Size : bodySize);
                var body   = new StringBuilder();
                var buffer = new char[8192];
                int read;
                while (size > 0)
                {
                    read = _Reader.Read(buffer, 0, Math.Min(size, buffer.Length));
                    body.Append(buffer, 0, read);
                    size -= read;
                    if (size == 0)
                    {
                        if (_Reader.Peek() != ')')
                        {
                            size     = Math.Max(bodySize, mail.Size) - Math.Min(bodySize, mail.Size > 0 ? mail.Size : bodySize);
                            bodySize = Math.Max(bodySize, mail.Size);
                        }
                    }
                }

                mail.Load(body.ToString(), headersonly);

                x.Add(mail);
            }

            IdleResume();
            return(x.ToArray());
        }
コード例 #13
0
        public MailMessage[] GetMessages(string start, string end, bool uid, bool headersonly, bool setseen, string[] fields)
        {
            CheckMailboxSelected();
            IdlePause();

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

            string response;
            var    x = new List <MailMessage>();

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

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

                var mail = new MailMessage {
                    Encoding = Encoding
                };
                var regex = new Regex(@"UID\s(?<uid>[0-9]*)\sFLAGS\s\((?<flags>.*?)\)\sBODY\[.*?\]\s\{(?<size>[0-9]*)\}");
                var match = regex.Match(response);
                //var imapHeaders = ParseImapHeader(response.Substring(response.IndexOf('(') + 1));
                mail.Size = match.Groups["size"].Value.ToInt();

                if (match.Groups["uid"].Success)
                {
                    mail.Uid = match.Groups["uid"].Value;
                }

                if (match.Groups["flags"].Success)
                {
                    mail.SetFlags(match.Groups["flags"].Value);
                }

                /*
                 * foreach (
                 *  var key in
                 *      imapHeaders.AllKeys.Except(new[] {"UID", "Flags", "BODY[]", "BODY[HEADER]"},
                 *                                 StringComparer.OrdinalIgnoreCase))
                 *  mail.Headers.Add(key, new HeaderValue(imapHeaders[key]));
                 */
                //using (var body = new System.IO.MemoryStream()) {
                //  int remaining = mail.Size;
                //  var buffer = new byte[8192];
                //  int read;
                //  while (remaining > 0) {
                //    read = _Stream.Read(buffer, 0, Math.Min(remaining, buffer.Length));
                //    body.Write(buffer, 0, read);
                //    remaining -= read;
                //  }

                //  var next = Convert.ToChar(_Stream.ReadByte());
                //  System.Diagnostics.Debug.Assert(next == ')');

                //  body.Position = 0;
                //  mail.Load(body, headersonly);
                //}

                mail.Load(_Stream, headersonly, mail.Size);

                x.Add(mail);
            }

            IdleResume();
            return(x.ToArray());
        }
コード例 #14
-1
ファイル: ImapClient.cs プロジェクト: kareem613/aenetmail
        public MailMessage[] GetMessages(string start, string end, bool uid, bool headersonly, bool setseen)
        {
            CheckMailboxSelected();
              IdlePause();

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

              string response;
              var x = new List<MailMessage>();

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

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

            var mail = new MailMessage { Encoding = Encoding };
            var imapHeaders = ParseImapHeader(response.Substring(response.IndexOf('(') + 1));
            mail.Size = (imapHeaders["BODY[HEADER]"] ?? imapHeaders["BODY[]"]).Trim('{', '}').ToInt();

            if (imapHeaders["UID"] != null)
              mail.Uid = imapHeaders["UID"];

            if (imapHeaders["Flags"] != null)
              mail.SetFlags(imapHeaders["Flags"]);

            foreach (var key in imapHeaders.AllKeys.Except(new[] { "UID", "Flags", "BODY[]", "BODY[HEADER]" }, StringComparer.OrdinalIgnoreCase))
              mail.Headers.Add(key, new HeaderValue(imapHeaders[key]));

            //using (var body = new System.IO.MemoryStream()) {
            //  int remaining = mail.Size;
            //  var buffer = new byte[8192];
            //  int read;
            //  while (remaining > 0) {
            //    read = _Stream.Read(buffer, 0, Math.Min(remaining, buffer.Length));
            //    body.Write(buffer, 0, read);
            //    remaining -= read;
            //  }

            //  var next = Convert.ToChar(_Stream.ReadByte());
            //  System.Diagnostics.Debug.Assert(next == ')');

            //  body.Position = 0;
            //  mail.Load(body, headersonly);
            //}

            mail.Load(_Stream, headersonly, mail.Size);

            var n = Convert.ToChar(_Stream.ReadByte());
            System.Diagnostics.Debug.Assert(n == ')');

            x.Add(mail);
              }

              IdleResume();
              return x.ToArray();
        }