How to obtain ImapFolder's "IsSubscribed" state?

+1 vote
asked Feb 7, 2014 by SeToY (520 points)
edited Feb 10, 2014

Hello,

I'm trying to implement a simple "Subscribe" / "Unsubscribe" functionality for IMAP Folders. Unfortunately I haven't found a way to determine if an ImapFolder-Instance is currently subscribed to or not.

How can I get this state?

Thank you!

Applies to: Rebex Secure Mail

1 Answer

0 votes
answered Feb 10, 2014 by Tomas Knopp (58,890 points)
edited Feb 10, 2014

Hello,

here is a sample code which determines whether the folder is Subscribed or not using the Imap.GetFolderList method together with the ImapFolderListMode.Subscribed mode:

private bool IsSubscribed(Imap client, string folder)
{
    if (!client.IsConnected || !client.IsAuthenticated)
        return false;

    // get the subfolders which are subscribed 
    var subscribedFolders = client.GetFolderList("", ImapFolderListMode.Subscribed);

    // you might want to use GetFolderList("", ImapFolderListMode.Subscribed, true)
    // to also return subfolders of subfolders

    foreach (var subscribed in subscribedFolders)
    {
        // the folder was found
        if (subscribed.Name == folder)
            return true;
    }

    return false;
}

Imap client = new Imap();

client.Connect("exchange");
client.Login("user", "password");

client.SelectFolder("INBOX");

client.Subscribe("subfolder");
Console.WriteLine(IsSubscribed(client, "subfolder"));

client.Unsubscribe("subfolder");    
Console.WriteLine(IsSubscribed(client, "subfolder"));

client.Disconnect();
...