2011-07-06 156 views
0

我們剛剛啓動了一項新任務,以使用asp.net Web應用程序從交換服務器爲不同帳戶檢索電子郵件。我以前沒有這樣的經歷,但在網上搜索後,我發現了一個代碼片段,可以與Outlook進行通信並從那裏獲取電子郵件。然而,有一個例外,每次I測試是代碼時間:ASP.NET網頁從outlook問題中獲取電子郵件

"Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.PostItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063024-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)). "

有誰知道原因嗎?

順便說一句,任何幫助直接從交換服務器獲取電子郵件的建議非常感謝!

我的代碼:

 Microsoft.Office.Interop.Outlook.Application app = null; 
     Microsoft.Office.Interop.Outlook._NameSpace ns = null; 
     Microsoft.Office.Interop.Outlook.PostItem item = null; 
     Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null; 
     Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null; 
     try 
     { 
      app = new Microsoft.Office.Interop.Outlook.Application(); 
      ns = app.GetNamespace("MAPI"); 
      ns.Logon("user", "password", false, false); 
      inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); 

      StringBuilder sb = new StringBuilder(); 
      sb.AppendFormat("Folder Name:{0},EntryId:{1}", inboxFolder.Name, inboxFolder.EntryID); 
      sb.AppendFormat(" Num Items:{0}", inboxFolder.Items.Count.ToString()); 
      Response.Write(sb); 
      for (int i = 1; i <= inboxFolder.Items.Count; i++) 
      { 
       item = (Microsoft.Office.Interop.Outlook.PostItem)inboxFolder.Items[i];//this is the exception happened line 
       Console.WriteLine("Item: {0}", i.ToString()); 
       Console.WriteLine("Subject: {0}", item.Subject); 
       Console.WriteLine("Sent: {0} {1}" item.SentOn.ToLongDateString(), item.SentOn.ToLongTimeString()); 
       Console.WriteLine("Categories: {0}", item.Categories); 
       Console.WriteLine("Body: {0}", item.Body); 
       Console.WriteLine("HTMLBody: {0}", item.HTMLBody); 

      } 
     } 
     catch (Exception) 
     { 

      throw; 
     } 
     finally 
     { 
      ns = null; 
      app = null; 
      inboxFolder = null; 
     } 
+0

你必須互操作使用,也可以代替那樣做使用POP從郵件服務器檢索郵件大多數應用程序? – Earlz

+0

你有沒有讀過 - http://stackoverflow.com/questions/652549/read-ms-exchange-email-in-c? –

+0

@Eariz,這不是必須的,但我需要一種方法來從交換服務器收回電子郵件。 –

回答

1

這裏所說:

有了這些行...

Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null; 
inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); 

...你創建到用戶的收件箱的參考。到現在爲止還挺好。

但後來這裏...

for (int i = 1; i <= inboxFolder.Items.Count; i++) 
{ 
    item = (Microsoft.Office.Interop.Outlook.PostItem)inboxFolder.Items[i]; 
    //other stuff... 
} 

...你說,「在收件箱中的每個項目,認爲該項目是一個PostItem。」

According to MSDN, a PostItem

Represents a post in a public folder that others may browse.

用戶的收件箱是不會滿後的項目。它將包含代表用戶電子郵件的MailItem對象。既然如此,該行代碼也許應該是

item = (Microsoft.Office.Interop.Outlook.MailItem)inboxFolder.Items[i]; 

買者:我不知道有足夠的瞭解Outlook的API,以瞭解是否有可能爲那裏是Outlook項目比的MailItem對象的對象等收件箱,但我不打賭。作爲參考,full list of Outlook Item Objects is here

0

使用,如下圖所示:

// not PostItem 
item = (Microsoft.Office.Interop.Outlook.MailItem)inboxFolder.Items[i]; 
相關問題