我如何使用EWS Web服务在Outlook中检查文件夹和子文件夹是否存在& C#

发布于 2025-02-01 13:51:06 字数 1625 浏览 3 评论 0 原文

我是C#开发的新手。 我正在尝试使用Exchange Web服务在Outlook Mailbox中检查并创建一个文件夹/子文件夹。

文件夹结构

  • main_folder
  • sub folder-1
  • sub文件夹-2
  • sub文件夹3

实现,

public void checkFolderExistOrNot( String folder_name)
        {
            FolderView fv = new FolderView(100);

            var findFoldersResults = exchangeService.FindFolders(
                WellKnownFolderName.Inbox,
                new SearchFilter.SearchFilterCollection(
                    LogicalOperator.Or,
                    new SearchFilter.ContainsSubstring(FolderSchema.DisplayName, folder_name)),
                fv);

            foreach (var folder in findFoldersResults)
            {
                if (folder is Folder)
                {
                    if (folder.DisplayName == folder_name)
                    {
                        archiveFolderID = folder.Id;
                    }
                   

                }
            }
            //if archive folder not found create and assign the variable to the folderID
            if (archiveFolderID == null)
            {
                Folder folder = new Folder(exchangeService);
                folder.DisplayName = folder_name;
                folder.Save(WellKnownFolderName.Inbox);
                archiveFolderID = folder.Id;
            }
            
        }

checkFolderExistOrNot(MAIN_folder)
checkFolderExistOrNot(MAIN_folder.Sub Folder-1)
checkFolderExistOrNot(MAIN_folder.Sub Folder-2)
checkFolderExistOrNot(MAIN_folder.Sub Folder-3)

但这仅在收件箱下创建主文件夹。如果有人可以帮助我确定我的实施中缺少的内容,这将不胜感激。

提前致谢。

I'm very new to C# development.
I'm trying to check and create a folder/ sub-folder exist in Outlook Mailbox using Exchange Web Service.

Folder Structure

  • MAIN_folder
  • Sub Folder-1
  • Sub Folder-2
  • Sub Folder-3

Implementation

public void checkFolderExistOrNot( String folder_name)
        {
            FolderView fv = new FolderView(100);

            var findFoldersResults = exchangeService.FindFolders(
                WellKnownFolderName.Inbox,
                new SearchFilter.SearchFilterCollection(
                    LogicalOperator.Or,
                    new SearchFilter.ContainsSubstring(FolderSchema.DisplayName, folder_name)),
                fv);

            foreach (var folder in findFoldersResults)
            {
                if (folder is Folder)
                {
                    if (folder.DisplayName == folder_name)
                    {
                        archiveFolderID = folder.Id;
                    }
                   

                }
            }
            //if archive folder not found create and assign the variable to the folderID
            if (archiveFolderID == null)
            {
                Folder folder = new Folder(exchangeService);
                folder.DisplayName = folder_name;
                folder.Save(WellKnownFolderName.Inbox);
                archiveFolderID = folder.Id;
            }
            
        }

checkFolderExistOrNot(MAIN_folder)
checkFolderExistOrNot(MAIN_folder.Sub Folder-1)
checkFolderExistOrNot(MAIN_folder.Sub Folder-2)
checkFolderExistOrNot(MAIN_folder.Sub Folder-3)

But this is only creating the Main folder under the inbox. It would be greatly appreciated if someone could help me to identify what is missing in my implementation.

Thanks in Advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明媚如初 2025-02-08 13:51:06

判断文件夹是否存在的唯一方法是通过搜索搜索它,因为您没有指定folderview中的遍历,它将始终很浅。如果您在其中指定了深度遍历,

FolderView fv = new FolderView(100);
fv.Traversal = FolderTraversal.Deep;

则应该能够找到要在ON上创建新子文件夹的父文件夹。只要您没有任何名称冲突的文件夹级别,您的逻辑就可以正常工作。否则我要做的就是 Exchange Web Service for A for A用户创建的文件夹

The only way to tell if a folder exists is to search for it with your search because you don't specify the traversal in the Folderview it will always be shallow. If you specify a deep traversal in

FolderView fv = new FolderView(100);
fv.Traversal = FolderTraversal.Deep;

You should then be able to find the parent folder you want to create a new subfolder on. Your logic should work okay as long as you don't have any name clashes a different folder levels. Otherwise what I do is this Exchange Web Service FolderId for a folder created by user or Get to an Exchange folder by path using EWS

等你爱我 2025-02-08 13:51:06

您是否给出了 Microsoft Graph 看起来?

您基本上可以将其用于Microsoft 365中的任何东西。您也可以实现自己的目标。

您将需要创建一个 GraphServiceClient ,并且可以进行以下操作以检查文件夹是否存在:

string user = "emailAddressOfTheUser";

var parentFolderRequest = graphClient.Users[user].MailFolders.Inbox.ChildFolders
                                                   .Request()
                                                   .Filter($"startsWith(displayName, 'parentFolderName')");

var parentMailFolder = await parentFolderRequest.GetAsync(cancellationToken);

一旦有父母文件夹,您就可以获取它的ID,并且一旦您知道可以搜索它儿童文件夹:

var parentMailFolderID = parentMailFolder.First().Id;

var childFolderRequest = graphClient.Users[user].MailFolders[parentMailFolderID].ChildFolders
                                                   .Request()
                                                   .Filter($"startsWith(displayName, 'childFolderName')");

var childMailFolder = await parentFolderRequest.GetAsync(cancellationToken);

如果 childmailfolder.count> 0 然后存在文件夹,如果不是,则创建了子文件夹:

var childFolder = new MailFolder
{
   DisplayName = "childFolderName",
   IsHidden = false
};

await graphClient.Users[graphUser.Id]
                 .MailFolders[parentMailFolderID].ChildFolders
                 .Request()
                 .AddAsync(childFolder );

Have you given Microsoft Graph a look?

You can basically use it for anything in Microsoft 365. With you you can also achieve your goal.

You will need to create a GraphServiceClient and with it you can do the following to check if a folder exists:

string user = "emailAddressOfTheUser";

var parentFolderRequest = graphClient.Users[user].MailFolders.Inbox.ChildFolders
                                                   .Request()
                                                   .Filter(
quot;startsWith(displayName, 'parentFolderName')");

var parentMailFolder = await parentFolderRequest.GetAsync(cancellationToken);

Once you have the parent folder you can get it's ID and once you know that you can search it for child folders:

var parentMailFolderID = parentMailFolder.First().Id;

var childFolderRequest = graphClient.Users[user].MailFolders[parentMailFolderID].ChildFolders
                                                   .Request()
                                                   .Filter(
quot;startsWith(displayName, 'childFolderName')");

var childMailFolder = await parentFolderRequest.GetAsync(cancellationToken);

If the childMailFolder.Count > 0 then the folder exists, if not you create the child folder:

var childFolder = new MailFolder
{
   DisplayName = "childFolderName",
   IsHidden = false
};

await graphClient.Users[graphUser.Id]
                 .MailFolders[parentMailFolderID].ChildFolders
                 .Request()
                 .AddAsync(childFolder );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文