通过编程为Outlook 2007添加邮件规则

时间:2023-03-09 20:35:39
通过编程为Outlook 2007添加邮件规则

Outlook 所支持的邮件规则相当有用,我们经常需要针对某些特征的邮件做特殊的处理。例如将其移动到某个特定文件夹,或者删除它等等。

Outlook所支持的邮件规则主要两大类:收到邮件时和发送邮件时

一个邮件规则的三大要素

1. 条件(Condition)

2. 动作(Action)

3. 例外(Exception)

下面是一个简单的范例,这是通过Visual Studio 2008所编写的Outlook 2007 外接程序(Add -in )。这个小程序演示了如何添加一个规则,该规则在收到邮件时检查所有发件人,如果发件人是chenxizhang@gmail.com,那么将执行一个动作(播放一个声音)。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;

namespace TestMailRule
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            Outlook.Rules rules = Application.Session.DefaultStore.GetRules();
            if (rules["测试"] == null)
            {
                Outlook.Rule rule = rules.Create("测试", Outlook.OlRuleType.olRuleReceive);
                rule.Conditions.From.Recipients.Add("chenxizhang@gmail.com");
                rule.Conditions.From.Enabled = true;
                rule.Conditions.From.Recipients.ResolveAll();
                rule.Actions.PlaySound.FilePath = @"E:\My Documents\LOADER.WAV";

rule.Actions.PlaySound.Enabled = true;
                rule.Enabled = true;
                rules.Save(true);
            }
        }

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

#region VSTO 生成的代码

/// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        #endregion
    }
}

看起来不错,对吧?但事实上你完全可以通过手工做出上述的效果。

还有一个难题没有解决:如何自定义动作,并将其部署到Outlook里面去?