jstedfast/MailKit · error · ArgumentException

The socket is not connected.

Error message

The socket is not connected.

What it means

Thrown as ArgumentException from CheckCanConnect(Socket,...) when a caller passes a Socket to ConnectSocket that is not in a connected state. MailKit requires an already-established socket since it will not dial the endpoint itself.

Solutions

  1. Call socket.Connect(host, port) (or the async equivalent) before passing it to ConnectSocket
  2. Verify socket.Connected is true before handing the socket to MailKit
  3. Create a new socket instead of reusing one that may have been closed

Example fix

// before
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.ConnectSocket(socket);
// after
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(host, port);
client.ConnectSocket(socket);
Defensive patterns

Strategy: validation

Validate before calling

if (socket == null || !socket.Connected)
    throw new InvalidOperationException("Provide a connected socket");

Type guard

bool IsUsableSocket(Socket s) => s is { Connected: true };

Try / catch

try { client.ConnectSocket(socket); }
catch (ArgumentException) { /* reconnect the socket yourself and retry */ }

Prevention

When it happens

Trigger: Passing a freshly created socket without calling Connect(); passing a socket that was closed or reset earlier; passing an unconnected socket from a connection pool.

Common situations: Custom socket/proxy setup code that builds the socket but forgets to connect it; reusing a socket after a previous IMAP session dropped it; async connect raced and failed silently before being handed to MailKit.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

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

				throw;
			}
		}

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

			CheckCanConnect (host, port);
		}

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

			if (!socket.Connected)
				throw new ArgumentException ("The socket is not connected.", nameof (socket));

			CheckCanConnect (host, port);
		}

		/// <summary>
		/// Establish a connection to the specified IMAP or IMAP/S server using the provided socket.
		/// </summary>
		/// <remarks>
		/// <para>Establishes a connection to the specified IMAP or IMAP/S server using
		/// the provided socket.</para>
		/// <para>If the <paramref name="options"/> has a value of
		/// <see cref="SecureSocketOptions.Auto"/>, then the <paramref name="port"/> is used
		/// to determine the default security options. If the <paramref name="port"/> has a value
		/// of <c>993</c>, then the default options used will be
		/// <see cref="SecureSocketOptions.SslOnConnect"/>. All other values will use
		/// <see cref="SecureSocketOptions.StartTlsWhenAvailable"/>.</para>
		/// <para>Once a connection is established, properties such as
		/// <see cref="AuthenticationMechanisms"/> and <see cref="Capabilities"/> will be

View on GitHub (pinned to 9d3859a785)