c# - Microsoft Graph API 无法发送电子邮件 C# 控制台

标签 c# azure azure-active-directory azure-ad-graph-api microsoft-graph-mail

我创建了一个小型控制台应用p来使用Microsoft Graph API发送电子邮件。

使用教程

https://learn.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0&tabs=csharp

错误

ServiceException: Code: NoPermissionsInAccessToken Message: The token contains no permissions, or permissions can not be understood.

代码

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

namespace GraphAPI
 {
    class Program
    {

    static void Main(string[] args)
    {
        // Azure AD APP
        string clientId = "<client Key Here>";
        string tenantID = "<tenant key here>";
        string clientSecret = "<client secret here>";

        Task<GraphServiceClient> callTask = Task.Run(() => SendEmail(clientId, tenantID, clientSecret));
        // Wait for it to finish
        callTask.Wait();
        // Get the result
        var astr = callTask;
    }

    public static async Task<GraphServiceClient> SendEmail(string clientId, string tenantID, string clientSecret)
    {

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

        ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);       

        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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3954406d567c54585055795e54585055175a5654" rel="noreferrer noopener nofollow">[email protected]</a>"
                    }
                }
            },
            CcRecipients = new List<Recipient>()
            {
                new Recipient
                {
                    EmailAddress = new EmailAddress
                    {
                        Address = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7914003a3a3c14181015391e14181015571a1614" rel="noreferrer noopener nofollow">[email protected]</a>"
                    }
                }
            }
        };

        var saveToSentItems = true;

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

        return graphClient;

    }

}

}

这是我授予 AD APP 的权限屏幕截图

enter image description here

那么,有人可以指导我哪里出错了

最佳答案

根据您的屏幕截图,您尚未授予管理员对 Mail.Send 应用程序权限的同意。

点击 API 权限下的授予管理员同意按钮。

enter image description here

更新:

交互式提供商:

            string[] scopes = { "Mail.Send" };
            string clientId = "";
            IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
            .Create(clientId)
            .WithRedirectUri("https://localhost")
            .Build();

            InteractiveAuthenticationProvider authProvider = new InteractiveAuthenticationProvider(publicClientApplication, scopes);

            GraphServiceClient graphClient = new GraphServiceClient(authProvider);

关于c# - Microsoft Graph API 无法发送电子邮件 C# 控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57411274/

相关文章:

azure - 从开发迁移到云时从一个连接字符串切换到另一个连接字符串

azure - 如何在 Visual Studio 2015 中获取最新的 "Add Azure Webjob Dialog"?

azure-active-directory - Azure AD 自动添加了 offline_access

c# - OCR TesseractEngine

c# - EmguCV 在查询高分辨率帧时性能非常差

azure - 将 SQL Azure 数据库导出到 blob - Start-AzureSqlDatabaseExport : Cannot convert AzureStorageContainer to AzureStorageContainer

azure-active-directory - Microsoft Graph API 用于获取用户拥有的 AAD 组列表

Azure AD - 区分应用程序 token 和用户 token

C#构造函数问题

c# - EF Core Azure Cosmos DB 提供程序与 Cosmos LINQ to SQL 转换