Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to use the MailSystem.Net to retrieve mails from my gmail account but i get the above error. I have just follow through an example online to make a demo application as a window service. I tried to log the errors to a folder and after running the service i get the above error repeatedly in my error error folder

What I have tried:

<pre lang="c#">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);
                    Client.Login(login, password);


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






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
                }
            }
        }
Posted
Updated 1-Nov-17 1:42am

1 solution

It looks like connections are not closed (disconnected) when not used anymore.

To know how to do that you should read the documentation of the used Imap4Client class.

I would expect that connections are closed in the Imap4Client destructor. But even then it would be better to disconnect explicitly.

You can add a disconnect method to the MailRepository class and call that when finished using it (when returning from ReadImap):
C#
public void Disconnect()
{
    // Check the Imap4Client documentation for functions doing this
    if (Client.isConnected())
        Client.Disconnect();
}
But note that you can't query data anymore after doing so.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900