简体   繁体   English

ASP.NET web 页面从 outlook 问题中获取电子邮件

[英]ASP.NET web page getting emails from outlook problem

We just launched a new task to retrieve emails from exchange server for different accounts using asp.net web application.我们刚刚启动了一项新任务,使用 asp.net web 应用程序从不同帐户的 Exchange 服务器检索电子邮件。 I don't have such experience in past, but after searching online I found a code snippet which could communicate with outlook and fetch emails from there.我过去没有这样的经验,但是在网上搜索后,我发现了一个可以与 outlook 通信并从那里获取电子邮件的代码片段。 However, there is an exception occurs every time I test the code which is:但是,每次我测试代码时都会发生异常,即:

"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)). " "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}' 由于以下错误而失败:不支持此类接口(来自 HRESULT 的异常:0x80004002 (E_NOINTERFACE))。"

Does anyone know the reason?有谁知道原因?

By the way, any suggestion helping on fetching emails from exchange server directly is highly appreciated!顺便说一句,非常感谢任何有助于直接从 Exchange 服务器获取电子邮件的建议!

My code:我的代码:

        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;
        }

Here goes:开始:

With these lines...有了这些线...

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

...you're creating a reference to the user's Inbox. ...您正在创建对用户收件箱的引用。 So far so good.到目前为止,一切都很好。

But then here...但后来这里...

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

...you're saying, "for every Item in the Inbox, assume that the Item is a PostItem." ...您是说,“对于收件箱中的每个项目,假设该项目是一个 PostItem。”

According to MSDN, a PostItem :根据 MSDN,一个 PostItem

Represents a post in a public folder that others may browse.表示其他人可以浏览的公共文件夹中的帖子。

The user's inbox isn't going to be full of post items.用户的收件箱不会装满帖子。 It's going to contain MailItem objects representing the user's emails.它将包含代表用户电子邮件的MailItem对象。 That being the case, that line of code should probably be既然如此,那一行代码大概应该是

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

Caveat : I don't know enough about Outlook's API to understand whether it's possible for there to be Outlook Item Objects other than MailItem objects in the Inbox, but I'd wager not.警告:我对 Outlook 的 API 了解不足,无法了解收件箱中是否可能存在 Outlook 项目对象而不是 MailItem 对象,但我不敢打赌。 For reference, the full list of Outlook Item Objects is here .作为参考, Outlook 项目对象的完整列表在这里

Use as shown below:如下图使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM