{"record":{"id":"0d71311bf149e2e4","repo":"jstedfast/MailKit","slug":"the-host-name-cannot-be-empty","errorCode":null,"errorMessage":"The host name cannot be empty.","messagePattern":"The host name cannot be empty\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/MailService.cs","lineNumber":955,"sourceCode":"\t\t/// <exception cref=\"System.OperationCanceledException\">\n\t\t/// The operation was canceled via the cancellation token.\n\t\t/// </exception>\n\t\t/// <exception cref=\"System.Net.Sockets.SocketException\">\n\t\t/// A socket error occurred trying to connect to the remote host.\n\t\t/// </exception>\n\t\t/// <exception cref=\"System.IO.IOException\">\n\t\t/// An I/O error occurred.\n\t\t/// </exception>\n\t\t/// <exception cref=\"ProtocolException\">\n\t\t/// A protocol error occurred.\n\t\t/// </exception>\n\t\tpublic void Connect (string host, int port, bool useSsl, CancellationToken cancellationToken = default)\n\t\t{\n\t\t\tif (host == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (host));\n\n\t\t\tif (host.Length == 0)\n\t\t\t\tthrow new ArgumentException (\"The host name cannot be empty.\", nameof (host));\n\n\t\t\tif (port < 0 || port > 65535)\n\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (port));\n\n\t\t\tConnect (host, port, useSsl ? SecureSocketOptions.SslOnConnect : SecureSocketOptions.StartTlsWhenAvailable, cancellationToken);\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Asynchronously establish a connection to the specified mail server.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// <para>Asynchronously establishes a connection to the specified mail server.</para>\n\t\t/// <note type=\"note\">\n\t\t/// <para>The <paramref name=\"useSsl\"/> argument only controls whether or\n\t\t/// not the client makes an SSL-wrapped connection. In other words, even if the\n\t\t/// <paramref name=\"useSsl\"/> parameter is <see langword=\"false\" />, SSL/TLS may still be used if\n\t\t/// the mail server supports the STARTTLS extension.</para>\n\t\t/// <para>To disable all use of SSL/TLS, use the","sourceCodeStart":937,"sourceCodeEnd":973,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/MailService.cs#L937-L973","documentation":"Connect(string host, int port, bool useSsl) requires a non-empty host string to resolve via DNS. An empty string cannot be a host name, so MailKit throws ArgumentException rather than attempting a DNS lookup that would inevitably fail.","triggerScenarios":"Calling Connect with host == string.Empty, e.g. Connect(\"\", 587, true).","commonSituations":"Missing or misnamed key in configuration producing an empty default (host = config[\"SmtpHost\"] ?? \"\"), environment variable not set, or appsettings/env mismatch after deployment.","solutions":["Provide the actual SMTP/IMAP host, e.g. Connect(\"smtp.example.com\", 587, true)","Fix the config source so the host key is populated (appsettings.json, env var)","Guard in code: if (string.IsNullOrEmpty(host)) fail with a clear config error before calling Connect"],"exampleFix":"// before\nstring host = config[\"SMTP_HOST\"] ?? \"\";\nclient.Connect(host, 587, true);\n// after\nstring host = config[\"SMTP_HOST\"] ?? \"smtp.example.com\";\nclient.Connect(host, 587, true);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(host))\n    throw new InvalidOperationException(\"SMTP host is not configured\");\nclient.Connect(host, port, useSsl);","typeGuard":"static bool HasHost(string host) => !string.IsNullOrWhiteSpace(host);","tryCatchPattern":"try { client.Connect(host, port, useSsl); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"host name cannot be empty\")) { throw new ConfigurationException(\"SMTP host is missing — check SMTP_HOST/appsettings\", ex); }","preventionTips":["Never default host config values to empty string; fail fast at startup if missing","Validate required mail settings (host, port, user, password) once at application startup","Keep host as a plain string; only wrap in Uri when a scheme is intentionally present"],"tags":["mailkit","argumentexception","configuration","connection"],"backgroundTag":"empty-required-field","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}