{"record":{"id":"33cd6a6e3f0de702","repo":"jstedfast/MailKit","slug":"operation-timed-out-after-readtimeout-milliseconds","errorCode":null,"errorMessage":"Operation timed out after {readTimeout} milliseconds","messagePattern":"Operation timed out after (.+?) milliseconds","errorType":"exception","errorClass":"TimeoutException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/NetworkStream.cs","lineNumber":193,"sourceCode":"\t\t\t// Capture the ReadTimeout so even if we get an exception and disconnect the socket, we still have it.\n\t\t\tint readTimeout = ReadTimeout;\n\n\t\t\tusing (var timeout = new CancellationTokenSource (readTimeout)) {\n\t\t\t\tusing (var linked = CancellationTokenSource.CreateLinkedTokenSource (cancellationToken, timeout.Token)) {\n\t\t\t\t\tusing (var registration = linked.Token.Register (() => tcs.TrySetCanceled (), false)) {\n\t\t\t\t\t\trecv!.SetBuffer (buffer, offset, count);\n\t\t\t\t\t\trecv.UserToken = tcs;\n\n\t\t\t\t\t\tif (!Socket.ReceiveAsync (recv))\n\t\t\t\t\t\t\tAsyncOperationCompleted (null, recv);\n\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tawait tcs.Task.ConfigureAwait (false);\n\t\t\t\t\t\t\treturn recv.BytesTransferred;\n\t\t\t\t\t\t} catch (OperationCanceledException ex) {\n\t\t\t\t\t\t\tDisconnect ();\n\t\t\t\t\t\t\tif (timeout.IsCancellationRequested)\n\t\t\t\t\t\t\t\tthrow new TimeoutException ($\"Operation timed out after {readTimeout} milliseconds\", ex);\n\t\t\t\t\t\t\tthrow;\n\t\t\t\t\t\t} catch (Exception ex) {\n\t\t\t\t\t\t\tDisconnect ();\n\t\t\t\t\t\t\tif (ex is SocketException)\n\t\t\t\t\t\t\t\tthrow new IOException (ex.Message, ex);\n\t\t\t\t\t\t\tthrow;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tpublic override void Write (byte[] buffer, int offset, int count)\n\t\t{\n\t\t\ttry {\n\t\t\t\tSocket.Send (buffer, offset, count, SocketFlags.None);\n\t\t\t} catch (SocketException ex) {\n\t\t\t\tthrow new IOException (ex.Message, ex);","sourceCodeStart":175,"sourceCodeEnd":211,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/NetworkStream.cs#L175-L211","documentation":"MailKit's NetworkStream.ReadAsync wraps the socket receive in a cancellation-linked timeout; when the read doesn't complete within readTimeout, the OperationCanceledException is converted to a TimeoutException with the readTimeout in the message, and the stream disconnects first. It's the generic 'server took too long to send data' timeout.","triggerScenarios":"Any read on a NetworkStream-based connection (e.g. AcceptProxyConnection reading the proxy protocol header, IMAP/POP3/SMTP response reads) that exceeds the client's Timeout property; server hangs or stops responding mid-command.","commonSituations":"Slow or overloaded mail servers; very large responses over slow links where readTimeout is too small; firewalls silently dropping packets; debugging proxies that hold connections open.","solutions":["Increase the client's Timeout property (e.g. client.Timeout = 120000) to a value large enough for the slowest server response.","Catch TimeoutException and retry the operation with a fresh connection, since MailKit disconnects the stream on timeout.","Investigate server-side latency (queue size, greylisting on SMTP, bandwidth) if timeouts persist at reasonable values."],"exampleFix":"// before\nvar client = new ImapClient();\nclient.Connect(\"imap.example.com\", 993, true);\n\n// after\nvar client = new ImapClient();\nclient.Timeout = 120_000; // 2 minutes\nclient.Connect(\"imap.example.com\", 993, true);","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { await op(); }\ncatch (TimeoutException) {\n    // stream already disconnected; reconnect and retry with a larger Timeout\n    client.Timeout = 120_000;\n    await ReconnectAsync();\n    await op();\n}","preventionTips":["Set client.Timeout generously for slow servers or large payloads","Catch TimeoutException on every network call and reconnect — MailKit disconnects on timeout","Profile server latency before choosing timeout values"],"tags":["network","timeout","socket","mailkit"],"backgroundTag":"request-timeout","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}