8

I am using MailKit to read messages from a gmail account. Works great. But, I want to get the message status as whether its read, unread, important, starred etc. Is this possible with MailKit? I can´t seem to find anything about it.

Here is my code:

 var inbox = client.Inbox;
 var message = inbox.GetMessage(4442);//4442 is the index of a message.

 Console.WriteLine("Message Importance : {0}", message.Importance);
 Console.WriteLine("Message Priority : {0}", message.Priority);

Importance and priority always returns "Normal". How to find this message is marked as important or not? and how to get read or unread status of this message?.

1 Answers1

12

There is no message property because a MimeMessage is just the parsed raw MIME message stream and IMAP does not store those states on the message stream, it stores them separately.

To get the info you want, you'll need to use the Fetch() method:

var info = client.Inbox.Fetch (new [] { 4442 }, MessageSummaryItems.Flags | MessageSummaryItems.GMailLabels);
if (info[0].Flags.Value.HasFlag (MessageFlags.Flagged)) {
    // this message is starred
}
if (info[0].Flags.Value.HasFlag (MessageFlags.Draft)) {
    // this is a draft
}
if (info[0].GMailLabels.Contains ("Important")) {
    // the message is Important
}

Hope that helps.

jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • Thanks for your help.) But, how do I find whether that message is Seen or UnSeen? – siva Jan 09 '16 at 04:11
  • 1
    Check Flags for MessageFlags.Seen - if it's not there, then it's Unseen. – jstedfast Jan 09 '16 at 04:23
  • Fetch() not accepting int as first parameter. and what is 'folder' ? – siva Jan 09 '16 at 04:32
  • Sorry, you're right, it takes a list of messages indexes, code has been updated. 'folder' is whatever folder you called GetMessage() on. – jstedfast Jan 09 '16 at 04:35
  • This is my code : var message = inbox.GetMessage(i); var info = inbox.Fetch(new[] { i }, MessageSummaryItems.Flags); if (info[0].Flags.HasFlag(MessageFlags.Draft)) { // this is a draft } Error CS1061 'MessageFlags?' does not contain a definition for 'HasFlag' and no extension method 'HasFlag' accepting a first argument of type 'MessageFlags?' could be found (are you missing a using directive or an assembly reference?) – siva Jan 09 '16 at 04:39
  • In Gmail we have 'Starred' as well as 'Important' flags. if (info[0].Flags.Value.HasFlag (MessageFlags.Flagged)) { // this message is starred/important } What does this code return? I need both starred and important flags.But now I get only one flag. – siva Jan 09 '16 at 05:03
  • In GMail, "Important" is not a flag, it's a label. I've updated the code to show how to get labels. – jstedfast Jan 09 '16 at 15:13
  • FWIW, every attribute you want to get for a message, you will want to use the Fetch() method to get it. Just browse over the MessageSummaryItems enum to figure out which summary item(s) you want to get and pass it to Fetch(). – jstedfast Jan 09 '16 at 15:24