从Azure Ad用户发送电子邮件 – 类似noreply

huangapple go评论92阅读模式
英文:

Send emails from Azure Ad User - Like noreply

问题

这是我目前拥有的:

string scope = _configuration["AzureAd:Scope"];
string[] scopes = { scope };
var tenantId = _configuration["AzureAd:TenantId"];
var clientId = _configuration["AzureAd:ClientId"];
var clientSecret = _configuration["AzureAd:ClientSecret"];
var options = new TokenCredentialOptions
{
    AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};

var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options);

var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

var requestBody = new SendMailPostRequestBody
{
    Message = new Message
    {
        Subject = "重置密码",
        Body = new ItemBody
        {
            ContentType = BodyType.Html,
            Content = rendered
        },
        ToRecipients = new List<Recipient>
        {
            new Recipient { EmailAddress = new EmailAddress { Address = messageDto.Email } }
        },
        From = new Recipient { EmailAddress = new EmailAddress { Address = "noreply@myemailaddress" } }
    }
};

await graphClient.Users["ObjectId"].SendMail.PostAsync(requestBody);

这给我返回了一个 403 ODataError。我正在使用 /.default 作用域,并且在我的应用程序中启用了 Mail.Send 作用域,并为委托权限和应用程序权限都进行了同意。有人遇到过类似的情况吗?

英文:

I have an AzureAd application and a user called noreply so I can send automated emails from my account for like password resets etc with Microsoft Graph.

Here is what I have so far:

string scope = _configuration[&quot;AzureAd:Scope&quot;];
string[] scopes = { scope };
var tenantId = _configuration[&quot;AzureAd:TenantId&quot;];
var clientId = _configuration[&quot;AzureAd:ClientId&quot;];
var clientSecret = _configuration[&quot;AzureAd:ClientSecret&quot;];
var options = new TokenCredentialOptions
{
      AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};

var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options);

var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

var requestBody = new SendMailPostRequestBody
                {
                    Message = new Message
                    {
                        Subject = &quot;Reset Password&quot;,
                        Body = new ItemBody
                        {
                            ContentType = BodyType.Html,
                            Content = rendered
                        },
                        ToRecipients = new List&lt;Recipient&gt;
                        {
                            new Recipient { EmailAddress = new EmailAddress { Address = messageDto.Email } }
                        },
                        From = new Recipient { EmailAddress = new EmailAddress { Address = &quot;noreply@myemailaddress&quot; } }
                    }
                };

                await graphClient.Users[&quot;ObjectId&quot;].SendMail.PostAsync(requestBody);

This is giving me a 403 ODataError. I am using the /.default scope and in my app I have the Mail.Send scope enabled and consented for both Delegated permission and application permissions.

Anyone had to do anything similar?

答案1

得分: 1

请注意,您的代码中使用了客户端凭据流程,仅适用于**Application**权限。

我注册了一个Azure AD应用程序,并通过授予同意方式添加了**Mail.Send**应用程序权限,如下所示:

从Azure Ad用户发送电子邮件 – 类似noreply

当我在我的环境中运行下面的修改后的代码时,我成功地收到了以下响应

using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;

var scopes = new[] { &quot;https://graph.microsoft.com/.default&quot; };

var clientId = &quot;55bc1f7b-3bc1-439e-bddd-4xxxxxxxxxx&quot;;
var tenantId = &quot;2773f7fd-d343-4afe-8230-exxxxxxxx&quot;;
var clientSecret = &quot;xxxxxxxxxxxx&quot;;

var options = new ClientSecretCredentialOptions
{
    AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};

var clientSecretCredential = new ClientSecretCredential(
    tenantId, clientId, clientSecret, options);

var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

var requestBody = new Microsoft.Graph.Users.Item.SendMail.SendMailPostRequestBody
{
    Message = new Message
    {
        Subject = &quot;Reset Password&quot;,
        Body = new ItemBody
        {
            ContentType = BodyType.Html,
            Content = &quot;Please reset your password soon!&quot;,
        },
        ToRecipients = new List&lt;Recipient&gt;
        {
            new Recipient
            {
                EmailAddress = new EmailAddress
                {
                    Address = &quot;sri@xxxxxxxxx.onmicrosoft.com&quot;,
                },
            },
        },
        From = new Recipient { EmailAddress = new EmailAddress { Address = &quot;admin@xxxxxxxx.onmicrosoft.com&quot; } }
    },
};
await graphClient.Users[&quot;7488bc42-6a7c-4ba2-a4c3-xxxxxxxx&quot;].SendMail.PostAsync(requestBody);

Console.WriteLine(&quot;Mail sent successfully!&quot;);

响应:

从Azure Ad用户发送电子邮件 – 类似noreply

当我检查邮箱时,邮件已成功发送,如下所示:

从Azure Ad用户发送电子邮件 – 类似noreply

在您的情况下,请确保仅授予应用程序权限,并尝试按照我提到的方式修改您的代码。

英文:

> Note that, you are using client credentials flow in your code that
> only works with Application permissions.

I registered one Azure AD application and added Mail.Send Application permission by granting consent like this:

从Azure Ad用户发送电子邮件 – 类似noreply

When I ran below modified code in my environment, I got response successfully like this:

using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;

var scopes = new[] { &quot;https://graph.microsoft.com/.default&quot; };

var clientId = &quot;55bc1f7b-3bc1-439e-bddd-4xxxxxxxxxx&quot;;
var tenantId = &quot;2773f7fd-d343-4afe-8230-exxxxxxxx&quot;;
var clientSecret = &quot;xxxxxxxxxxxx&quot;;

var options = new ClientSecretCredentialOptions
{
    AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};

var clientSecretCredential = new ClientSecretCredential(
    tenantId, clientId, clientSecret, options);

var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

var requestBody = new Microsoft.Graph.Users.Item.SendMail.SendMailPostRequestBody
{
    Message = new Message
    {
        Subject = &quot;Reset Password&quot;,
        Body = new ItemBody
        {
            ContentType = BodyType.Html,
            Content = &quot;Please reset your password soon!&quot;,
        },
        ToRecipients = new List&lt;Recipient&gt;
        {
            new Recipient
            {
                EmailAddress = new EmailAddress
                {
                    Address = &quot;sri@xxxxxxxxx.onmicrosoft.com&quot;,
                },
            },
        },
        From = new Recipient { EmailAddress = new EmailAddress { Address = &quot;admin@xxxxxxxx.onmicrosoft.com&quot; } }
    },
};
await graphClient.Users[&quot;7488bc42-6a7c-4ba2-a4c3-xxxxxxxx&quot;].SendMail.PostAsync(requestBody);

Console.WriteLine(&quot;Mail sent successfully!&quot;);

Response:

从Azure Ad用户发送电子邮件 – 类似noreply

When I checked the mailbox, mail sent successfully like below:

从Azure Ad用户发送电子邮件 – 类似noreply

In your case, make sure to grant only Application permissions and try modifying your code as I mentioned.

huangapple
  • 本文由 发表于 2023年7月3日 22:36:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605769.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定