0

I am developing a small project where I need to be able to retrieve emails from an Outlook mailbox in order to process the emails received.

I took an old program made in VB.net to rewrite it in C #. With the project in Vb.net I have no errors

However I have this error: 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.' with the C# project.

My references :

enter image description here enter image description here

When I compare the Microsoft.Office.Interop.outlook library to the project in Vb.net installed is exactly the same (same version, etc.).

So if you have an idea to solve this problem I am interested :D

Laura Destier
  • 41
  • 3
  • 11
  • 2
    Are you working with M365 mailboxes? If so [use the MS Graph API](https://learn.microsoft.com/en-us/graph/outlook-mail-concept-overview) instead of interop. There are very few reasons to use interop with Office in 2020. – Crowcoder Oct 07 '20 at 12:18
  • I'm working with Office 16 – Laura Destier Oct 07 '20 at 12:22
  • You are working with Office 16 but trying to use office 15 dll ? – Franck Oct 07 '20 at 12:30
  • But i add the Microsoft office interop Outlook 16 (I added a screenshoot to my post) so i suppose is the good version for Office 16 or i do a mistake ? – Laura Destier Oct 07 '20 at 12:33
  • You can refer to this [ticket](https://stackoverflow.com/questions/32399420/could-not-load-file-or-assembly-office-version-15-0-0-0). In addition, you can try to add True to your csproj file. Please also refer to [this ticket](https://github.com/dotnet/project-system/issues/5735). – Walter Oct 08 '20 at 10:06
  • @LauraDestier, is any update? Please check if my answer works for you. – Jack J Jun Oct 20 '20 at 05:59

1 Answers1

1

You can try the following steps to solve the System.IO.FileNotFoundException problem.

First, you need to change the current framework to .NET Core 3.0.

enter image description here

Second, Select Interop.Microsoft.Office.Interop.Outlook, right click properties and set "Embed Interop Types" to "Yes".

enter image description here

Finally, you can use the following code to retrieve emails from an Outlook mailbox.

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);
            }
Jack J Jun
  • 5,633
  • 1
  • 9
  • 27