Free Support Forum - aspose.com

Get all unread emails in an Exchange Server mailbox

Hi


We want to read all unread emails from a mailbox folder in an exchange server, and mark them as read. Now we uses the following code snippet to filter the unread emails, where client is an exchange client (WebDAV or EWS) object.

var msgCollection = client.ListMessages(folderUri);

if (msgCollection == null || !msgCollection.Any()) return;

var unreadMsgCollection = (from msg in msgCollection
where (!msg.IsRead)
select msg).ToList();

This piece of code has a problem: in case that folder has 1000 emails, where 50 are unread, the msgCollection contains all the 1000 msgInfo, which seems to be a waste of memory. ImapClient can filter unread emails using a query like “(‘Seen’ = ‘False’)”, but I did not find similar methods in exchangeClient.

I thought of using

while (some condition)
{
var msgCollection = client.ListMessages(folderUri, 50);
// filter unread emails in msgCollection
}

I am afraid that the msgCollection will always be the same 50 emails since it downloads lastest 50 emails. Then I have no way to read the rest 950 emails unless I move the messages to other folder.

Please let me know whether I am correct. Thanks.

Xu

Hi Xu,


Thanks for writing to Aspose.Email support team.

We are investigating the requirement and request you to spare us little time as we are preparing the new release. As soon as this release is available online, we will provide you code to extract unread messages using new exchange features introduced in incoming release.

Your patience is highly appreciated in this regard.
Hi Xu,

We are sorry for the delayed response.

Please have a look at the following code sample for downloading unread messages from Exchange Server and ImapClient. In both these examples MailQuery class object has been used to retrieve the Unread messages from the server.

Sample Code for EWS:

const string mailboxUri = "https://outlook.office365.com/ews/exchange.asmx";

const string domain = @"";

const string username = @"username@ASE305.onmicrosoft.com";

const string password = @"password";

NetworkCredential credentials = new NetworkCredential(username, password, domain);

IEWSClient client = EWSClient.GetEWSClient(mailboxUri, credentials);

ExchangeQueryBuilder builder1 = new ExchangeQueryBuilder();

builder1.HasNoFlags(ExchangeMessageFlag.IsRead);

MailQuery query = builder1.GetQuery();

ExchangeMessageInfoCollection coll = client.ListMessages(client.MailboxInfo.InboxUri, query, false);

foreach (ExchangeMessageInfo msgInfo in coll)

{

MailMessage msg = client.FetchMessage(msgInfo.UniqueUri);

msg.Save(msg.Subject.Replace(":", "") + ".eml", MailMessageSaveType.EmlFormat);

}


Sample Code for ImapClient:

ImapClient client = new ImapClient("exchange.domain.com", 993, "username", "passwrod");

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);

}

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