我們剛剛啓動了一項新任務,以使用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;
}
你必須互操作使用,也可以代替那樣做使用POP從郵件服務器檢索郵件大多數應用程序? – Earlz
你有沒有讀過 - http://stackoverflow.com/questions/652549/read-ms-exchange-email-in-c? –
@Eariz,這不是必須的,但我需要一種方法來從交換服務器收回電子郵件。 –