我正在使用下面的代码来检索电子邮件。但是我在尝试连接服务器时遇到了 Socket 异常(client.Connect(uri, cancel.Token);)。
未处理的异常:System.Net.Sockets.SocketException:连接超时
using MailKit;
using MimeKit;
using System.Net;
using System.Threading;
using MailKit.Net.Pop3;
namespace TestMail
{
class Program
{
public static void Main (string[] args)
{
using (var client = new Pop3Client ()) {
var Server = "gmail.com";
var Port = "995";
var UseSsl = false;
var credentials = new NetworkCredential("abc@gmail.com", "abc");
var cancel = new CancellationTokenSource ();
var uri = new Uri(string.Format("pop{0}://{1}:{2}", (UseSsl ? "s" : ""), Server, Port));
//Connect to email server
client.Connect(uri, cancel.Token);
client.AuthenticationMechanisms.Remove ("XOAUTH2");
client.Authenticate (credentials, cancel.Token);
//Fetch Emails
for (int i = 0; i < client.Count; i++) {
var message = client.GetMessage (i);
Console.WriteLine ("Subject: {0}", message.Subject);
}
//Disconnect Connection
client.Disconnect(true);
}
}
}
}