confusedMind
confusedMind

Reputation: 2653

How to get folder by path in multiple Accounts Outlook c#

I have 2 accounts added to outlook , two seperate pst files. You will get clear idea with image below :

enter image description here

First i prompt user select folders from the outlook which ones to read, image attached for better understanding:

enter image description here

At the end i have the folderpaths in a list, same as in the image.

Now i want to read emails only from these specific paths and send them replies or delete them, so how can i do that?How can i read the folder based on a path n such way i get the account associated with it so i can also send emails using that accounts.

Upvotes: 0

Views: 3093

Answers (1)

confusedMind
confusedMind

Reputation: 2653

Use the following code to loop through all the folders then match the path with the ones in the list.

CODE:

        OutLook.Application oApp = new OutLook.Application();
        OutLook.NameSpace oNS = (OutLook.NameSpace)oApp.GetNamespace("MAPI");
        oNS.Logon(Missing.Value, Missing.Value, false, true);

        foreach (OutLook.MAPIFolder folder in oNS.Folders)
        {
            string folderName = folder.Name;

            GetFolders(folder);

        }



   public void GetFolders(MAPIFolder folder)
    {
        if (folder.Folders.Count == 0)
        {
            string path = folder.FullFolderPath;


            if (foldersTocheck.Contains(path))
            { 
                //GET EMAILS.....
                foreach (OutLook.MailItem item in folder.Items)
                {
                    Console.WriteLine(item.SenderEmailAddress + " " + item.Subject + "\n" + item.Body);


                }
            }
        }
        else
        {
            foreach (MAPIFolder subFolder in folder.Folders)
            {
                GetFolders(subFolder);
            }
        }
    }

Upvotes: 1

Related Questions