Search code examples
c#imapgmail-imap

'Command "list "inbox" "*"" failed' while using MailSystem.net


I'm trying to use the MailSystem.Net to retrieve mails from my gmail account but i get the above error. I don't seem to find any link related to such error on googl. Here's my code

public class MailRepository
        {
            private Imap4Client client;
            public MailRepository(string mailServer, int port, bool ssl, string login, string password)
            {
                if (ssl)

                    Client.ConnectSsl(mailServer, port);
                else

                    Client.Connect(mailServer, port);


            }
            public IEnumerable<Message> GetAllMails(string mailBox)
            {
                return GetMails(mailBox, "ALL").Cast<Message>();
            }

            public IEnumerable<Message> GetUnreadMails(string mailBox)
            {
                return GetMails(mailBox, "UNSEEN").Cast<Message>();
            }

            protected Imap4Client Client
            {
                get { return client ?? (client = new Imap4Client()); }
            }

            private MessageCollection GetMails(string mailBox, string searchPhrase)
            {
                Mailbox mails = Client.SelectMailbox(mailBox);
                MessageCollection messages = mails.SearchParse(searchPhrase);
                return messages;
            }
        }

this is the error i got : Command "list "inbox" "*"" failed : 171031010631135 BAD Unknown command b7mb174701481wmf

 private void ReadImap()
        {
            var mailRespository = new MailUtil.MailRepository("imap.gmail.com", 993, true, "myGmailAccount", "Mypassword");
            var emailList = mailRespository.GetAllMails("inbox");
            foreach(Message email in emailList)
            {
                //DoSomething

                if(email.Attachments.Count > 0)
                {
                    //DoSomething
                }
            }
        }

What am i doing wrong?? I'm just replicating what i read online here for Demo purposes.


Solution

  • Have you tried logging in?

    It looks like you're getting that error because you never logged in, so the LIST command is not valid.

    From your example, you dropped the Client.Login:

        public MailRepository(string mailServer, int port, bool ssl, string login, string password)
        {
            if (ssl)
                Client.ConnectSsl(mailServer, port);
            else
                Client.Connect(mailServer, port);
            Client.Login(login, password); // LINE YOU MISSED
        }