Esempio n. 1
0
        /// <summary>
        /// Parse IMAP output from a LIST, LSUB, or XLIST command.
        /// </summary>
        /// <param name="lineFromListCommand">Raw output line from a LIST, LSUB, or XLIST command.</param>
        /// <returns></returns>
        public static Mailbox CreateFromList(string lineFromListCommand)
        {
            // Ensure the list of flags is contained on this line.
            int startsFlagList = lineFromListCommand.IndexOf("(");
            int endFlagList = lineFromListCommand.IndexOf(")", startsFlagList + 1);
            if (startsFlagList > -1 && endFlagList > -1)
            {
                Mailbox mailbox = new Mailbox();

                string[] flags = lineFromListCommand.Substring(startsFlagList + 1, endFlagList - startsFlagList - 1).Split(' ');
                foreach (string flag in flags)
                {
                    if (!mailbox.Flags.Contains(flag))
                        mailbox.Flags.Add(flag);
                }

                // Ensure the hierarchy delimiter and name are returned.
                string[] remainingParts = lineFromListCommand.Substring(endFlagList + 2).Split(new char[] { ' ' }, 2);
                if (remainingParts.Length == 2)
                {
                    mailbox.HierarchyDelimiter = remainingParts[0].Replace("\"", "");

                    // Escape modifed UTF-7 encoding for ampersands or Unicode characters.
                    mailbox.Name = Functions.FromModifiedUTF7(remainingParts[1].Replace("\"", ""));

                    return mailbox;
                }
            }

            // No valid mailbox listing found, so return null.
            return null;
        }
Esempio n. 2
0
        /// <summary>
        /// Select a mailbox for subsequent operations and return its properties.
        /// </summary>
        /// <param name="mailboxName">Mailbox to work with.</param>
        /// <param name="qResyncParameters">Quick Resynchronization parameters, such as UIDValidity, the last known modification sequence, known UIDs, and/or known sequence ranges.</param>
        public async Task<Mailbox> SelectMailboxAsync(string mailboxName, string qResyncParameters)
        {
            // Protect against commands being called out of order.
            if (!IsAuthenticated)
                throw new ImapException("Must be connected to the server and authenticated prior to calling the SELECT command.");

            // Generate a unique command tag for tracking this command and its response.
            string commandTag = UniqueCommandTag();

            if (!string.IsNullOrEmpty(qResyncParameters))
                await SendCommandAsync(commandTag, "SELECT " + Functions.EscapeMailboxName(mailboxName) + " (QRESYNC (" + qResyncParameters + "))\r\n");
            else
                await SendCommandAsync(commandTag, "SELECT " + Functions.EscapeMailboxName(mailboxName) + "\r\n");

            string response = await ReadDataAsync(commandTag, "SELECT");

            if (LastCommandResult)
            {
                CurrentMailboxName = mailboxName;
                SessionIsMailboxSelected = true;

                CurrentMailbox = new Mailbox(mailboxName, response);
                return CurrentMailbox;
            }
            else
                return null;
        }