Failing to Log into Gmail

Apr 12, 2011 at 11:05 PM
Edited Apr 12, 2011 at 11:22 PM

Trying:

var client = new IMAPClient();
client.Security = SecurityPolicies.Explicit;
client.Connect("imap.gmail.com:, 993); bool loggedIn = client.Login(userName, password);

For some reason loggedIn = false. I've tried different accounts (gmail, google apps) to no avail.

I've tried where the username is the email address and also where it is just the username. Still false.

From debugging, I can see it goes into the else
public bool Login(NetworkCredential credentials)
        {
            var authenticator = new ImapAuthenticator(this);
            if (authenticator.CanAuthenticate) {
                IsAuthenticated = authenticator.Login(credentials);
            }
            else {
                InvokeManualSaslAuthenticationRequired(credentials, this);
            }

            return IsAuthenticated;
        }


Any pointers?

Coordinator
Apr 13, 2011 at 3:45 PM

Sure, you are not logged in because the client can't do it autonomously.
Gmail doesn't advertise any authentication mechanics other than XOAUTH, therefor the ManualSaslAuthenticationRequired event is being thrown, prompting you to do it by yourself, since the client can't do it for you.

You have two options:

1. use XOAUTH => http://equinox.codeplex.com/wikipage?title=Authenticating%20the%20User%20with%20XOAUTH&referringTitle=Read%20the%20Imap%20Programming%20Guide

2. enforce LOGIN mechanic (this will not work forever, Google won't remove it from the supported list without the intend to get rid of it someday), but it still works for now.

namespace ClassLibrary1
{
    public class Class1
    {
        public static void Main()
        {
            using (var client = new ImapClient()) {
                client.Security = SecurityPolicies.Explicit;
                client.ManualSaslAuthenticationRequired += OnManualSaslAuthenticationRequired;
                client.Connect("imap.gmail.com", 993);
                client.Login("", "");
            }
            Console.ReadKey(false);
        }

        private static void OnManualSaslAuthenticationRequired(object sender, ManualSaslAuthenticationRequiredEventArgs e)
        {
            var text = string.Format("LOGIN {0} {1}", "<username>", "<password>");
            var command = new RawCommand(text);
            var success = e.Client.SendAndReceive(command).IsOk;
            Console.WriteLine(success);
        }
    }
}

 

Apr 13, 2011 at 5:12 PM
I agree about xoauth, just not sure about the 2L way. When you will post an example, I will definitely convert it.