1. 程式人生 > >通過編程為Outlook 2007添加郵件規則

通過編程為Outlook 2007添加郵件規則

str href intern creat rule XML ons gen 小程序

Outlook 所支持的郵件規則相當有用,我們經常需要針對某些特征的郵件做特殊的處理。例如將其移動到某個特定文件夾,或者刪除它等等。

Outlook所支持的郵件規則主要兩大類:收到郵件時和發送郵件時

一個郵件規則的三大要素

1. 條件(Condition)

2. 動作(Action)

3. 例外(Exception)

下面是一個簡單的範例,這是通過Visual Studio 2008所編寫的Outlook 2007 外接程序(Add -in )。這個小程序演示了如何添加一個規則,該規則在收到郵件時檢查所有發件人,如果發件人是[email protected],那麽將執行一個動作(播放一個聲音)。

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("[email protected]

/* */");
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裏面去?

通過編程為Outlook 2007添加郵件規則