This project is read-only.

Check for new messages

Jul 1, 2008 at 1:03 AM
Hi,

I'm just wondering if there is a way to check for new messages, while still connected to the server? The code I have at the moment is below;
           
            IMAPFolder inbox;
            int count = 0;

            while (count++ < 100)
            {
                inbox = client.Folders["INBOX"];
                Console.WriteLine("Message Count in Inbox: " + inbox.Messages.Count);
                Thread.Sleep(1000);
            }

While this is running, I send myself a test message, but it doesn't update the count. Is there a way to 'reexamine' a folder to check for new messages that have been received while we are connected to the server? Once I disconnect from the server, and reconnect, the message count is updated correctly.

Thanks.


Jul 1, 2008 at 4:55 AM
Edited Jul 1, 2008 at 4:59 AM
OK, for anyone interested, I have found how to do this. According to the IMAP specification, you first need use the CLOSE command to close the connection to the folder you have selected. Once you've done this, you can then SELECT that folder again (without disconnecting from the server), and EXAMINE the contents. This updates the message count of a folder, without having to logout and login again.

Unfortunately, the CLOSE command hasn't been implemented in InterIMAP.

As a workaround, you can simply SELECT or EXAMINE another folder, and this will break the connection to the current folder. If you then SELECT the initial folder, it will be EXAMINEd again, and you will have your updated message count, without disconnecting from the server.

Below is an example using Gmail (remember that the [Gmail] folder isn't selectable);
           
            /* assume inbox is currently selected */
            IMAPFolder inbox = client.Folders["INBOX"];
            IMAPFolder selectedFolder = client.Folders["[Gmail]"].SubFolders["Starred"];

            selectedFolder.Select();
            inbox.Examine();
            WriteLine("Message Count in " + inbox.FolderName + ": " + inbox.Messages.Count);

If there is any chance of getting the CLOSE command implemented, that would be ideal. Thanks.
Jul 1, 2008 at 2:31 PM
Hello,

I just checked in new code to facilitate the checking of new messages in a folder. It looks like this:

IMAPFolder f = client.Folders["INBOX"];

            int[] msgCount = null;

            while (msgCount.Length == 0)
            {
                msgCount = f.CheckForNewMessages();
                Thread.Sleep(1000);
            }

            foreach (int id in msgCount)
            {
                IMAPMessage msg = f.GetMessageByID(id);
                // do some logic here
                msg.MarkAsRead();
            }

As i mentioned in the changeset comments, the library now obeys the AutoGetMsgUIDs flag in the config, and will not automatically download all the UIDs in the selected folder. This should allow you to connect to the server, select a folder and check for new messages without the need to touch any existing messages. As the example above shows, the CheckForNewMessages returns an array of ints which are the UIDs of the new messages. To process these new messages you can use the GetMessageByID method to download them individually.

If you attempt to access the Messages collection of the folder, it will force ALL of the UIDs to be downloaded which may not be desired.

Hope this addresses the problem you were having,
Jason
Aug 31, 2008 at 4:45 AM
int[] msgCount = null;

            while (msgCount.Length == 0)// msgCount is null; will hit an exception :)
            {
                msgCount = f.CheckForNewMessages();
                Thread.Sleep(1000);
            }
//below will do

do

{

msgCount = f.CheckForNewMessages();

 

Thread.Sleep(1000);

 

}

 

while ((msgCount == null) & (msgCount.Length == 0));

 

Nov 25, 2008 at 4:09 PM
is there any reason why there is a Thread.Sleep(1000); in that code?

It seems to work without that as well. (I do have config.AutoGetMsgID = false; though, maybe it is related?)
Nov 25, 2008 at 4:40 PM
That was just put in to limit the number of calls to the IMAP server to check for new messages. In most cases it is not necessary to spam the server as often as possible.
Dec 10, 2009 at 12:58 PM
Edited Dec 21, 2009 at 1:11 PM

This doesn't work for me at all man!

Jason don't you have a simple documentation for using this?

IMAP l = new IMAP();

              bool logged = l.Login("imap.gmail.com", "******", "*******", true);
              if (logged == true)
              {
                  IMAPFolder f = new IMAPFolder("INBOX", "INBOX");
                  l.SelectFolder(f);
                  System.Collections.ArrayList list = new System.Collections.ArrayList();
                  int mails = 0;
                  int[] msgCount = null;

                  do
                  {
                      msgCount = f.CheckForNewMessages();
                      Thread.Sleep(1000);
                  } while (msgCount.Length == 0 || msgCount.Length == null);  /// object not set to a reference error?

                  foreach (int id in msgCount)
                  {
                      IMAPMessage msg = f.GetMessageByID(id);
                      // do some logic here
                      msg.MarkAsRead();
                  }

 

doesn't work object not set to a reference error

please help

Dec 20, 2009 at 11:54 PM

koitohay,

In reference to the below line;

} while (msgCount.Length == 0 || msgCount.Length == null);  /// object not set to a reference error?

Your code is attempting to check an element of a variable that may not have been set. That is, if "msgCount" is null, then obviously "msgCount.Length" won't be set as anything, and that's why you're getting the "object not set to a reference error".

To solve this, use dump3roses' code above;

} while ((msgCount == null) & (msgCount.Length == 0));

This will first check if the msgCount variable is null, then check the length of the new messages if it is not null.

If that's not what you'd intended to do, please post back.

Dec 21, 2009 at 10:58 AM

Hi Haser,

 

thanks for your response,

 

I tried that and i still get the same error. when i try to debug i see that even without checking the folder inbox message count is 0. CAn you please provide me with a simple code on how to connect and check if there are new messages and get the message.

Thanks.