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?