2014-05-16 43 views
1

我期待從Outlook 2013監控所有新電子郵件並獲取電子郵件的主題。 閱讀了幾個博客後,我明白最好的/簡單的方法是使用Microsoft.Office.Interop.Outlook和MAPI和ApplicationEvents_11_NewMailExEventHandler事件。在C#中監控Outlook 2013中的傳入電子郵件並獲取電子郵件的主題

我嘗試在博客此基礎上,我做了一點修改:

using System; 
using System.Windows.Forms; 
using Microsoft.Office.Interop.Outlook; 


class EmailMonitoring 
{ 
    static void Main(string[] args) 
    { 
     // Create an Outlook application object. 
     Microsoft.Office.Interop.Outlook.Application app = null; 
     Microsoft.Office.Interop.Outlook._NameSpace ns = null; 

     try 
     { 
      //Email 
      app = new Microsoft.Office.Interop.Outlook.Application(); 
      ns = app.GetNamespace("MAPI"); 
      ns.Logon(null, null, false, false); 

      // Ring up the new message event. 
      app.NewMailEx += new ApplicationEvents_11_NewMailExEventHandler(outLookApp_NewMailEx); 
      Console.WriteLine("Please wait for new messages..."); 
      Console.ReadLine(); 
     } 
     catch (System.Exception e) 
     { 
      Console.WriteLine("Exception in Main "+e); 
     } 
     finally 
     { 
      ns = null; 
      app = null; 

     } 
    } 

    private static void outLookApp_NewMailEx(string EntryIDCollection) 
    { 
     Console.WriteLine("You've got a new mail whose EntryIDCollection is \n" + EntryIDCollection); 
     Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application(); 
     Microsoft.Office.Interop.Outlook.MailItem item = null; 
     Microsoft.Office.Interop.Outlook._NameSpace ns = app.GetNamespace("MAPI"); 

     try 
     { 

      ns.Logon(null, null, false, false); 

      item = ns.GetItemFromID(EntryIDCollection); 
      Console.WriteLine("test" + item.Subject); 
     } 
     catch(System.Exception e) 
     { 
      Console.WriteLine("Exception in event Handler "+e); 
     } 
     finally 
     { 
      ns = null; 
      app = null; 

     } 
    } 

} 

然而,當我在Outlook中有新的電子郵件時,控制檯顯示的EntryID和主題,但展望成爲凍結對於某些特定的電子郵件並拋出此錯誤消息: 「電子郵件已停止工作。」

  • 我忘了刪除一些對象嗎?
  • 有沒有更好的方法來做到這一點?我不喜歡我需要定義2個Outlook.Applications的事實。
  • 您是否推薦任何描述如何做的博客/書籍?
+0

也許嘗試這個例子嗎? http://msdn.microsoft.com/en-us/library/ms268998.aspx –

+1

您的代碼不清除顯式的非託管Outlook COM資源。毫無疑問,這是造成這種情況的原因。這可能是全部原因,但肯定至少是問題的一部分。 –

回答

0

試試這個,這將不會鎖定文件夾在您的Outlook文件,新郵件將在隊列

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using Outlook = Microsoft.Office.Interop.Outlook; 

namespace Sample 
{ 
    public partial class frmSample : Form 
    { 
     Outlook.NameSpace outlookNameSpace; 
     Outlook.MAPIFolder inbox; 
     Outlook.Items items; 
     public frmSample() 
     { 
      InitializeComponent(); 
     } 

     private void frmSample_Load(object sender, EventArgs e) 
     { 
      Outlook.Application app = new Outlook.Application(); 
      outlookNameSpace = app.GetNamespace("MAPI"); 
      inbox = outlookNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); 
      items = inbox.Items; 
      items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(items_ItemAdd); 
     } 
     void items_ItemAdd(object Item) 
     { 
      Outlook.MailItem mail = (Outlook.MailItem)Item; 
      if (Item != null) 
      { 
       string tempvariable = mail.To; 
       tempvariable = mail.Subject; 
       tempvariable = mail.Body; 
       tempvariable = mail.SenderEmailAddress; 
       tempvariable = mail.SenderName.ToString();       
      } 
      } 
     } 
    } 
}