如何使用 Microsoft Graph 发送邀请电子邮件 API

How to sent invitation email using Microsoft Graph API

我想在我们的活动 Directory/Tenant 中邀请一位用户。为此,使用 Micorosoft Graph API。代码使用如下

  IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
            .Create(clientId)
            .WithTenantId(tenantID)
            .WithClientSecret(clientSecret)
            .Build();

        ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);

        GraphServiceClient graphClient = new GraphServiceClient(authProvider);

        var invitation = new Invitation
        {
            InvitedUserEmailAddress = "myemailaddress@gmail.com",
            InviteRedirectUrl = "https://myapp.com"
        };

        await graphClient.Invitations
            .Request()
            .AddAsync(invitation);

在此之后,我可以在 Azure 的活动目录门户中看到该用户。但不要收到邀请电子邮件。

但是,当我从 Azure 门户单击 重新发送邀请 时,邀请电子邮件就来了。

能否请教一下,为什么从API发送邀请时没有收到邀请邮件?

您需要在您的 Invitation 对象中将 SendInvitationMessage 设置为 true

var invitation = new Invitation
        {
            InvitedUserEmailAddress = "myemailaddress@gmail.com",
            InviteRedirectUrl = "https://myapp.com",
            SendInvitationMessage = true
        };

有关详细信息,您可以阅读 Docs

sendInvitationMessage Boolean Indicates whether an email should be sent to the user being invited or not. The default is false.