jstedfast/MailKit · error · ArgumentException

The host name cannot be empty.

Error message

The host name cannot be empty.

What it means

ImapClient.CheckCanConnect validates arguments before connecting. This ArgumentException is thrown when the host string is non-null but empty (Length == 0), since an empty host cannot identify an IMAP server to connect to.

Solutions

  1. Supply the correct host name to Connect (e.g. "imap.example.com")
  2. Validate host before calling: if (string.IsNullOrWhiteSpace (host)) fail early with a clear config error
  3. Fix the source of the empty value: missing config key, empty env var, or bad URI parse

Example fix

// before
var host = Environment.GetEnvironmentVariable ("IMAP_HOST") ?? "";
client.Connect (host, 993, true); // ArgumentException
// after
var host = Environment.GetEnvironmentVariable ("IMAP_HOST") ?? throw new InvalidOperationException ("IMAP_HOST not set");
if (string.IsNullOrWhiteSpace (host)) throw new InvalidOperationException ("IMAP host is required");
client.Connect (host, 993, true);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace (host)) throw new ArgumentException ("IMAP host must be provided", nameof (host));

Type guard

bool IsValidHost (string? host) => !string.IsNullOrWhiteSpace (host);

Try / catch

try { client.Connect (host, port, options); } catch (ArgumentException ex) when (ex.ParamName == "host") { // surface a config error to the user
}

Prevention

When it happens

Trigger: Calling Connect("", port, ...) or passing an empty host from configuration, an unparsed URI, or a variable that defaulted to string.Empty.

Common situations: Missing config value that initialized host to ""; URL parsing that produced an empty host component; environment variable read that returned an empty string.

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

Appendix: source

Thrown at MailKit/Net/Imap/ImapClient.cs:1436

				break;
			case SecureSocketOptions.SslOnConnect:
				uri = new Uri (string.Format (CultureInfo.InvariantCulture, "imaps://{0}:{1}", host, port));
				starttls = false;
				break;
			default:
				uri = new Uri (string.Format (CultureInfo.InvariantCulture, "imap://{0}:{1}", host, port));
				starttls = false;
				break;
			}
		}

		void CheckCanConnect (string host, int port)
		{
			if (host == null)
				throw new ArgumentNullException (nameof (host));

			if (host.Length == 0)
				throw new ArgumentException ("The host name cannot be empty.", nameof (host));

			if (port < 0 || port > 65535)
				throw new ArgumentOutOfRangeException (nameof (port));

			CheckDisposed ();

			if (IsConnected)
				throw new InvalidOperationException ("The ImapClient is already connected.");
		}

		void SslHandshake (SslStream ssl, string host, CancellationToken cancellationToken)
		{
#if NET5_0_OR_GREATER
			ssl.AuthenticateAsClient (GetSslClientAuthenticationOptions (host, ValidateRemoteCertificate));
#else
			ssl.AuthenticateAsClient (host, ClientCertificates, SslProtocols, CheckCertificateRevocation);
#endif
		}

View on GitHub (pinned to 9d3859a785)