Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing GMail using AE.Net.Mail in C#

Tags:

c#

gmail

imap

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

like image 722
Sriram B Avatar asked Jul 22 '13 09:07

Sriram B


1 Answers

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:

  • ping/traceroute imap.gmail.com and see if it's reachable
  • 993 is IMAP SSL port so you can use openssl 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.
  • check your proxy settings. Try to connect without using proxies.
  • check your router/firewall setting. Is port 993 open?
  • try using imap.googlemail.com instead of imap.gmail.com
like image 69
Bojan Komazec Avatar answered Sep 22 '22 13:09

Bojan Komazec