如何在C#中使用Office 365 API创建组邮件别名 [英] How can I create group mail alias using office 365 API in C#

查看:164
本文介绍了如何在C#中使用Office 365 API创建组邮件别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Office 365 API和C#为组或特定的电子邮件帐户创建别名?

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

我想为Office 365中已经创建的组(或特定电子邮件)动态创建组别名.

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

推荐答案

在这种情况下,您可以考虑使用Microsoft Graph-

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

首先,确保已为Azure AD中的应用程序分配了"Microsoft Graph">读取和写入所有组"权限.

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

代码供您参考:

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

这篇关于如何在C#中使用Office 365 API创建组邮件别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆