MailClient.GetCategories Method


Receives a specified email's categories from Office 365 or Exchange Server.

[Visual Basic]
Public Function GetCategories( _
    info As MailInfo
) As CategoryCollection

Public Async Function GetCategoriesAsync( _
    info As MailInfo
) As Task (Of CategoryCollection)
[C#]
public CategoryCollection GetCategories(
    MailInfo info
);

public async Task<CategoryCollection> GetCategoriesAsync(
    MailInfo info
);
[C++]
public: CategoryCollection^ GetCategories(
    MailInfo^ info
);
[JScript]
public function GetCategories( 
    info: MailInfo
) : CategoryCollection;

Parameters

info
The MailInfo instance to retrieve the categories.

Return Value

A CategoryCollection object.

Remarks

This method retrieves email's categories, it only supports EWS and Graph API protocol. If you have set GetMailInfosParam.GetMailInfosOptions.GetCategories, you can get the categories from MailInfo.Categories property directly after calling GetMailInfos method.

Example

[C#]
var client = new MailClient("TryIt");
// you must use EWS or Graph API to connect the server
client.Connect(server);

// retrieve categories and follow up flag to MailInfo
client.GetMailInfosParam.GetMailInfosOptions |= GetMailInfosOptionType.GetCategories;
client.GetMailInfosParam.GetMailInfosOptions |= GetMailInfosOptionType.GetFollowUpFlag;
                
var mailInfos = client.GetMailInfos();
Console.WriteLine("Total {0} emails", mailInfos.Length);
for (int i = 0; i < mailInfos.Length; i++)
{
    // if you have set GetMailInfosOptionType.GetCategories flag, you don't need to call this method
    client.GetCategories(mailInfos[i]);
    
    Console.WriteLine(string.Format("Index: {0}", mailInfos[i].Index));
    Console.WriteLine(string.Format("Categories: {0}", _categoriesToString(mailInfos[i].Categories)));
}

static CategoryCollection _stringToCategories(string input)
{
    var categories = new CategoryCollection();
    string[] values = input.Split(',');
    for(int i = 0; i < values.Length; i++)
    {
        string value = values[i].Trim();
        if(!string.IsNullOrEmpty(value))
        {
            categories.Add(new Category(value));
        }
    }
    return categories;
}

static string _categoriesToString(CategoryCollection categories)
{
    if (categories == null || categories.Count == 0)
        return "None";

    StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < categories.Count; i++)
    {
        buffer.Append(categories[i].Name);
        if (i < categories.Count - 1)
            buffer.Append(",");
    }
    return buffer.ToString();
}

See Also

MailClient.GetMail Method
Mail Class