2012-03-13 112 views
2

我正在嘗試爲Outlook 2007中的共享郵箱創建一個彈出郵件提醒。 以下代碼不起作用。爲什麼?共享郵箱Outlook 2007的新郵件提醒

private Microsoft.Office.Interop.Outlook._Explorers Explorers; 
private Microsoft.Office.Interop.Outlook.NameSpace outlookNamespace; 
private Microsoft.Office.Interop.Outlook.MAPIFolder mFolder; 
private void ThisAddIn_Startup(object sender, System.EventArgs e) 
{ 
    Explorers = this.Application.Explorers;   
    outlookNamespace = this.Application.GetNamespace("MAPI");   
    mFolder = outlookNamespace.Folders["Mailbox -AdditionalMailBox"].Folders["Inbox"]; 
    mFolder.Application.NewMailEx += new ApplicationEvents_11_NewMailExEventHandler(Application_NewMailEx); 
} 
private void Application_NewMailEx(string EntryID) 
{ 
    MessageBox.Show("New MailReceived!"); 
} 

回答

0

爲了使您的事件處理程序的生存垃圾收集 - 您需要確保包含您的事件對象將其添加爲一個私有成員變量維持生命。嘗試添加Application私人類變量,您的事件訂閱應該可以正常工作。有關更多詳情,請參閱related SO post

private Microsoft.Office.Interop.Outlook.Application application; 
private void ThisAddIn_Startup(object sender, System.EventArgs e) 
{ 
    application = this.Application; 
    Explorers = application.Explorers;   
    // ...   
    application.NewMailEx += new ApplicationEvents_11_NewMailExEventHandler(Application_NewMailEx); 
} 

你發現的另一個問題是,NewMailEx只火了你的主郵箱 - 而不是次要郵箱爲confirmed in this forum post。您將需要依靠Folder.Items事件ItemAdd

private Outlook.Items mFolderItems; 
private void ThisAddIn_Startup(object sender, System.EventArgs e) 
{ 
    // ... 
    mFolderItems = mFolder.Items; // avoid GC for ItemAdd event 
    mFolderItems.ItemAdd += new ItemsEvents_ItemAddEventHandler(mFolder_ItemAdd); 
    // ... 
}   
private void mFolder_ItemAdd(object addedItem) 
{ 
    Outlook.MailItem newItem = addedItem as Outlook.MailItem; 
} 
+0

應用程序僅在主郵箱中訂閱新郵件。它不適用於額外的郵箱。 – 2012-03-13 14:04:01

+0

@ShnerkaZoid - 你是對的 - 'NewEmailEx'只適用於主郵箱,請參閱上面的編輯。 – SliverNinja 2012-03-13 14:26:25

+0

它的工作!謝謝! – 2012-03-14 08:45:41

相關問題