【问题标题】:Microsoft.Office.Interop.outlook doesn't workMicrosoft.Office.Interop.outlook 不起作用
【发布时间】:2020-10-07 12:08:06
【问题描述】:

我正在开发一个小项目,我需要能够从 Outlook 邮箱中检索电子邮件以处理收到的电子邮件。

我拿了一个用 VB.net 制作的旧程序用 C# 重写它。 使用 Vb.net 中的项目,我没有错误

但是我有这个错误:System.IO.FileNotFoundException : 'Could not load file or assembly 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'. The specified file can not be found.' 与 C# 项目。

我的参考资料:

当我将 Microsoft.Office.Interop.outlook 库与 Vb.net 中安装的项目进行比较时,完全一样(相同版本等)。

所以如果你有解决这个问题的想法我很感兴趣:D

【问题讨论】:

  • 您在使用 M365 邮箱吗?如果是这样use the MS Graph API 而不是互操作。在 2020 年使用与 Office 互操作的理由很少。
  • 我正在使用 Office 16
  • 您正在使用 Office 16 但尝试使用 office 15 dll ?
  • 但是我添加了 Microsoft Office 互操作 Outlook 16(我在帖子中添加了屏幕截图),所以我想 Office 16 的好版本还是我做错了?
  • 可以参考这个ticket。此外,您可以尝试将 True 添加到您的 csproj 文件中。另请参考this ticket

标签: c# vb.net visual-studio outlook


【解决方案1】:

您可以尝试以下步骤来解决System.IO.FileNotFoundException 问题。

首先,您需要将当前框架更改为 .NET Core 3.0。

其次,选择Interop.Microsoft.Office.Interop.Outlook,右键属性,将“Embed Interop Types”设置为“Yes”。

最后,您可以使用以下代码从 Outlook 邮箱中检索电子邮件。

using Outlook = Microsoft.Office.Interop.Outlook;


            Outlook.Application oApp = null;
            oApp = new Outlook.Application();
            Outlook.NameSpace nameSpace = oApp.GetNamespace("MAPI");
            Outlook.Items items = null;
            try
            {
                // use default profile and DO NOT pop up a window
                // on some pc bill gates fails to login without the popup, then we must pop up and lets use choose profile and allow access
                nameSpace.Logon("", "", false, Missing.Value);
                var folder = nameSpace.Folders["emailaddress"].Folders["Folder"];
                items = folder.Items;
                foreach (Outlook.MailItem item in items)
                {
                        Console.WriteLine(item.Subject);
                }
                Console.WriteLine("yes");
                Console.ReadKey();
            }
            catch (Exception)
            {
                // use default profile and DO pop up a window
                nameSpace.Logon("", "", true, true);
            }

【讨论】:

    猜你喜欢
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    相关资源
    最近更新 更多