MailKit connection timeout error when accessing Gmail inbox

I’m trying to fetch messages from my Gmail account using MailKit library but keep running into connection issues. The application throws a socket timeout exception whenever I attempt to establish connection with the mail server.

Here’s the error I’m encountering:
System.Net.Sockets.SocketException: Connection timed out

My implementation looks like this:

using MailKit;
using MimeKit;
using System.Net;
using System.Threading;
using MailKit.Net.Pop3;

namespace EmailReader
{
    class Application
    {
        public static void Main(string[] args)
        {
            using (var popClient = new Pop3Client())
            {
                var mailServer = "gmail.com";
                var serverPort = "995";
                var enableSsl = false;
                var userCredentials = new NetworkCredential("[email protected]", "password");
                var tokenSource = new CancellationTokenSource();
                var serverUri = new Uri(string.Format("pop{0}://{1}:{2}", (enableSsl ? "s" : ""), mailServer, serverPort));

                // Establish server connection
                popClient.Connect(serverUri, tokenSource.Token);
                popClient.AuthenticationMechanisms.Remove("XOAUTH2");
                popClient.Authenticate(userCredentials, tokenSource.Token);

                // Retrieve messages
                for (int messageIndex = 0; messageIndex < popClient.Count; messageIndex++)
                {
                    var emailMessage = popClient.GetMessage(messageIndex);
                    Console.WriteLine("Subject: {0}", emailMessage.Subject);
                }

                // Close connection
                popClient.Disconnect(true);
            }
        }
    }
}

What could be causing this connection timeout issue?

The timeout’s happening because you’re hitting port 995 without SSL. Gmail’s POP3 server needs encrypted connections on that port, so when you set enableSsl = false, the handshake fails and times out. Just change enableSsl = true in your code. Also, if you’ve got 2FA turned on for your Gmail account, you’ll need an app-specific password - your regular password won’t work even with SSL set up correctly.

try using pop.gmail.com instead of gmail.com for your server. also, ensure ssl is enabled (enableSsl = true) since gmail requires secure connections on port 995, otherwise, you’ll keep getting that timeout.

Been there with the Gmail timeout dance. The SSL fix others mentioned works, but here’s what actually saved me time with email integrations at scale.

Skip the MailKit headaches and automate the whole workflow instead. Set up a scenario connecting to Gmail’s API, pull messages by your criteria, and process them however you want.

No socket timeouts, no app passwords, and you can add filtering, forwarding, or data extraction without touching code. When Gmail changes something, you update the workflow instead of debugging connections.

I use this for support emails, invoice processing, and personal email sorting. Way more reliable than fighting POP3 connections.

Check it out: https://latenode.com