简体   繁体   中英

How can I create group mail alias using office 365 API in C#

How can I create aliases for groups or specific e-mail accounts using the Office 365 API using C#?

I want to dynamically create a group alias name for a group (or specific email) that was already created in Office 365.

In this case, you can consider using the Microsoft Graph - Create Group

First, ensure you have assigned the "Microsoft Graph" > "Read and write all groups" permission to app in Azure AD.

在此处输入图片说明

Code for your reference:

        string authority = "https://login.windows.net/yourdomain.onmicrosoft.com";

        string clientId = "{client_id}";

        Uri redirectUri = new Uri("http://localhost");

        string resourceUrl = "https://graph.microsoft.com";

        HttpClient client = new HttpClient();

        AuthenticationContext authenticationContext = new AuthenticationContext(authority, false);

        AuthenticationResult authenticationResult = authenticationContext.AcquireToken(resourceUrl,
            clientId, redirectUri, PromptBehavior.Always);

        client.DefaultRequestHeaders.Add("Authorization", "Bearer " + authenticationResult.AccessToken);

        string content = @"{
          'displayName': 'mailgrouptest',
          'groupTypes': ['Unified'],
          'mailEnabled': true,
          'mailNickname': 'mailalias1',
          'securityEnabled': false
        }";

        var httpContent = new StringContent(content, Encoding.GetEncoding("utf-8"), "application/json");

        var response = client.PostAsync("https://graph.microsoft.com/v1.0/groups", httpContent).Result;

        Console.WriteLine(response.Content.ReadAsStringAsync().Result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM