Retrieve Unread messages Imap

Hi guys,

Can someone tel how to get unread messages using Imap? I tried ListMessages but it gives all the messages from my inbox.

Mark

Hi Mark,

Thanks for writing to Aspose.Email support team.

ImapClient provides detailed features for processing account on Imap server. ImapFolderInfo, ImapMessageInfoCollection and ImapMessageInfo can be used to select folders/sub-folders and identify the read and unread mails in those folders.

Please give a try to the following sample code which login to Imap server, selects all folder and recursive sub-folders and then displays only those mails which are unread. These message are also saved on disc to demonstrate the storage of unread messages on the disc.

private static ImapClient client = null;
static void EMAIL_442910_Retrieve_Unread_Messages_Imap()
{
try
{
//Reading sender account start
client = new ImapClient(“[imap.gmail.com](http://imap.gmail.com/)”, 993, "user@gmail.com", “password”);

// set the security mode to explicit
client.SecurityMode = ImapSslSecurityMode.Implicit;

// enable SSL
client.EnableSsl = true;
client.Connect(true);
Console.WriteLine(“Connected to IMAP server.”);

// The root folder (which will be created on disk) consists of host and username
string rootFolder = client.Host + “-” + client.Username;

// Create the root folder
Directory.CreateDirectory(rootFolder);

// List all the folders from IMAP server
ImapFolderInfoCollection folderInfoCollection = client.ListFolders();
foreach (ImapFolderInfo folderInfo in folderInfoCollection)
{
// Call the recursive method to read messages and get sub-folders
ListMessagesInFolder(folderInfo, rootFolder);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
client.Disconnect();
Console.WriteLine(“Disconnected.”);
}
}
private static void ListMessagesInFolder(ImapFolderInfo folderInfo, string rootFolder)
{
// Create the folder in disk (same name as on IMAP server)
string currentFolder = rootFolder + “\” + folderInfo.Name;
Directory.CreateDirectory(currentFolder);

// Read the messages from the current folder, if it is selectable
if (folderInfo.Selectable == true)
{
// send status command to get folder info
ImapFolderInfo folderInfoStatus = client.ListFolder(folderInfo.Name);
Console.WriteLine(folderInfoStatus.Name + " folder selected. New messages: " + folderInfoStatus.NewMessageCount +
“, Total messages: " + folderInfoStatus.TotalMessageCount);

// Select the current folder
client.SelectFolder(folderInfo.Name);

// List messages
ImapMessageInfoCollection msgInfoColl = client.ListMessages();
Console.WriteLine(“Listing messages…”);
foreach (ImapMessageInfo msgInfo in msgInfoColl)
{
if (!msgInfo.IsRead)
{
// Get subject and other properties of the message
Console.WriteLine(“Subject: " + msgInfo.Subject);
Console.WriteLine(“Read: " + msgInfo.IsRead + “, Recent: " + msgInfo.Recent + “, Answered: " + msgInfo.Answered);

// Get rid of characters like ? and :, which should not be included in a file name
string fileName = msgInfo.Subject.Replace(”:”, " “).Replace(”?”, " “);

// Save the message in MSG format
MailMessage msg = client.FetchMessage(msgInfo.SequenceNumber);
msg.Save(currentFolder + “\” + fileName + “-” + msgInfo.SequenceNumber + “.msg”, MailMessageSaveType.OutlookMessageFormat);
}
}
Console.WriteLine(”============================\n”);
}
else
{
Console.WriteLine(folderInfo.Name + " is not selectable.”);
}
try
{
// If this folder has sub-folders, call this method recursively to get messages
ImapFolderInfoCollection folderInfoCollection = client.ListFolders(folderInfo.Name);
foreach (ImapFolderInfo subfolderInfo in folderInfoCollection)
{
ListMessagesInFolder(subfolderInfo, rootFolder);
}
}
catch (Exception) { }
}

Please feel free to write us back if you have any further query in this regard.

Hi Kashif,

Thank you for detail code. I had a similar idea as you have demonstrated but this takes too much time as it has to bring all the MessageInfos first and then search through them. A quick forum search revealed that ImapMessageFlags.Recent can also be used to retrieve only new messages. However, I am unable to get the desired output using this with the code below. Can you help if I am missing something here?

ImapQueryBuilder builder = new ImapQueryBuilder();

builder.HasFlags(ImapMessageFlags.Recent);

MailQuery query = builder.GetQuery();

ImapMessageInfoCollection messages = client.ListMessages(query);

Hi Marc,

I have tested this scenario using Aspose.Email for .NET 2.6.0 and it is working at my end. After a little experimentation, I could notice that all the newly arrived messages are reported by the ImapMessageFlgs.Recent. However, if a message is marked as Unread using a client, then the flag doesn’t remain effective and such emails won’t be reported by the API. Please give a try to the following code and let us know your feedback.

ImapClient client = new ImapClient(“[ImapClient.com](http://imapclient.com/)”, 993, “Username”, “password”);

client.EnableSsl = true;

client.Connect(true);

client.Login();

client.SelectFolder(ImapFolderInfo.InBox);

ImapQueryBuilder builder = new ImapQueryBuilder();

builder.HasFlags(ImapMessageFlags.Recent);

MailQuery query = builder.GetQuery();

ImapMessageInfoCollection messages = client.ListMessages(query);

foreach (ImapMessageInfo msgInfo in messages)
{
Console.WriteLine(“Subject:” + msgInfo.Subject);
}