/// <summary>
        /// CRUD operations for Inbox rules.
        /// </summary>
        /// <param name="exchangeService">Exchange service.</param>
        public static async System.Threading.Tasks.Task CreateReadUpdateDeleteInboxRuleAsync(ExchangeService exchangeService)
        {
            MessageRule messageRule = new MessageRule(exchangeService);

            messageRule.IsEnabled = true;
            messageRule.Sequence  = 1;
            messageRule.Actions   = new MessageRuleActions()
            {
                Delete = true,
                StopProcessingRules = true
            };

            messageRule.Conditions = new MessageRulePredicates()
            {
                FromAddresses = new List <Recipient>()
                {
                    new Recipient()
                    {
                        EmailAddress = new EmailAddress()
                        {
                            Address = "*****@*****.**"
                        }
                    }
                }
            };

            messageRule.DisplayName = "Test rule";

            Assert.IsNull(messageRule.Id);
            await messageRule.SaveAsync();

            Assert.IsNotNull(messageRule.Id);

            Assert.IsNotNull(messageRule.Id);

            MessageRule getMessageRule = await exchangeService.GetInboxRuleAsync(messageRule.Id);

            Assert.IsNotNull(getMessageRule);

            getMessageRule.IsEnabled = false;
            await getMessageRule.UpdateAsync();

            Assert.IsFalse(getMessageRule.IsEnabled);


            List <MessageRule> rules = await exchangeService.GetInboxRulesAsync();

            Assert.IsTrue(rules.Count == 1);

            await getMessageRule.DeleteAsync();

            rules = await exchangeService.GetInboxRulesAsync();

            Assert.IsTrue(rules.Count == 0);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// MessageRule CRUD operation.
        /// </summary>
        /// <param name="exchangeService"></param>
        /// <returns></returns>
        public static async Task CreateReadUpdateDeleteMessageRule(ExchangeService exchangeService)
        {
            string      ruleName = Guid.NewGuid().ToString();
            MessageRule rule     = new MessageRule(exchangeService);

            rule.DisplayName = ruleName;
            rule.Sequence    = 1;
            rule.IsEnabled   = true;
            rule.Conditions  = new MessageRulePredicates()
            {
                SenderContains = new List <string>()
                {
                    "testUser"
                }
            };
            rule.Actions = new MessageRuleActions()
            {
                ForwardTo = new List <Recipient>()
                {
                    new Recipient()
                    {
                        EmailAddress = new EmailAddress()
                        {
                            Address = "*****@*****.**"
                        }
                    }
                },

                StopProcessingRules = true
            };

            await rule.SaveAsync();

            SearchFilter searchFilter = new SearchFilter.IsEqualTo(
                MessageRuleObjectSchema.DisplayName,
                ruleName);

            FindItemResults <MessageRule> rules = await exchangeService.FindItems(
                new MessageRuleView(),
                searchFilter);

            Assert.AreEqual(
                1,
                rules.TotalCount);

            Assert.IsTrue(rules.Items[0].IsEnabled);

            rule.IsEnabled = false;
            await rule.UpdateAsync();

            rules = await exchangeService.FindItems(
                new MessageRuleView(),
                searchFilter);

            Assert.AreEqual(
                1,
                rules.TotalCount);

            Assert.IsFalse(rules.Items[0].IsEnabled);

            await rule.DeleteAsync();
        }