Get all mails in outlook from a specific folder

泄露秘密 提交于 2020-01-02 07:06:02

问题


I would like to get all mails in outlook from a specific folder:

My folder structure:

I find this Code to get all mails in outlook inbox folder (not the inbox in picture!):

Outlook.Application oApp;
Outlook._NameSpace oNS;
Outlook.MAPIFolder oFolder;
Outlook._Explorer oExp;

oApp = new Outlook.Application();
oNS = (Outlook._NameSpace)oApp.GetNamespace("MAPI");
oFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
oExp = oFolder.GetExplorer(false);
oNS.Logon(Missing.Value, Missing.Value, false, true);

Outlook.Items items = oFolder.Items;
foreach (Outlook.MailItem mail in items)
{
    //do someting
}

Can someone tell me how to get all mails from a specific folder (like: wolfy-server)?


回答1:


This should work!!!

MAPIFolder mailsFromThisFolder;

MAPIFolder mainFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

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

public void GetFolders(MAPIFolder folder)
{
    if (folder.Folders.Count == 0)
    {
            if (folder.Name == "Folder Name")
            {
                Console.WriteLine(m.FullFolderPath);
                mailsFromThisFolder = folder;
            }
    }
    else
    {
         foreach (MAPIFolder subFolder in folder.Folders)
         {
              GetFolders(subFolder);
         }
    }
}

Outlook.Items items = mailsFromThisFolder.Items;
foreach (Outlook.MailItem mail in items)
{
    //do someting
}



回答2:


  1. Logon must come before you do anything else.
  2. Use something like the following

    oFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox); oSubfolder = oFolder.Folders.Item("wolfy-server");



来源:https://stackoverflow.com/questions/5660332/get-all-mails-in-outlook-from-a-specific-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!