Home > Back-end >  how to Assign categories to message uisng mailkit
how to Assign categories to message uisng mailkit

Time:11-02

I am using mailkit to read email message from inbox based on uid (IMAP) which is working fine. However, I do not find an option to set the category to an email using mailkit programmatically.

I do see an option to set custom flags using below code. However this does not seems to be working as we cannot visualize the status of the email's.

var customFlags = new HashSet<string>();
                customFlags.Add("$Testing");
inbox.AddFlags(UniqueId.Parse(uid), MessageFlags.None, customFlags, true);

I am looking for something on how we can set the below category to email message using mailkit only.

Client: Imap , Mail Box: O365

enter image description here

CodePudding user response:

IMAP does not have a concept of categories or styles.

This is up to the user's mail client to synthesize by either setting custom flags on the message(s) (assuming that the IMAP server supports custom flags) or by just storing those attributes somehow locally on the user's desktop (or mobile device).

CodePudding user response:

As Jazb stated, IMAP does not have a concept of "categories" and it is up to the client to find alternative means of storing this state.

I'm not sure whether Office365 supports storing custom keywords (aka custom flags) or not, but what you could do is create a test folder and in that folder add 6 messages, 1 for each category, giving them subjects that would easily clue you in to what category you are assigning each of them (e.g. "Subject: Blue category").

Then use MailKit to Fetch the MessageSummaryItems.Flags | MessageSummaryItems.Envelope data:

var items = folder.Fetch (0, -1, MessageSummaryItems.Flags | MessageSummaryItems.Envelope);
foreach (var item in items) {
    Console.WriteLine ("Office365 uses the {0} keyword to designate the {1}", item.Keywords.FirstOrDefault (), item.Envelope.Subject);
}

You will get output like this:

Office365 uses the $MailLabel1 keyword to designate the Blue category
Office365 uses the $MailLabel2 keyword to designate the Green category
Office365 uses the $MailLabel3 keyword to designate the Orange category
Office365 uses the $MailLabel4 keyword to designate the Purple category
Office365 uses the $MailLabel5 keyword to designate the Red category
Office365 uses the $MailLabel6 keyword to designate the Yellow category

Once you know the real name of the keyword (which may or may not be "$MailLabel1"), then you can set the keywords via:

var keywords = new HashSet<string> () { "$MailLabel1" };
folder.AddFlags (uid, MessageFlags.None, keywords, true);
  • Related