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

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

Send emails from Azure Ad User - Like noreply

问题

这是我目前拥有的:

  1. string scope = _configuration["AzureAd:Scope"];
  2. string[] scopes = { scope };
  3. var tenantId = _configuration["AzureAd:TenantId"];
  4. var clientId = _configuration["AzureAd:ClientId"];
  5. var clientSecret = _configuration["AzureAd:ClientSecret"];
  6. var options = new TokenCredentialOptions
  7. {
  8. AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
  9. };
  10. var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options);
  11. var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
  12. var requestBody = new SendMailPostRequestBody
  13. {
  14. Message = new Message
  15. {
  16. Subject = "重置密码",
  17. Body = new ItemBody
  18. {
  19. ContentType = BodyType.Html,
  20. Content = rendered
  21. },
  22. ToRecipients = new List<Recipient>
  23. {
  24. new Recipient { EmailAddress = new EmailAddress { Address = messageDto.Email } }
  25. },
  26. From = new Recipient { EmailAddress = new EmailAddress { Address = "noreply@myemailaddress" } }
  27. }
  28. };
  29. 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:

  1. string scope = _configuration[&quot;AzureAd:Scope&quot;];
  2. string[] scopes = { scope };
  3. var tenantId = _configuration[&quot;AzureAd:TenantId&quot;];
  4. var clientId = _configuration[&quot;AzureAd:ClientId&quot;];
  5. var clientSecret = _configuration[&quot;AzureAd:ClientSecret&quot;];
  6. var options = new TokenCredentialOptions
  7. {
  8. AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
  9. };
  10. var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options);
  11. var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
  12. var requestBody = new SendMailPostRequestBody
  13. {
  14. Message = new Message
  15. {
  16. Subject = &quot;Reset Password&quot;,
  17. Body = new ItemBody
  18. {
  19. ContentType = BodyType.Html,
  20. Content = rendered
  21. },
  22. ToRecipients = new List&lt;Recipient&gt;
  23. {
  24. new Recipient { EmailAddress = new EmailAddress { Address = messageDto.Email } }
  25. },
  26. From = new Recipient { EmailAddress = new EmailAddress { Address = &quot;noreply@myemailaddress&quot; } }
  27. }
  28. };
  29. 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

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

  1. using Azure.Identity;
  2. using Microsoft.Graph;
  3. using Microsoft.Graph.Models;
  4. var scopes = new[] { &quot;https://graph.microsoft.com/.default&quot; };
  5. var clientId = &quot;55bc1f7b-3bc1-439e-bddd-4xxxxxxxxxx&quot;;
  6. var tenantId = &quot;2773f7fd-d343-4afe-8230-exxxxxxxx&quot;;
  7. var clientSecret = &quot;xxxxxxxxxxxx&quot;;
  8. var options = new ClientSecretCredentialOptions
  9. {
  10. AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
  11. };
  12. var clientSecretCredential = new ClientSecretCredential(
  13. tenantId, clientId, clientSecret, options);
  14. var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
  15. var requestBody = new Microsoft.Graph.Users.Item.SendMail.SendMailPostRequestBody
  16. {
  17. Message = new Message
  18. {
  19. Subject = &quot;Reset Password&quot;,
  20. Body = new ItemBody
  21. {
  22. ContentType = BodyType.Html,
  23. Content = &quot;Please reset your password soon!&quot;,
  24. },
  25. ToRecipients = new List&lt;Recipient&gt;
  26. {
  27. new Recipient
  28. {
  29. EmailAddress = new EmailAddress
  30. {
  31. Address = &quot;sri@xxxxxxxxx.onmicrosoft.com&quot;,
  32. },
  33. },
  34. },
  35. From = new Recipient { EmailAddress = new EmailAddress { Address = &quot;admin@xxxxxxxx.onmicrosoft.com&quot; } }
  36. },
  37. };
  38. await graphClient.Users[&quot;7488bc42-6a7c-4ba2-a4c3-xxxxxxxx&quot;].SendMail.PostAsync(requestBody);
  39. 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:

  1. using Azure.Identity;
  2. using Microsoft.Graph;
  3. using Microsoft.Graph.Models;
  4. var scopes = new[] { &quot;https://graph.microsoft.com/.default&quot; };
  5. var clientId = &quot;55bc1f7b-3bc1-439e-bddd-4xxxxxxxxxx&quot;;
  6. var tenantId = &quot;2773f7fd-d343-4afe-8230-exxxxxxxx&quot;;
  7. var clientSecret = &quot;xxxxxxxxxxxx&quot;;
  8. var options = new ClientSecretCredentialOptions
  9. {
  10. AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
  11. };
  12. var clientSecretCredential = new ClientSecretCredential(
  13. tenantId, clientId, clientSecret, options);
  14. var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
  15. var requestBody = new Microsoft.Graph.Users.Item.SendMail.SendMailPostRequestBody
  16. {
  17. Message = new Message
  18. {
  19. Subject = &quot;Reset Password&quot;,
  20. Body = new ItemBody
  21. {
  22. ContentType = BodyType.Html,
  23. Content = &quot;Please reset your password soon!&quot;,
  24. },
  25. ToRecipients = new List&lt;Recipient&gt;
  26. {
  27. new Recipient
  28. {
  29. EmailAddress = new EmailAddress
  30. {
  31. Address = &quot;sri@xxxxxxxxx.onmicrosoft.com&quot;,
  32. },
  33. },
  34. },
  35. From = new Recipient { EmailAddress = new EmailAddress { Address = &quot;admin@xxxxxxxx.onmicrosoft.com&quot; } }
  36. },
  37. };
  38. await graphClient.Users[&quot;7488bc42-6a7c-4ba2-a4c3-xxxxxxxx&quot;].SendMail.PostAsync(requestBody);
  39. 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:

确定