0

Hi I am trying to connect to gmail using ImapX library in C#. But getting error like this No connection could be made because the target machine actively refused it 74.125.25.109:993 while creating TcpClient (inside ImapX). I browse few same questions on stackoverflow but none was helpful to me.

Here is my code.

static void Main(string[] args)
{
    var client = new ImapClient("imap.gmail.com",993, true);
    client.SslProtocol = System.Security.Authentication.SslProtocols.Ssl2;
    client.UseSsl = true;
    if (client.Connect()) // This method creates new instance of TcpClient which throws error so returning false from catch block method has been described below.
    {

        if (client.Login("example@gmail.com", "example123"))
        {
            // login successful
        }
        Console.WriteLine("Connection Successful...!");
        Console.ReadLine();
    }
    else
    {
        Console.WriteLine("Connection UnSuccessful...!");
        Console.ReadLine();
        // connection not successful
    }
}

Here is Client method in ImapX library.

public bool Connect(string host, int port, SslProtocols sslProtocol = SslProtocols.None,
    bool validateServerCertificate = true)
{
    _host = host;
    _port = port;
    _sslProtocol = sslProtocol;
    _validateServerCertificate = validateServerCertificate;

    if (IsConnected)
        throw new InvalidStateException("The client is already connected. Please disconnect first.");

    try
    {

        _client = new TcpClient(_host, _port);
        if (_sslProtocol == SslProtocols.None)
        {
            _ioStream = _client.GetStream();
            _streamReader = new StreamReader(_ioStream);
        }
        else
        {
            _ioStream = new SslStream(_client.GetStream(), false, CertificateValidationCallback, null);
            (_ioStream as SslStream).AuthenticateAsClient(_host, null, _sslProtocol, false);
            _streamReader = new StreamReader(_ioStream);
        }


        string result = _streamReader.ReadLine();

        _lastActivity = DateTime.Now;

        if (result != null && result.StartsWith(ResponseType.ServerOk))
        {
            Capability();
            return true;
        }
        else if (result != null && result.StartsWith(ResponseType.ServerPreAuth))
        {
            IsAuthenticated = true;
            Capability();
            return true;
        }
        else
            return false;
    }
    catch (Exception)
    {
        return false;
    }
    finally
    {
        if (!IsConnected)
            CleanUp();
    }
}

Thanks in advance.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
Jenish Rabadiya
  • 6,708
  • 6
  • 33
  • 62
  • If I use Telenet then I am getting like this. Microsoft Telnet> open imap.gmail.com 993 Connecting To imap.gmail.com...Could not open connection to the host, on port 99 3: Connect failed – Jenish Rabadiya Sep 11 '14 at 09:10
  • Are you able to connect with some mail client sw such as thunderbird? Do you have any proxy configured? – František Žiačik Sep 11 '14 at 09:12
  • @František Žiačik I am not sure about thunderbird instead I am using Windows Live mail as my mail client and I do not have any proxy configured. – Jenish Rabadiya Sep 11 '14 at 09:17
  • Did you try different protocols? Like client.SslProtocol = System.Security.Authentication.SslProtocols.Default. I would expect SSl2 is fine, but you never know. – bastijn Sep 11 '14 at 09:23
  • @bastijn yes none of them is working and I have tried connecting pop server using openpop that works fine with me but I need to perform mail transfer using Imap only. – Jenish Rabadiya Sep 11 '14 at 09:26
  • Your code looks decent on first sight. Maybe have a look at google's OAuth and dont forget to register your application in your google account? First google hit: http://www.limilabs.com/blog/oauth2-gmail-imap-service-account . – bastijn Sep 11 '14 at 09:32
  • 1
    Somebody is blocking you. Could be your firewall. Could by a proxy. Could be your anti-malware. Could be Google because your IP address was used to send way too much spam. – Hans Passant Sep 11 '14 at 12:10
  • @HansPassant Thanks Hans on the another machine my code gets working there is something which is blocking me.Thank you to all for your time. – Jenish Rabadiya Sep 11 '14 at 13:43

1 Answers1

0

When you're using ImapX with GMail, the following code is enough to establish the connection:

var client = new ImapClient("imap.gmail.com", true);
if (client.Connect()) {
    // ...
}

It will use SSL with the standard 993 port. If you want to specify the SSL version manually, for GMail you need to use SslProtocols.Default, which is equivalent to SslProtocols.Ssl3 | SslProtocols.Tls

Pavel Azanov
  • 188
  • 1
  • 11
  • You are right. The problem was with my machine and network who was not allowing me to connect to imap.gmail.com on port 993. I tried with another machine and it started working. Thanks for your time. – Jenish Rabadiya Nov 17 '14 at 03:55