jstedfast/MailKit · warning · MessageNotFoundException

The IMAP server did not return the requested message.

Error message

The IMAP server did not return the requested message.

What it means

ProcessGetMessageResponse throws MessageNotFoundException when, after processing the FETCH response, the server did not return the requested message section. This happens when the message was expunged between listing and fetching, or the server silently omitted it. It means the UID no longer resolves to retrievable content on the server.

Solutions

  1. Catch MessageNotFoundException and treat the message as gone; refresh the summary.
  2. Verify folder.UidValidity has not changed; re-open the folder if it did.
  3. Retry the fetch once after a NOOP/examine to re-sync flags and expunges.

Example fix

// before
var msg = folder.GetMessage(uid);
// after
try {
    var msg = folder.GetMessage(uid);
} catch (MessageNotFoundException) {
    // message was expunged server-side; refresh and continue
    folder.NoOp();
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (folder.UidValidity != savedUidValidity) { /* reopen folder and refetch summaries */ }

Try / catch

try { return folder.GetMessage(uid); }
catch (MessageNotFoundException) {
    // expunged server-side: resync and treat as deleted
    folder.NoOp();
    return null;
}

Prevention

When it happens

Trigger: Message expunged by another client between folder.Fetch and GetMessage; UIDVALIDITY changed so the UID no longer maps to a message; server-side race where the FETCH returns no matching untagged FETCH response.

Common situations: Long-lived mailbox connections with aggressive server-side cleanup (spam purge); multiple clients on the same account; fetching messages by cached UIDs from a previous session.

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/c38ff0979f47a849. Report an issue: GitHub.

Appendix: source

Thrown at MailKit/Net/Imap/ImapFolderFetch.cs:3329

				throw new ArgumentException ("The uid is invalid.", nameof (uid));

			CheckState (true, false);

			var ic = new ImapCommand (Engine, cancellationToken, this, "UID FETCH %u (BODY.PEEK[])\r\n", uid.Id);
			ic.RegisterUntaggedHandler ("FETCH", FetchStreamHandler);
			ic.UserData = ctx = new FetchStreamContext (progress);

			Engine.QueueCommand (ic);

			return ic;
		}

		Stream ProcessGetMessageResponse (ImapCommand ic, FetchStreamContext ctx, UniqueId uid)
		{
			ProcessFetchResponse (ic);

			if (!ctx.TryGetSection (uid, string.Empty, out var section, true))
				throw new MessageNotFoundException ("The IMAP server did not return the requested message.");

			return section.Stream;
		}

		/// <summary>
		/// Get the specified message.
		/// </summary>
		/// <remarks>
		/// Gets the specified message.
		/// </remarks>
		/// <returns>The message.</returns>
		/// <param name="uid">The UID of the message.</param>
		/// <param name="cancellationToken">The cancellation token.</param>
		/// <param name="progress">The progress reporting mechanism.</param>
		/// <exception cref="System.ArgumentException">
		/// <paramref name="uid"/> is invalid.
		/// </exception>
		/// <exception cref="System.ObjectDisposedException">

View on GitHub (pinned to 9d3859a785)