我们刚刚启动了一项新任务,以使用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,这不是必须的,但我需要一种方法来从交换服务器收回电子邮件。 –