0 votes
by (8.4k points)
edited

I need to set keywords (custom flags) to mail messages on IMAP server.

Can I do it on any IMAP server or how can I find out whether the keywords are supported?

Applies to: Rebex Secure Mail

1 Answer

+1 vote
by (18.0k points)
edited
 
Best answer

Messages on IMAP server can be decorated by one or more flags (e.g. \Seen for marking message read, \Answered for marking message as answered etc.).

There is set of system flags with exactly defined function (defined in RFC 2060) and each IMAP server can (but need not) support some of them. Working with system flags is described in the Message Flags Tutorial.

Some IMAP servers also allow user to define custom flags (also called keywords). If supported, an IMAP client can set arbitary word as a keyword to a mail message on the IMAP server.

Set of flags supported by the IMAP server is available in the ImapFolder.PermanentFlags property. You can retrieve them by following steps:

using Rebex.Net;
...
// create client, connect and log in   
Imap client = new Imap();
client.Connect("hostname");
client.Login("username", "password");

// select working folder
client.SelectFolder("Inbox");

// write all supported flags
Console.WriteLine("Permanend flags: {0}", client.CurrentFolder.PermanentFlags);

// check whether Keywords are supported
if ((client.CurrentFolder.PermanentFlags & ImapMessageFlags.Keywords) == 0)
    Console.WriteLine("Keywords are not supported.");
else
    Console.WriteLine("Keywords are supported.");

If the server supports keywords, you can work with them by following way:

// get current keywords of the first message in the current folder
ImapMessageInfo imapMessage = client.GetMessageInfo(1, ImapListFields.Fast);
Console.WriteLine("Keywords: {0}", string.Join(", ", imapMessage.GetKeywords()));

// set a keyword to the first message in the current folder
client.SetMessageFlags(1, ImapFlagAction.Add, ImapMessageFlags.Keywords, "Keyword1");

// clear a keyword to the first message in the current folder
client.SetMessageFlags(1, ImapFlagAction.Remove, ImapMessageFlags.Keywords, "Keyword1");
...