0

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.

Max
  • 10,701
  • 2
  • 24
  • 48
Mcbaloo
  • 157
  • 5
  • 18
  • Please provide the entire error and traceback. If relevant, in your question – Max Oct 31 '17 at 13:15
  • Command "list "inbox" "*"" failed : 171031010631135 BAD Unknown command b7mb174701481wmf is one of the error i got. I wrote it as a service and i'm logging the error to a folder. Series of Command "list "inbox"" error is what i can find in the log folder i created – Mcbaloo Oct 31 '17 at 13:20
  • It looks like you never login. You pass your username and password and never use it. – Max Oct 31 '17 at 13:24
  • Again, provide your whole error in your QUESTION. Press the edit link to change your question and provide all pertinent information. – Max Oct 31 '17 at 13:24
  • Just did that now – Mcbaloo Oct 31 '17 at 13:42
  • The error and traceback is still not in the question. Do not put it in only in the title, it is not formatted nicely there. This isn't hard. – Max Oct 31 '17 at 14:30

1 Answers1

1

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
    }
Max
  • 10,701
  • 2
  • 24
  • 48
  • I just tried modifying the code to be sure i'm able to log in. I'm currently able to login. Its when i want to get my present unread messages that i get the error – Mcbaloo Oct 31 '17 at 13:40
  • Provide the code you are USING in the question. Your current code does not login, which is why you get this error. You have to login on every connection! – Max Oct 31 '17 at 14:29
  • I think i'm getting you a little. Like i said, i'm only using what i read online to practice. Still haven't gotten hold of it.. You said i'm currently unable to login in, but in the code i'm using, my gmail credential are there. Is there any other thing i need to do to log in?? You can help with a code snippet – Mcbaloo Oct 31 '17 at 14:36
  • You pass a username and password to your function. You do not use it. Have you tried the .login() function, as showed in the examples? – Max Oct 31 '17 at 14:38
  • Thanks for your time.. this is the link the the example i'm using https://stackoverflow.com/questions/7056715/reading-emails-from-gmail-in-c-sharp ... it doesn't have a .login function – Mcbaloo Oct 31 '17 at 14:43
  • 2
    Yes it does, right after Client.Connect: Client.Login(login, password); You have a bunch of extra newlines where it should be. – Max Oct 31 '17 at 14:53
  • Thanks for your time. I really appreciate. I think i can login now because i now have a new error which is Unexpected end of stream. Thanks a lot – Mcbaloo Oct 31 '17 at 15:05
  • Feel free to upvote and 'accept' the answer if it helped you. – Max Oct 31 '17 at 15:16