使用MailSystem.net时,“命令”列表“收件箱”“*”“失败”

时间:2017-10-31 12:31:55

标签: c# imap gmail-imap

我正在尝试使用MailSystem.Net从我的Gmail帐户中检索邮件,但是我收到了上述错误。我似乎没有在googl上找到任何与此类错误相关的链接。这是我的代码

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;
            }
        }

这是我得到的错误:命令“list”收件箱“”*“”失败:171031010631135 BAD未知命令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
                }
            }
        }

我做错了什么?我只是在复制我在网上看到的演示目的。

1 个答案:

答案 0 :(得分:1)

您是否尝试过登录?

您似乎因为从未登录而收到该错误,因此LIST命令无效。

从您的示例中,您删除了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
    }