jstedfast/MailKit · error · ArgumentException

All folders must be ImapFolders.

Error message

All folders must be ImapFolders.

What it means

The Mailboxes event group requires every folder in the list to be an ImapFolder (the concrete MailKit implementation), because it stores them as ImapFolder[] to format IMAP mailbox names. Passing any other IMailFolder implementation throws ArgumentException("All folders must be ImapFolders.").

Solutions

  1. Use only folders returned by ImapClient/ImapFolder APIs (GetFolder, GetSubfolders, Inbox) so they are real ImapFolder instances
  2. Unwrap decorator/wrapper IMailFolder implementations to the underlying ImapFolder before building the group
  3. In tests, use real ImapFolder objects or a fake IMAP server rather than interface mocks

Example fix

// before
IList<IMailFolder> folders = new List<IMailFolder> { new MyFolderWrapper(realFolder) };
var group = ImapEventGroup.Mailboxes("notify", folders);

// after
var folders = matchedFolders.OfType<ImapFolder>().ToList();
var group = ImapEventGroup.Mailboxes("notify", folders);
Defensive patterns

Strategy: type-guard

Validate before calling

var imapFolders = folders.OfType<ImapFolder>().ToList();
if (imapFolders.Count != folders.Count)
    throw new ArgumentException("List contains non-ImapFolder IMailFolder implementations.");

Type guard

bool AllAreImapFolders(IList<IMailFolder> folders) => folders.All(f => f is ImapFolder);

Try / catch

try {
    var group = ImapEventGroup.Mailboxes(name, folders);
} catch (ArgumentException ex) when (ex.Message.Contains("ImapFolders")) {
    // filter with OfType<ImapFolder>() and retry
}

Prevention

When it happens

Trigger: Calling ImapEventGroup.Mailboxes with a list containing IMailFolder instances that are not ImapFolder - e.g. wrapper/decorator implementations, mocks from a testing library, or folders from a different provider (IMailFolder fakes).

Common situations: Unit tests passing mock IMailFolder objects; custom adapter classes implementing IMailFolder around another store; mixing folders obtained from a non-ImapClient source.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at MailKit/Net/Imap/ImapEventGroup.cs:314

			/// </exception>
			/// <exception cref="System.ArgumentException">
			/// <para>The list of <paramref name="folders"/> is empty.</para>
			/// <para>-or-</para>
			/// <para>The list of <paramref name="folders"/> contains folders that are not of
			/// type <see cref="ImapFolder"/>.</para>
			/// </exception>
			internal Mailboxes (string name, IList<IMailFolder> folders) : base (name)
			{
				if (folders == null)
					throw new ArgumentNullException (nameof (folders));

				if (folders.Count == 0)
					throw new ArgumentException ("Must supply at least one folder.", nameof (folders));

				this.folders = new ImapFolder[folders.Count];
				for (int i = 0; i < folders.Count; i++) {
					if (folders[i] is not ImapFolder folder)
						throw new ArgumentException ("All folders must be ImapFolders.", nameof (folders));

					this.folders[i] = folder;
				}
			}

			/// <summary>
			/// Format the IMAP NOTIFY command for this particular IMAP mailbox filter.
			/// </summary>
			/// <remarks>
			/// Formats the IMAP NOTIFY command for this particular IMAP mailbox filter.
			/// </remarks>
			/// <param name="engine">The IMAP engine.</param>
			/// <param name="command">The IMAP command builder.</param>
			/// <param name="args">The IMAP command argument builder.</param>
			internal override void Format (ImapEngine engine, StringBuilder command, IList<object> args)
			{
				command.Append (Name);
				command.Append (' ');

View on GitHub (pinned to 9d3859a785)