我试图获取Outlook中共享邮箱中的文件夹和子文件夹列表。使用缓存Exchange模式和下载共享文件夹都已选中。 文件夹最多3个级别:收件箱=>公司=> ABC公司。以下是我的代码。
myApp = new Microsoft.Office.Interop.Outlook.Application();
ns = myApp.Session;
stores = ns.Stores;
for (int i = 1; i <= stores.Count; i++)
{
var _store = stores[i];
if (_store.DisplayName.ToLower().Contains(displayName.ToLower()))
{
store = _store;
break;
}
}
rootFolder = store.GetRootFolder();
folders = rootFolder.Folders;
for (int i = 1; i < folders.Count; i++)
{
mainfolder = (Microsoft.Office.Interop.Outlook.MAPIFolder)folders[i];
if (mainfolder != null)
{
if (mainfolder.Name.Trim().ToLower() == mainfolderName.Trim().ToLower())
{
foreach (Microsoft.Office.Interop.Outlook.MAPIFolder f in mainfolder.Folders)
{
if (f.Name.Trim().ToLower() == folderName.Trim().ToLower())
{
folder = f;
if (subFolderName != "")
{
foreach (Microsoft.Office.Interop.Outlook.MAPIFolder sub in f.Folders)
{
if (sub.Name.Trim().ToLower() == subFolderName.Trim().ToLower())
{
subfolder = sub;
break;
}
}
}
break;
}
}
break;
}
}
}
ns = myApp.Session;
stores = ns.Stores;
for (int i = 1; i <= stores.Count; i++)
{
var _store = stores[i];
if (_store.DisplayName.ToLower().Contains(displayName.ToLower()))
{
store = _store;
break;
}
}
rootFolder = store.GetRootFolder();
folders = rootFolder.Folders;
for (int i = 1; i < folders.Count; i++)
{
mainfolder = (Microsoft.Office.Interop.Outlook.MAPIFolder)folders[i];
if (mainfolder != null)
{
if (mainfolder.Name.Trim().ToLower() == mainfolderName.Trim().ToLower())
{
foreach (Microsoft.Office.Interop.Outlook.MAPIFolder f in mainfolder.Folders)
{
if (f.Name.Trim().ToLower() == folderName.Trim().ToLower())
{
folder = f;
if (subFolderName != "")
{
foreach (Microsoft.Office.Interop.Outlook.MAPIFolder sub in f.Folders)
{
if (sub.Name.Trim().ToLower() == subFolderName.Trim().ToLower())
{
subfolder = sub;
break;
}
}
}
break;
}
}
break;
}
}
}
它成功获得第一级 - 收件箱,草稿,存档,垃圾邮件等。但它并不总是获得第二级,mainfolder.Folders.Count返回0.我错过了什么?谢谢。
答案 0 :(得分:0)
我通过在获取邮箱中的文件夹之前ping电子邮件服务器找到了解决方案。这是我到目前为止找到的解决方案,现在已经有一周了,至少它在我的案例中起作用了。谢谢。