MailKit Imap get read and unread status of a mail
Asked Answered
R

1

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?.

Ranie answered 8/1, 2016 at 12:11 Comment(0)
B
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.

Bactericide answered 9/1, 2016 at 0:58 Comment(8)
Thanks for your help.) But, how do I find whether that message is Seen or UnSeen?Shaeffer
Check Flags for MessageFlags.Seen - if it's not there, then it's Unseen.Bactericide
Fetch() not accepting int as first parameter. and what is 'folder' ?Shaeffer
Sorry, you're right, it takes a list of messages indexes, code has been updated. 'folder' is whatever folder you called GetMessage() on.Bactericide
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?)Shaeffer
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.Shaeffer
In GMail, "Important" is not a flag, it's a label. I've updated the code to show how to get labels.Bactericide
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().Bactericide

© 2022 - 2024 — McMap. All rights reserved.