+1 vote
by (520 points)
edited

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
by (58.9k points)
edited

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();
...