【问题标题】:Unable to send email using Mail.Send Delegated Permissions无法使用 Mail.Send Delegated Permissions 发送电子邮件
【发布时间】:2021-01-27 04:10:21
【问题描述】:

我创建了一个 C# 控制台应用程序来使用 Microsoft Graph API 发送电子邮件。在向我的应用程序添加 Mail.Send Delegated Permission 时,我看到以下异常:

我已启用“允许公共客户流”:

应用程序具有 Mail.Send 权限:

这是我的代码:

        public async Task SendMail(string subject, string content, string recipientAddress)
        {

            var publicClientApplication = PublicClientApplicationBuilder
            .Create("<client id>")
            .WithTenantId("<tenant id>")
            .Build();

            string[] scopes = new string[] { "mail.send" };

            UsernamePasswordProvider authProvider = new UsernamePasswordProvider(publicClientApplication, scopes);

            GraphServiceClient graphClient = new GraphServiceClient(authProvider);

            var message = new Message
            {
                Subject = subject,
                Body = new ItemBody
                {
                    ContentType = BodyType.Text,
                    Content = content
                },
                ToRecipients = new List<Recipient>()
                {
                    new Recipient
                    {
                        EmailAddress = new EmailAddress { Address = recipientAddress }
                    }
                }
            };                       

            var securePassword = new SecureString();
            foreach (char c in _senderPassword)
                securePassword.AppendChar(c);

            var saveToSentItems = true;

            await graphClient.Me
                    .SendMail(message, saveToSentItems)
                    .Request().WithUsernamePassword(_senderAddress, securePassword)
                    .PostAsync();

        }

我错过了什么?

【问题讨论】:

  • 请检查您的访问令牌是否在jwt.ms 上具有权限。
  • 是的,按照 shiva 的建议,解析您的令牌并提供屏幕截图。
  • 您能告诉我如何检查吗?
  • 从日志中我发现产生的错误消息不是因为 sendMail API,而是看起来你在代码中点击 /groups/{groupid}。如果您想获取群组详细信息,您需要添加 Group.Read.All 或 Directory.Read.All。
  • 我添加了 Group.Read.All 和 Directory.Read.All 权限。我仍然看到同样的错误。

标签: c# azure-active-directory microsoft-graph-api


【解决方案1】:

您需要满足以下几点:

  1. 您必须拥有Mail.Send委托权限,您可以使用jwt.ms解析您的访问令牌以查看scp声明:

2.确保您的帐户具有 O365 订阅下的 Exchange 在线许可证。见:assign licenses to one user

我的代码供你参考:

using Microsoft.Graph;
using Microsoft.Graph.Auth;
using Microsoft.Identity.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace devicecode
{
    class Program
    {
        static async Task Main(string[] args)
        {

                string graphScope = "User.Read User.ReadBasic.All Mail.Send Mail.Send.Shared";
                var graphScopes = graphScope.Split(' ').ToArray();

                // Build a client application.
                IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
                            .Create("My clienid")
                            .Build();

            DeviceCodeProvider authProvider = new DeviceCodeProvider(publicClientApplication, graphScopes);
            // Create an authentication provider by passing in a client application and graph scopes.
                // Create a new instance of GraphServiceClient with the authentication provider.
                GraphServiceClient graphClient = new GraphServiceClient(authProvider);

                var message = new Message
                {
                    Subject = "Meet for lunch?",
                    Body = new ItemBody
                    {
                        ContentType = BodyType.Text,
                        Content = "The new cafeteria is open."
                    },
                    ToRecipients = new List<Recipient>()
                {
                    new Recipient
                    {
                        EmailAddress = new EmailAddress
                        {
                            Address = "mytestaccount"
                        }
                    }
                }
                };

                var saveToSentItems = false;

                await graphClient.Me
                    .SendMail(message, saveToSentItems)
                    .Request()
                    .PostAsync();
            }
        }
    }

打印:

【讨论】:

  • 谢谢!您能否告诉我您在代码中的哪个位置生成令牌?
  • 请查看我的更新代码 - 我正在使用 UsernamePasswordProvider authProvider = new UsernamePasswordProvider(publicClientApplication, scopes);
猜你喜欢
  • 2015-06-11
  • 2015-09-06
  • 2021-04-13
  • 2021-09-12
  • 2013-03-30
  • 2017-02-15
  • 2014-06-20
  • 2013-06-15
相关资源
最近更新 更多