2011-07-06 155 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]; 
相关问题