not returning the message

Feb 8, 2012 at 4:39 PM

I having trouble returning the message using a variable, but when I use the same value stored in the variable the message is returned?

 

// This fails to return the message

var view1 = client.FetchMessageBySequenceNumber(message.Uid);

 

// This succeeds a return the message

 var view2 = client.FetchMessageBySequenceNumber(4);





// This is the class that I store the inital data for the messages

 

private class

Messages

 

{

 

 

public Envelope envelope { get; set; }

 

 

 

 

 

 

 

public int Uid { get; set; }

 

}

Coordinator
Feb 8, 2012 at 8:42 PM

Hi,

unfortunately this is not enough information to find the error, make sure message.Uid has actually the value 4 stored.

Alex

Feb 9, 2012 at 8:35 AM

Hi Alex

Below is all of the code that selects the information from the ImapClient. I can confirm that the value stored in the Uid property is an Int and has the value of 4.

I have a screen capture but I can't see away of adding the image to this posting.

            #region Get Messages


            // Create a date range for the messages to return
            var since_from = DateTime.Now.AddDays(-1);
            var since_to = DateTime.Now.AddDays(1);

            var query = client.Messages
                .Where(x => x.Date > since_from && x.Date < since_to)
                .Select(x => new Messages
                {
                    envelope = x.Envelope,
                    Uid = x.Uid              
                });


            foreach (var message in query)
            {

                if (message.envelope != null)
                {
                    #region display message header
                    Console.WriteLine("Message To: {0}", message.envelope.To.FirstOrDefault().ToString());
                    Console.WriteLine("        Subject: {0}", message.envelope.Subject);
                    if (message.envelope.Cc.Count() == 0)
                    {
                        Console.WriteLine("        CC:");
                    }
                    else
                    {
                        foreach (var vCC in message.envelope.Cc)
                        {
                            Console.WriteLine("         CC: {0}", vCC.Name);
                        }
                    }


                    if (message.envelope.Bcc.Count() == 0)
                    {
                        Console.WriteLine("        BCC:");
                    }
                    else
                    {
                        foreach (var vBCC in message.envelope.Bcc)
                        {
                            Console.WriteLine("         BCC: {0}", vBCC.Name);
                        }
                    }



                    if (message.envelope.From.Count() == 0)
                    {
                        Console.WriteLine("        From:");
                    }
                    else
                    {
                        foreach (var vFrom in message.envelope.From)
                        {
                            Console.WriteLine("        From: {0}", vFrom.Name);
                        }
                    }
                    #endregion

                    int sn = int.Parse(message.Uid.ToString());
                    // This fails to return the message
                    var view1 = client.FetchMessageBySequenceNumber(sn);

                    // This sucedes to return the message
                    var view2 = client.FetchMessageBySequenceNumber(4);
                }
            #endregion
            }

Coordinator
Feb 9, 2012 at 11:54 AM

Hey,

You have actually two problems in your code.

First, you fetch the Uid but pass it into the FetchMessageBySequenceNumber method, you need to use FetchMessageByUid.

Secondly,

IMAP commands need to be applied sequentially and discrete. In order to be able to react on every message as it comes in without the need to wait for the complete batch to finish the iterator (for ... loop) does only fetch the data necessary for the current message in every iteration.
If you stop iteration at item 2 of 10, you actually would have only downloaded  2 items.

You are calling the FetchMessageBy ... methods inside the loop it means you send a new command before the old one has finished.
Although the server does handle this, the client cannot. The server will start sending replies for both commands at the same time through the same connection, essentially mixing apple's and oranges's. Unfortunately the client is not able to distinguish which response belongs to which command and will eventually fail at some point. You need to finish the loop before issuing new commands to the server or use a second connection.

Alex