Jaspreet Singh

Jaspreet Singh

  • NA
  • 20
  • 3.5k

Unhandled exception. System.IO.FileNotFoundException: Could

Oct 23 2019 5:49 AM
Hello Team,
 
I am getting the following error messsage while executing the following code:-
 
Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'office, Version=15.0.0.0
 
Here is my Code:-
 
using System;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Office.Interop.Outlook;
using Microsoft.Office365.OutlookServices;
namespace FunctionProject
{
class Program
{
static void Main(string[] args)
{
ReadMailItems();
}
private static void ReadMailItems()
{
Application outlookApplication = null;
NameSpace outlookNamespace = null;
MAPIFolder inboxFolder = null;
Items mailItems = null;
try
{
outlookApplication = new Application();
outlookNamespace = outlookApplication.GetNamespace("MAPI");
inboxFolder = outlookNamespace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
mailItems = inboxFolder.Items;
foreach (MailItem item in mailItems)
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine("From: " + item.SenderEmailAddress);
stringBuilder.AppendLine("To: " + item.To);
stringBuilder.AppendLine("CC: " + item.CC);
stringBuilder.AppendLine("");
stringBuilder.AppendLine("Subject: " + item.Subject);
stringBuilder.AppendLine(item.Body);
Console.WriteLine(stringBuilder);
Marshal.ReleaseComObject(item);
}
}
catch (System.Exception ex) { throw ex; }
finally
{
ReleaseComObject(mailItems);
ReleaseComObject(inboxFolder);
ReleaseComObject(outlookNamespace);
ReleaseComObject(outlookApplication);
}

}
private static void ReleaseComObject(object obj)
{
if (obj != null)
{
Marshal.ReleaseComObject(obj);
obj = null;
}
}
}
}
 

Answers (1)