This project is read-only.

How get all messages from all folders?

Topics: Help requests
Jan 26, 2016 at 8:13 AM
Hi,
i am using the latest build of ImapX, and i wanted to get messages and attachments from all folders

That is my code, i can get all folders, but messages not. How to get messages?
if (client.Login(login, pass))
                {
                    // login successful
                    FolderCollection folders = client.Folders;
                    foreach (Folder myfolder in folders)
                    {

                        var messages = myfolder.Messages;

                        foreach (var message in messages)
                        {
                            MessageBox.Show(message.Subject);
                            var attachments = message.Attachments;
                            if (attachments.Count() > 0)
                                foreach (var attachment in attachments)
                                {
                                    MessageBox.Show(attachment.FileName);
                                }
                        }
                    }
                }
Jan 27, 2016 at 11:57 AM
Hi Ivan,

there are multiple ways for fetching the messages.

You can configure the client to always fetch the messages once you access Messages:
client.Behavior.AutoPopulateFolderMessages = true; 
This is not a good practice as it gets you less control about what and how you download. A better way is to use the Download or Search method:
/* .. Init client, authenticate .. */        
/* .. folder = any folder .. */  
folder.Messages.Download(/* optional, add filter, change download mode, limit number of messages */)
/* .. Init client, authenticate .. */        
/* .. folder = any folder .. */  
var messages = folder.Search(/* optional, add filter, change download mode, limit number of messages */)
More details can be found in the documentation:
Greets,

Pavel