As far as I know categories are not available through IMAP in Exchange/Office365.
You can only set Flagged
flag easily:
client.FlagMessageByUID(uid, Flag.Flagged);
Keywords are however added as a Keyword
header to the message it self (Keywords: Green category,Orange category
)
So they are easy to obtain (using IMail.Headers["Keywords"]
), however are harder to update/modify.
Following code downloads an email modifies it by adding Keyword
e-mail header and uploads it, deleting the original message:
using (Imap client = new Imap())
{
client.ConnectSSL(server);
client.UseBestLogin(user, token);
client.SelectInbox();
long uid = client.GetAll()[0];
MessageInfo info = client.GetMessageInfoByUID(uid);
byte[] eml = client.GetMessageByUID(uid);
IMail email = new MailBuilder().CreateFromEml(eml);
email.Headers["Keywords"]
= "Green category,Orange category";
client.UploadMessage(email, new UploadMessageInfo{
Flags = info.Flags,
InternalDate = info.Envelope.InternalDateString});
// Delete original message
client.DeleteMessageByUID(uid);
client.Close();
}