Hello Team,
 
I am unable to read emails in outlook by using the following code:-
 
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Office.Interop.Outlook;
namespace EmailRead
{
    class Program
    {
        static void Main(string[] args)
        {
            ReadMailItems();
        }
        private static void ReadMailItems()
        {
            Application outlookApplication = null;
            NameSpace outlookNamespace = null;
            MAPIFolder inboxFolder = null;
            const string destinationDirectory = @"C:\TestFileSave"; 
            if (!Directory.Exists(destinationDirectory))
            {
                Directory.CreateDirectory(destinationDirectory);
            }
            int emailCounter = 0;
            try
            {
                outlookApplication = new Application();
                outlookNamespace = outlookApplication.GetNamespace("MAPI");
                inboxFolder = outlookApplication.ActiveExplorer().Session.GetDefaultFolder(OlDefaultFolders.olFolderInbox).Folders["Birthday Emails"];
                foreach (MailItem item in inboxFolder.Items)
                {
                    if (item.UnRead)
                    {
                        string fromEmailAddress = item.SenderEmailAddress.ToString();
                        emailCounter++;
                        var stringBuilder = new StringBuilder();
                        stringBuilder.AppendLine("From: " + item.SenderEmailAddress.ToString());
                        stringBuilder.AppendLine("To: " + item.To);
                        stringBuilder.AppendLine("CC: " + item.CC);
                        stringBuilder.AppendLine("");
                        stringBuilder.AppendLine("Subject: " + item.Subject);
                        stringBuilder.AppendLine(item.Body);
                        Attachments attachments = item.Attachments;
                        MailItem newEmail = item as MailItem;
                        if (newEmail == null) continue;
                        if (newEmail.Attachments.Count > 0)
                        {
                            for (int i = 1; i <= newEmail.Attachments.Count; i++)
                            {
                                string fileName = newEmail.Attachments[i].FileName.ToLower();
                                if (!fileName.Contains(".png") && !fileName.Contains(".jpg") && !fileName.Contains(".jpeg"))
                                {
                                    string filePath = Path.Combine(destinationDirectory, newEmail.Attachments[i].FileName);
                                    newEmail.Attachments[i].SaveAsFile(filePath);
                                    Console.WriteLine(stringBuilder.ToString());
                                }
                            }
                        }
                        item.UnRead = false;
                    }
                    Marshal.ReleaseComObject(item);
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            finally
            {
                ReleaseComObject(inboxFolder);
                ReleaseComObject(outlookNamespace);
                ReleaseComObject(outlookApplication);
            }
        }
        private static void ReleaseComObject(object obj)
        {
            if (obj != null)
            {
                Marshal.ReleaseComObject(obj);
                obj = null;
            }
        }
    }
}
 
I am using a Community Edition of Visual Studio and getting the following error message:-
 
Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'. The system cannot find the file specified.
File name: 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'
   at EmailRead.Program.ReadMailItems()
   at EmailRead.Program.Main(String[] args) in C:\Users\jaspreet1.singh\source\repos\EmailTrigger\EmailRead\Program.cs:line 13 
 
Can someone pleae advise on this.