Reputation: 915
I can get default folders from outlook no problem, but I'm struggling to get custom folders. I want to get the emails from a folder in my outlook called "Mass Archive" but I am struggling to understand how to use:
.GetFolderFromID()
From what I gathered, the first parameter it takes is the name of the folder e.g. .GetFolderFromID("Mass Archive")
But I cannot figure out what I am supposed to put as the object for the second parameter.
I'm really newbie so please explain things to me like I'm dumb.
outlookApplication = new Application();
outlookNameSpace = outlookApplication.GetNamespace("MAPI");
//inboxFolder = outlookNameSpace.GetDefaultFolder(OlDefaultFolders.olFolderDeletedItems);
inboxFolder = outlookNameSpace.GetFolderFromID("Mass Archive", "Mass Archive");
mailItems = inboxFolder.Items;
foreach (MailItem item in mailItems)
{
emailDetails = new OutlookEmails
{
EmailFrom = item.SenderEmailAddress,
EmailSubject = item.Subject,
EmailBody = item.Body,
ReceivedTime = item.ReceivedTime
};
listEmailDetails.Add(emailDetails);
ReleaseComObject(item);
}
Upvotes: 1
Views: 1504
Reputation: 1
It seems that the Outlook MAPI Namespace (fetched with OutlookApplication.GetNamespace("MAPI")
) has a collection of folders, which in their turn have a collection of folders, etc..
The folders are tree organized on the left pane of your Outlook application (for example if you have many emails accounts defined in your outlook each email addrees is an entry in your folder tree). So if you want to reach a specific folder you should get its relative number or get it by its name like said by Dimitri.
For example, you can reach email accounts with OutlookNamespace.Folders("[email protected]")
, then reach the desired subfolder either by number or by name. Hope this help.
Upvotes: 0
Reputation: 66215
You don't need to search - you can open it using MAPIFolder.Folders["The Folder Name"]
(where MAPIFolder
is the parent folder) - you just need to know where it exists relative to the default folders. E.g. if it is on the same level as the Inbox, you can retrieve the Inbox folder using GetDefaultFolder(olFolderInbox)
, then use Inbox.Parent.Folders["The Folder Name"]
.
Upvotes: 2