MailClient.SetCategories Method


Set a specified email with categories in Office 365 or Exchange Server.

[Visual Basic]
Public Sub SetCategories( _
    info As MailInfo, _
    categories As CategoryCollection _
)

Public Async Function SetCategoriesAsync( _
    info As MailInfo, _
    categories As CategoryCollection _
) As Task
[C#]
public void SetCategories(
    MailInfo info,
    CategoryCollection categories
);

public async Task SetCategoriesAsync(
    MailInfo info,
    CategoryCollection categories
);
[C++]
public: void SetCategories(
    MailInfo^ info,
    CategoryCollection^ categories
);
[JScript]
public function SetCategories(
    info : MailInfo,
    categories: CategoryCollection
);

Parameters

info
The email to set categories.
categories
The categories to set.

Remarks

This method only supports EWS and Graph API protocol.

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++)
{
    client.SetCategories(mailInfos[i], _stringToCategories("Red category, Green category"));
    
    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.GetCategories Method