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

浪尽此生 提交于 2019-12-13 09:37:21

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/37065503/how-can-i-create-group-mail-alias-using-office-365-api-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!