Get unread mail+attachment in inbox / maybe it's not a recent mail

Hello,

I’d like to know how can I get the unread emails and its attachments (from inbox).

Maybe the unread message is not recently sent and I’d like to get all of them.

The technology, isn’t important for me but I prefer to use Imap, because I don’t like to download emails locally.

And at the end, if I read the unread messages, is it automatically change to read email? If no, how can I change its state?

Thanks
Alireza

Hi Alireza,

Thank you for posting your inquiry.

You can use the API’s ImapClient to filter messages from inbox based on different flags. This also has the IsRead flag which shows if a message has already been read or not, and can serve your purpose. Message is changed to read mail once it is fetched from the server. Please try the following code sample at your end and let us know if we can be of any additional help to you in this regard.

Sample Code

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

ImapQueryBuilder builder = new ImapQueryBuilder();

builder.HasNoFlags(ImapMessageFlags.IsRead);

MailQuery query = builder.GetQuery();

ImapMessageInfoCollection coll = client.ListMessages(query);

Console.WriteLine(coll.Count);

foreach (ImapMessageInfo msgInfo in coll)
    client.FetchMessage(msgInfo.UniqueId);  //This will mark the message as Read

Thanks
Kashif Iqbal

1 Like

Perfect answer!!! That’s exactly what I need. Thanks a lot. :ok_hand:

But I still have a question on attachments. How can I download them localy?

Thanks again! :slight_smile:

@alireza.tadbir,

The FetchMessage command returns MailMessage object that will contain all the contents of the email message including its attachments. You can use MailMessage.Save to save the message to disc which will also save the attachments as part of email. You need to add following line of code to above for saving fetched message.

Sample Code

MailMessage eml = client.FetchMessage(msgInfo.UniqueId);

eml.Save("saved.eml");   //saves in EML format

eml.Save("saved.msg", SaveOptions.DefaultMsgUnicode);  //saves in Outlook MSG format

Thanks,
Kashif

1 Like

@kashif.iqbal

You’re wonderful. It works like a charm! Just some last questions:

• Does ASPOE library have a system to extract attachment file from ‘.eml’ or ‘.msg’ files?

• Is MessageId unique between all mails? If no, is there an unique id?

@alireza.tadbir,

  1. Yes, you can extract attachments from EML or MSG files once you load these in MailMessage. Please have a look at our documentation section, working with email attachments, which shows how to extract attachments from MailMessage and save to disc.

  2. The MessageId remains unique between all mails.

Thanks,
Kashif

1 Like

Thanks a lot for your support and I confirme that I have no more question :smile: (for now)

@alireza.tadbir,

You are welcome and please feel free to write to us in case of any further inquiry related to the API.

Kashif

1 Like