Download only unread emails from pop-imap and exchange server

Hi Aspose,

One of our system uses Aspose email lib for download emails from various email servers. is there anyway to download only unread emails by pop, imap and mapi protocols?

Hi Nayana,

Thank you for writing to us.

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. With respect to POP3, I’m afraid to share that I couldn’t find any such method and will write back here as soon as I find any information in this regard.

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", "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);
}