2

I'm trying to receive email from Gmail. Occaisionly (approx 1 out of 5 times) I get a System.Net.Sockets.SocketException, error message:

A socket operation was attempted to an unreachable network [2a00:1450:4013:c01::6d]:993

The network address is not always the same, but varies slightly. This error does appears occaisonally on all the Gmail boxes I want to check, but does not appear on my Office 365 mailbox at all.

My app is an MVC 5 applications hosted by Microsoft Azure. I use the S22 Imap library

The relevant part of the code to retrieve the email is:

using S22.Imap;

ImapClient Client;

List<MailMessage> NewMessages;
try
{
    Client = new ImapClient(tenant.ImapHostName, 
    tenant.ImapPortNumber, 
    tenant.ImapUserName, 
    tenant.ImapPassword,
    AuthMethod.Login, tenant.UseSsl);
}
catch (Exception e)
{
    return;
}

try
{
    NewMessages = GetUnseenMessages(Client);
}
catch (Exception e)
{
}

I've disable IPv6 on my Azure webservice (disabled it on the adapter) but still this error comes back over and over again.

RHAD
  • 1,317
  • 3
  • 13
  • 37

2 Answers2

2

imap.gmail.com returns several IP addresses (three right now, but the number might vary depending on time and location). You're supposed to try all three. If one fails in the manner you see, you're supposed to try the next address, so your next step is to find out whether S22 does that, and if not, how you can make that happen.

arnt
  • 8,949
  • 5
  • 24
  • 32
  • Fwiw, my [MailKit](https://github.com/jstedfast/MailKit) library will try all IP addresses returned by DNS until one connects successfully. I mention this in case the OP is unable to easily work around this limitation in S22.Imap. Good luck! – jstedfast Dec 14 '16 at 23:58
1

I had the same problem as OP and did following to retry multiple IP addresses with S22.Imap.dll

        var ips = Dns.GetHostAddresses("imap.gmail.com");
        foreach(var ip in ips)
        {
            try
            {
                return new ImapClient(ip.ToString(), "993", Email, Password, AuthMethod.Login, true);
            }
            catch(SocketException e) //error means server is down, try other IP
            {
                //nothing, check next IP for connection
            }
        }
Mr.Hunt
  • 4,833
  • 2
  • 22
  • 28