Get new messages

May 14, 2011 at 8:19 AM

Hello

I want to use this library, but somehow I can not get it to work. Only the sync model, but it does not get maintained anymore, so I do not want to use that.

I have det following code:

using System;
using System.Collections.Generic;
using System.Text;
using InterIMAP;
using InterIMAP.Asynchronous.Client;
using InterIMAP.Common.Interfaces;
using InterIMAP.Common.Requests;
using InterIMAP.Synchronous;
using InterIMAP.Common.Processors;

namespace IMAPTest
{
    class Program
    {
        private static void FolderTreeCallback(IRequest req)
        {
            //throw new NotImplementedException();
        }

        static void nmr_NewMessageRequestCompleted(IMessage[] messages)
        {
            throw new NotImplementedException();
        }

        static void Main()
        {
            IMAPConfig config = new IMAPConfig("******", "*****", "******", false, false, "");
            IMAPAsyncClient client = new IMAPAsyncClient(config, 5);
            client.Start();

            IBatchRequest batch = new SimpleBatchRequest();
            client.Start();

            FolderTreeRequest ftr = new FolderTreeRequest("\"\"", null);
            client.RequestManager.SubmitAndWait(ftr, false);

            /*
             * Get INBOX folder
             */
            Console.WriteLine("Get INBOX.");
            IFolder folder = null;
            FolderDataRequest fdr = new FolderDataRequest(client.MailboxManager.GetFolderByPath("INBOX"), null);
            fdr.RequestCompleted += delegate(IRequest req)
            {
                FolderDataProcessor fdp = req.GetProcessorAsType<FolderDataProcessor>();
                folder = fdp.Request.Command.ParameterObjects[0] as IFolder;
            };

            batch.Requests.Add(fdr);

            client.RequestManager.SubmitBatchAndWait(batch, false);
            batch.Requests.Clear();

            MessageListRequest mlr = new MessageListRequest(folder, null);
            mlr.RequestCompleted += delegate(IRequest req2)
            {
                MessageListProcessor fdp2 = req2.GetProcessorAsType<MessageListProcessor>();

                IFolder f2 = fdp2.Request.Command.ParameterObjects[0] as IFolder;
                Console.WriteLine("Message List for {0} loaded. {1} Messages found.", f2.Name, f2.Exists);

                IMessage[] messages = f2.Messages;
            };

            batch.Requests.Add(mlr);
            client.RequestManager.SubmitBatchAndWait(batch, false);

            Console.WriteLine("Press any key");
            Console.ReadLine();

            client.Stop();
        }
    }
}


In the line "IMessage[] messages = f2.Messages" I would expect it to get all messages. But it does not. It only gives me empty objects. It does however gives me the message count!! But gives me empty objects.

I have tried the sync model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InterIMAP;
using InterIMAP.Synchronous;

namespace InterIMAPSync
{
    class Program
    {
        static void Main(string[] args)
        {
            IMAPConfig config = new IMAPConfig("****", "*****", "*****", false, false, "");
            InterIMAP.Synchronous.IMAPClient client = new InterIMAP.Synchronous.IMAPClient(config, null, 5);

            client.Logon();

            IMAPMessageCollection messages = client.GetSingleFolder("INBOX").Messages;

            foreach (IMAPMessage message in messages)
            {
                Console.WriteLine(message.ThreadTopic);

            }

            
        }
    }
}

This works just fine, is easy readable, but it does not get maintained anymore, so I want to use the async way of doing it, but is there a way of making the async code as readable as the sync code?

I have tried to use this code snippet:

static void Main()
{
IMAPConfig config = new IMAPConfig("mail.myhost.tl", "xxxx@xxx.xx", "xxxxxx", false, false, "\"\"");
IMAPAsyncClient client = new IMAPAsyncClient(config, 5);
client.Start();
client.RequestManager.SubmitAndWait(new FolderTreeRequest("\"\"", FolderTreeCallback), false);

NewMessagesRequest nmr = new NewMessagesRequest(client);
nmr.NewMessageRequestCompleted += new NewMessagesRequest.NewMessageRequestCompletedHandler(nmr_NewMessageRequestCompleted);
//nmr.Start(); // checks for new messages in all folders
IFolder folder = client.MailboxManager.GetFolderByPath("INBOX");
nmr.Start(folder); // checks for new messages in the INBOX folder only
}

private static void FolderTreeCallback(IRequest req)
{
// whatever logic you need to do to indicate the process has completed.
}

static void nmr_NewMessageRequestCompleted(IMessage[] messages)
{
throw new NotImplementedException();
}

But this does not throw any exceptions. Or do anything expected. This is readable, but does not work :(

Am I doing anything wrong in my examples (is there an more readable way of writing an async example that fetches all unseen messages from the server?)? Or is there an easier way of doing it, that I have not spotted?

Hope to hear from someone.

Coordinator
Jun 22, 2011 at 4:21 PM

In order to get the message information itself, you must fire off a FullMessageRequest passing in the client object, and the IMessage object.