/// <summary>
        /// Provides a record-by-record processing functionality for the cmdlet.
        /// </summary>
        protected override void ProcessRecord()
        {
            // Create the Exchange Service Object and auth with bearer token
            ExchangeService exchangeService = new ExchangeService(new AuthenticationProvider(), this.SmtpAddress, RestEnvironment.OutlookBeta);

            MailFolder mailFolder = new MailFolder(exchangeService)
            {
                DisplayName = this.FolderName
            };

            mailFolder.Save(this.FolderRoot);

            FolderView         folderView         = new FolderView(10);
            FindFoldersResults findFoldersResults = null;

            do
            {
                // Find the new folder and output the result
                SearchFilter searchFilter = new SearchFilter.IsEqualTo(MailFolderObjectSchema.DisplayName, mailFolder.DisplayName);
                findFoldersResults = exchangeService.FindFolders(this.FolderRoot, searchFilter, folderView);
                folderView.Offset += folderView.PageSize;
                foreach (MailFolder folder in findFoldersResults)
                {
                    this.WriteObject(new PSObject(folder));
                }
            }while (findFoldersResults.MoreAvailable);
        }
        /// <summary>
        /// Create folder
        /// </summary>
        /// <param name="folderName"></param>
        /// <param name="exchangeService"></param>
        /// <returns></returns>
        internal static MailFolder CreateFolder(string folderName, ExchangeService exchangeService, WellKnownFolderName folderRoot)
        {
            MailFolder folder = new MailFolder(exchangeService);

            folder.DisplayName = folderName;
            folder.Save(folderRoot);

            return(folder);
        }
        /// <summary>
        /// Basic CRUD operations test.
        /// </summary>
        /// <param name="exchangeService"></param>
        public static void CreateReadUpdateDeleteMailFolder(ExchangeService exchangeService)
        {
            exchangeService.MailboxId = new MailboxId(AppConfig.MailboxA);

            foreach (MailFolder folder in exchangeService.FindFolders(WellKnownFolderName.Inbox, new FolderView(10)))
            {
                folder.Delete();
            }

            MailFolder folder1 = new MailFolder(exchangeService)
            {
                DisplayName = "MyTestFolder1"
            };

            Assert.IsNull(folder1.Id);
            folder1.Save(WellKnownFolderName.Inbox);
            Assert.IsNotNull(folder1.Id);

            MailFolder folder2 = new MailFolder(exchangeService);

            folder2.DisplayName = "MyTestFolder2";

            Assert.IsNull(folder2.Id);
            folder2.Save(WellKnownFolderName.Inbox);
            Assert.IsNotNull(folder2.Id);

            Thread.Sleep(5000);

            folder2 = folder2.Move(folder1.Id);

            folder1.DisplayName = "NewDisplayName";
            folder1.Update();

            Assert.AreEqual(
                "NewDisplayName",
                folder1.DisplayName);

            Assert.AreEqual(
                folder1.Id,
                folder2.ParentFolderId);

            folder2.Delete();
            Assert.IsNull(folder2.DisplayName);
            Assert.IsNull(folder2.Id);

            folder1.Delete();
            Assert.IsNull(folder1.DisplayName);
            Assert.IsNull(folder1.Id);
        }
        /// <summary>
        /// Test mail folder sync.
        /// </summary>
        /// <param name="exchangeService"></param>
        public static void SyncMailFolders(ExchangeService exchangeService)
        {
            string folder1Name = "TempSyncFolder1";
            string folder2Name = "TempSyncFolder2";

            exchangeService.MailboxId = new MailboxId(AppConfig.MailboxA);

            FindFoldersResults findFolders = exchangeService.FindFolders(
                WellKnownFolderName.MsgFolderRoot,
                new FolderView(30));

            foreach (MailFolder mailFolder in findFolders)
            {
                if (mailFolder.DisplayName == folder1Name ||
                    mailFolder.DisplayName == folder2Name)
                {
                    mailFolder.Delete();
                }
            }

            string syncState = null;
            int    counter   = 0;
            SyncMailFolderHierarchyResponse sync;

            do
            {
                sync      = exchangeService.SyncFolderHierarchy(syncState);
                syncState = sync.SyncState;

                counter++;
            } while (sync.MoreAvailable || counter == 4);

            Assert.IsFalse(sync.MoreAvailable);

            MailFolder folder1 = new MailFolder(exchangeService);

            folder1.DisplayName = folder1Name;
            folder1.Save(WellKnownFolderName.MsgFolderRoot);

            MailFolder folder2 = new MailFolder(exchangeService);

            folder2.DisplayName = folder2Name;
            folder2.Save(WellKnownFolderName.MsgFolderRoot);

            sync      = exchangeService.SyncFolderHierarchy(syncState);
            syncState = sync.SyncState;

            Assert.AreEqual(
                2,
                sync.TotalCount);

            foreach (ItemChange <MailFolder> change in sync)
            {
                Assert.IsTrue(change.ChangeType == SyncChangeType.Created);
            }

            folder1.Delete();
            folder2.Delete();

            sync = exchangeService.SyncFolderHierarchy(syncState);

            Assert.IsTrue(sync.TotalCount == 2);
            foreach (ItemChange <MailFolder> change in sync)
            {
                Assert.IsTrue(change.ChangeType == SyncChangeType.Deleted);
            }
        }