jstedfast/MailKit · error · ArgumentException

The language code cannot be empty.

Error message

The language code cannot be empty.

What it means

Pop3Client.SetLanguage validates the lang argument and throws this ArgumentException when the language code string is non-null but empty. An empty string can never be a valid RFC 8158 language tag, so MailKit rejects it immediately before any network traffic.

Solutions

  1. Pass a valid language tag such as "en" or "de" obtained from client.GetLanguages().
  2. Guard the call: if (!string.IsNullOrEmpty (lang)) client.SetLanguage (lang, ...).
  3. Fix the configuration/default so the language setting is never an empty string.
  4. Catch ArgumentException and fall back to the server default language.

Example fix

// before
client.SetLanguage (config.Language, cancellationToken); // config.Language == ""
// after
if (!string.IsNullOrEmpty (config.Language))
    client.SetLanguage (config.Language, cancellationToken);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty (lang))
    throw new ArgumentException ("Language code must be a non-empty RFC 8158 tag", nameof (lang));

Try / catch

try {
    client.SetLanguage (lang, cancellationToken);
} catch (ArgumentException) {
    // empty/invalid language code: use server default
}

Prevention

When it happens

Trigger: Calling SetLanguage("") or SetLanguage(string.Empty); passing a language variable that was initialized to "" and never populated (e.g. failed lookup or split of an empty setting string).

Common situations: Reading a language code from configuration that defaults to empty; parsing a header or user preference that yielded ""; code path where lang = selectedLanguage && selectedLanguage is empty.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at MailKit/Net/Pop3/Pop3Client.cs:1734

		public IList<Pop3Language> GetLanguages (CancellationToken cancellationToken = default)
		{
			var pc = QueueLangCommand (out var langs);

			engine.Run (true, cancellationToken);

			return new ReadOnlyCollection<Pop3Language> (langs);
		}

		void CheckCanSetLanguage (string lang)
		{
			CheckDisposed ();
			CheckConnected ();

			if (lang == null)
				throw new ArgumentNullException (nameof (lang));

			if (lang.Length == 0)
				throw new ArgumentException ("The language code cannot be empty.", nameof (lang));

			if ((Capabilities & Pop3Capabilities.Lang) == 0)
				throw new NotSupportedException ("The POP3 server does not support the LANG extension.");
		}

		/// <summary>
		/// Set the language used by the POP3 server for error messages.
		/// </summary>
		/// <remarks>
		/// If the POP3 server supports the LANG extension, it is possible to
		/// set the language used by the POP3 server for error messages.
		/// </remarks>
		/// <param name="lang">The language code.</param>
		/// <param name="cancellationToken">The cancellation token.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="lang"/> is <see langword="null" />.
		/// </exception>
		/// <exception cref="System.ArgumentException">

View on GitHub (pinned to 9d3859a785)