我正在编写一个必须获取可与规则匹配的项目的应用程序。
//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.
}
}
答案 0 :(得分:1)
通过为每个MailItem
手动枚举Rule Conditions(和excluding rule exceptions )来检查哪些规则适用于哪些规则的唯一方法。规则引擎通过执行您通过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);
}
}
}
}
}