feat: add support for custom Outlook folder search

- Allow searching custom folders by name (e.g., "Projects", "Archive")
- Add recursive folder search through all stores/accounts
- Include Inbox subfolders when searching "All"
- Update parameter descriptions to document custom folder support

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-19 12:57:23 -05:00
parent dafc1d6866
commit 09181d0991

View File

@@ -27,7 +27,7 @@ public class EmailSearchTools
[Description("Number of days back to search (default 365)")] int daysBack = 365,
[Description("Maximum number of results to return (default 25)")] int maxResults = 25,
[Description("Number of results to skip for pagination (default 0)")] int offset = 0,
[Description("Outlook folder to search: Inbox, SentMail, Drafts, DeletedItems, Junk, or All (default All)")] string folder = "All",
[Description("Outlook folder to search: Inbox, SentMail, Drafts, DeletedItems, Junk, All, or any custom folder name (default All)")] string folder = "All",
[Description("Filter by attachment: 'true' for emails with attachments, 'false' for without, or filename to search")] string? hasAttachment = null,
[Description("Filter by importance: High, Normal, or Low")] string? importance = null,
[Description("Filter by category name")] string? category = null,
@@ -79,7 +79,7 @@ public class EmailSearchTools
public static string ReadEmail(
[Description("Exact or partial subject line to match")] string subject,
[Description("Date of the email (supports: yyyy-MM-dd, MM/dd/yyyy, dd/MM/yyyy)")] string date,
[Description("Outlook folder: Inbox, SentMail, Drafts, DeletedItems, Junk, or All (default All)")] string folder = "All")
[Description("Outlook folder: Inbox, SentMail, Drafts, DeletedItems, Junk, All, or any custom folder name (default All)")] string folder = "All")
{
try
{
@@ -125,6 +125,9 @@ public class EmailSearchTools
{
folders.Add(ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox));
folders.Add(ns.GetDefaultFolder(OlDefaultFolders.olFolderSentMail));
// Also search subfolders of Inbox for "All"
var inbox = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
AddSubfolders(inbox, folders);
}
else if (folderMap.TryGetValue(folder, out var olFolder))
{
@@ -137,10 +140,74 @@ public class EmailSearchTools
// Folder may not exist (e.g., Archive on some configurations)
}
}
else
{
// Search for custom folder by name
var customFolder = FindFolderByName(ns, folder);
if (customFolder != null)
{
folders.Add(customFolder);
}
}
return folders;
}
private static MAPIFolder? FindFolderByName(_NameSpace ns, string folderName)
{
// Search through all accounts/stores
foreach (var store in ns.Stores)
{
if (store is Store s)
{
try
{
var rootFolder = s.GetRootFolder() as MAPIFolder;
if (rootFolder != null)
{
var found = SearchFolderRecursive(rootFolder, folderName);
if (found != null)
return found;
}
}
catch
{
// Skip stores that can't be accessed
}
}
}
return null;
}
private static MAPIFolder? SearchFolderRecursive(MAPIFolder parent, string folderName)
{
foreach (var subfolder in parent.Folders)
{
if (subfolder is MAPIFolder folder)
{
if (folder.Name.Equals(folderName, StringComparison.OrdinalIgnoreCase))
return folder;
var found = SearchFolderRecursive(folder, folderName);
if (found != null)
return found;
}
}
return null;
}
private static void AddSubfolders(MAPIFolder parent, List<MAPIFolder> folders)
{
foreach (var subfolder in parent.Folders)
{
if (subfolder is MAPIFolder folder)
{
folders.Add(folder);
AddSubfolders(folder, folders);
}
}
}
private static MailItem? FindEmail(MAPIFolder folder, string subject, DateTime targetDate)
{
var items = folder.Items;