Failing to Log into Gmail

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


var client = new IMAPClient();
client.Security = SecurityPolicies.Explicit;
client.Connect(", 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?

Apr 13, 2011 at 2: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 =>

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("", 993);
                client.Login("", "");

        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;


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