I have implemented the following sample code to access gmail and get the first 10 mail subjects using AE.Net.Mail.
I wonder what is exactly going wrong, I am repeatedly getting the error
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 173.194.79.108:993
This is the code.
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using AE.Net.Mail;
using AE.Net.Mail.Imap;
using System.Configuration;
namespace IMAP
{
class Program
{
private static void Main()
{
var items = ReadMail();
if (items != null && items.Count > 0)
{
foreach (var item in items.Take(10))
{
Console.WriteLine(item.Subject);
}
}
Console.ReadLine();
}
public static List<MailMessage> ReadMail()
{
List<MailMessage> messages = null;
try
{
string userName = "[email protected]"; // Replace with your actual gmail id
string passWord = "password"; // Replace with your password
if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(passWord))
{
using (var imapClient =
new ImapClient("imap.gmail.com", userName, passWord, ImapClient.AuthMethods.Login, 993, true))
{
imapClient.SelectMailbox("INBOX");
messages = new List<MailMessage>(imapClient.GetMessageCount());
messages = imapClient.GetMessages(0, 100, false, true).ToList();
imapClient.Disconnect();
}
}
else
{
Console.WriteLine("Username or Password is empty!");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return messages;
}
}
}
Please update me if any one faced a similar issue like this, and how resolved it.
Thanks, Sriram
I assume the error you're getting is from the exception thrown from the ImapClient
constructor which tries to connect to the remote IMAP server. That error is Winsock error 10060 (Connection timed out.) which usually happens if your connect packets are blocked or if you try to connect to a wrong host or to a wrong port. You have couple of options here:
imap.gmail.com
and see if it's reachableopenssl s_client -connect imap.gmail.com:993
to check whether you can connect to IMAP server. If you get connected, you'll be presented with gmail's IMAP server certificate and with message * OK Gimap ready for requests
after which you can type IMAP login command 0 LOGIN username pass
.993
open?imap.googlemail.com
instead of imap.gmail.com
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With