2012-07-12 68 views
3

我正在編寫一個必須獲取可與規則匹配的項目的應用程序。如何檢查可應用於Outlook中的郵件項目的所有規則

//new messages goes here 
void items_ItemAdd(object Item) 
    { 
     //all rules 
     Rules rules = Application.Session.DefaultStore.GetRules(); 

     Outlook.MailItem mail = (Outlook.MailItem)Item; 

     if (mail != null) 
     { 
      // I need to find out if mail matches with one of the rule. And handle in an appropriate way. 
     } 
    } 

回答

1

檢查的唯一方法哪些規則應用於哪些項目是通過手動列舉Rule Conditionsexcluding rule exceptions)爲每個MailItem。規則引擎通過執行您通過Rule.Execute定義的每個規則來工作 - 它不提供預覽受影響項目的機制。

下面是關於如何匹配參考的(未測試)的例子包含主題規則olConditionSubject)。您還需要處理other Rule Conditions types

if (mail != null) 
{ 
    foreach (Outlook.Rule rule in rules) 
    { 
     foreach (Outlook.RuleCondition condition in rule.Conditions) 
     { 
     if (condition is TextRuleCondition) 
     { 
      Outlook.TextRuleCondition trc = condition as Outlook.TextRuleCondition; 
      if (trc.ConditionType == Outlook.OlRuleConditionType.olConditionSubject) 
      { 
       // TODO: handle Rule.Exceptions conditions 
       bool containsSubject = mail.Subject.Contains(trc.Text); 
      } 
     } 
     } 
    } 
} 
+1

你能否給我提供代碼片段。 – Alexandr 2012-07-12 13:58:24

+0

要做到「規則條件」的複雜性 - 您必須通過您想要支持的每個條件1乘1。我可以添加一個參考例子。 – SliverNinja 2012-07-12 14:00:49

+0

,將不勝感激。 – Alexandr 2012-07-12 14:06:35

相關問題