应用EWS创建的邮箱规则

时间:2018-03-22 18:30:06

标签: c# exchange-server exchangewebservices

我写了一个小代码块,在我的邮箱中创建了以下规则:

  • 邮件到达后应用此规则
  • 在[当前日期 - X天]之前收到
  • 删除它。

这是相应的代码块:

var exchange = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
DateTime dtOldestDate = DateTime.Now.AddDays(iDays * -1);
exchange.Credentials = new WebCredentials(sUser, sPassword);
exchange.AutodiscoverUrl(sUser);
if (exchange != null)
{
    RuleCollection InboxRules = exchange.GetInboxRules(sUser);
    bool bRuleFound = false;
    foreach (Rule InboxRule in InboxRules)
    {
        if (InboxRule.DisplayName == "MailboxCleaner: Deletion Policy")
        {
            bRuleFound = true;
            InboxRule.Conditions.WithinDateRange.Start = null;
            InboxRule.Conditions.WithinDateRange.End = dtOldestDate;

            SetRuleOperation setRuleOperation = new SetRuleOperation(InboxRule);
            exchange.UpdateInboxRules(new RuleOperation[] { setRuleOperation }, true);
            break;
        }
    }

    if (!bRuleFound)
    {
        Rule NewInboxRule = new Rule();
        NewInboxRule.DisplayName = "MailboxCleaner: Deletion Policy";
        NewInboxRule.Priority = 1;
        NewInboxRule.IsEnabled = true;
        NewInboxRule.Conditions.WithinDateRange.Start = null;
        NewInboxRule.Conditions.WithinDateRange.End = dtOldestDate;
        NewInboxRule.Actions.PermanentDelete = true;
        CreateRuleOperation createOperation = new CreateRuleOperation(NewInboxRule);
        exchange.UpdateInboxRules(new RuleOperation[] { createOperation },  true);
    }

    // How can I go on and apply this rule on all messages?
}

在第二行,我想应用我的新/修改规则。我需要这样做,因为规则将以另一种方式应用。 Outlook因此有以下按钮:

"Run Rules Now..."-Button

(来自https://blogs.technet.microsoft.com/outlooking/2015/05/01/client-side-vs-server-side-rules/的图片)

我可以通过使用EWS以某种方式触发相同的程序吗?

提前致谢!

0 个答案:

没有答案