Example #1
0
        /// <summary>Retrieves a message header by its internal number (1..<see cref="ImapMailboxInfo.Exist" />).</summary>
        /// <param name="number">The internal message number (1..<see cref="ImapMailboxInfo.Exist" />).</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentOutOfRangeException"></exception>
        /// <exception cref="System.FormatException"></exception>
        public Rfc822Message GetMessageHeader(int number)
        {
            if (number < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(number));
            }

            while (true)
            {
                var answer = SendCommand("FETCH " + number + " BODY[HEADER]");
                if (answer.Data.Length == 0)
                {
                    continue;
                }

                var streamReader = answer.GetStreamReader(0);
                var header       = streamReader.ReadLine();
                var size         = int.Parse(header.Substring(header.LastIndexOf('{')).Unbox("{", "}", true));
                var dataReader   = answer.GetDataReader(header.Length + 2);
                var messageData  = dataReader.ReadBytes(size);
                if (messageData.Length != size)
                {
                    throw new FormatException();
                }

                var message = Rfc822Message.FromBinary(messageData);
                return(message);
            }
        }
Example #2
0
        /// <summary>Retrieves a message by its internal number (1..<see cref="ImapMailboxInfo.Exist" />).</summary>
        /// <param name="number">The internal message number (1..<see cref="ImapMailboxInfo.Exist" />).</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentOutOfRangeException"></exception>
        public Rfc822Message GetMessage(int number)
        {
            if (number < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(number));
            }

            var answer  = SendCommand("FETCH " + number + " BODY[]");
            var start   = Array.IndexOf(answer.Data, (byte)'\n') + 1;
            var end     = Array.LastIndexOf(answer.Data, (byte)')') - 2;
            var message = new byte[end - start];

            Array.Copy(answer.Data, start, message, 0, end - start);
            return(Rfc822Message.FromBinary(message));
        }