jstedfast/MailKit · error · FolderNotFoundException

The specified folder does not exist

Error message

The specified folder does not exist

What it means

When the subfolder is found locally cached/known but the queuing path indicates it cannot exist, GetSubfolder throws FolderNotFoundException carrying the requested name. This is MailKit's typed signal that the requested child mailbox does not exist.

Solutions

  1. Wrap in try/catch (FolderNotFoundException) and create the folder if missing
  2. List existing subfolders with GetSubfolders() to verify the exact name/casing first
  3. Ensure the server actually has the folder (LIST command) before assuming standard special folders exist

Example fix

// before
var archive = inbox.GetSubfolder("Archive"); // throws if missing
// after
IMailFolder archive;
try {
    archive = inbox.GetSubfolder("Archive");
} catch (FolderNotFoundException) {
    archive = inbox.Create("Archive", true);
}
Defensive patterns

Strategy: try-catch

Validate before calling

var existing = folder.GetSubfolders().FirstOrDefault(f => f.Name == name);
if (existing != null) return existing;

Try / catch

try {
    sub = folder.GetSubfolder(name);
} catch (FolderNotFoundException ex) {
    sub = folder.Create(name, true); // ensure-folder pattern
}

Prevention

When it happens

Trigger: Calling GetSubfolder(name) where TryQueueGetSubfolderCommand returns false and folder is null — the requested child folder does not exist under this parent (or the parent has no children because DirectorySeparator is '\0').

Common situations: Expecting a folder ('Sent', 'Drafts') the server never created; renamed or deleted folders; connecting to a fresh mailbox without standard folders; case-sensitivity mismatches.

Understand the failure class

Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15). Data as JSON: /api/errors/21eaa9926b165fbe. Report an issue: GitHub.

Appendix: source

Thrown at MailKit/Net/Imap/ImapFolder.cs:1945

		/// <exception cref="System.OperationCanceledException">
		/// The operation was canceled via the cancellation token.
		/// </exception>
		/// <exception cref="System.IO.IOException">
		/// An I/O error occurred.
		/// </exception>
		/// <exception cref="FolderNotFoundException">
		/// The requested folder could not be found.
		/// </exception>
		/// <exception cref="ImapProtocolException">
		/// The server's response contained unexpected tokens.
		/// </exception>
		/// <exception cref="ImapCommandException">
		/// The server replied with a NO or BAD response.
		/// </exception>
		public override IMailFolder GetSubfolder (string name, CancellationToken cancellationToken = default)
		{
			if (!TryQueueGetSubfolderCommand (name, cancellationToken, out var ic, out var list, out var fullName, out var encodedName, out var folder))
				return folder ?? throw new FolderNotFoundException (name);

			Engine.Run (ic);

			folder = ProcessGetSubfolderResponse (ic, list, encodedName);

			if (list.Count > 1 || folder == null) {
				// Note: if any folders returned in the LIST command are unparented, have the ImapEngine look up their
				// parent folders now so that they are not left in an inconsistent state.
				Engine.LookupParentFolders (list, cancellationToken);
			}

			if (folder == null)
				throw new FolderNotFoundException (fullName);

			return folder;
		}

		/// <summary>

View on GitHub (pinned to 9d3859a785)