2

I've been trying to get the count of unread emails in Gmail but I'm encountering some problems. I did a search and I found ImapX library that should help me achieve this, but the code I found here on StackOverFlow on previews questions doesn't work. This is my code right now:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string username = "my_email@gmail.com";
            string passwd = "my_pass";
            int unread = 0;

            ImapClient client = new ImapClient("imap.gmail.com", 993, true);
            bool result = client.IsConnected;

            if (result)
                Console.WriteLine("Connection Established");

            result = client.Login(username, passwd); // <-- Error here
            if (result)
            {
                Console.WriteLine("Logged in");
                FolderCollection folders = client.Folders;
               // Message messages = client.Folders["INBOX"].Messages;
                foreach (ImapX.Message m in client.Folders["INBOX"].Messages)
                {
                    if (m.Seen == false)
                        unread++;
                }
                Console.WriteLine(unread);
            }
        }
    }
}

The Error is:

The selected authentication mechanism is not supported" on line 26

which is result = client.Login(username, passwd);

Deduplicator
  • 44,692
  • 7
  • 66
  • 118

3 Answers3

0

Sample from ImapX:

var client = new ImapX.ImapClient("imap.gmail.com", 993, true);
client.Connection();
client.LogIn(userName, userPassword);
var messages = client.Folders["INBOX"].Search("ALL", true);

Maybe you have enabled Two-factor authentication and you need generate application password. Othery way you will receive an e-mail warning that something is trying to access your mailbox and you must add your application as an exception. As an alternate solution you can try https://github.com/jstedfast/MailKit sample code at the bottom of the README

IamK
  • 2,753
  • 5
  • 30
  • 39
0

Most likely, gmail is looking for you to do a STARTTLS command, which it appears ImapX does not support. If you look at the response to the IMAPX1 CAPABILITY request, you'll likely see a "LOGINDISABLED" element, which means the server won't accept the "LOGIN" statement yet. So even if you UseSSL, the server (Microsoft Exchange in my case) is still looking for the STARTTLS command before it will let me LOGIN.

NitrusCS
  • 597
  • 1
  • 5
  • 20
0

You have to make the connection.

        ImapClient client = new ImapClient("imap.gmail.com", 993, true);
        client.Connect();
        bool result = client.IsConnected;

So if you added the line client.Connect(), I think it solves your problem.

Sara
  • 1
  • 1
  • 2