简体   繁体   English

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

[英]Microsoft Graph API unable to Send Email C# Console

I have created a small Console Ap p to send email using Microsoft Graph API . 我创建了一个小型的Console Ap p来使用Microsoft Graph API发送电子邮件。

Tutorial Used 使用的教程

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

Error 错误

ServiceException: Code: NoPermissionsInAccessToken Message: The token contains no permissions, or permissions can not be understood. ServiceException:代码:NoPermissionsInAccessToken消息:令牌不包含权限,或者无法理解权限。

Code

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 = "myToEmail@gmail.com"
                    }
                }
            },
            CcRecipients = new List<Recipient>()
            {
                new Recipient
                {
                    EmailAddress = new EmailAddress
                    {
                        Address = "myCCEmail@gmail.com"
                    }
                }
            }
        };

        var saveToSentItems = true;

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

        return graphClient;

    }

}

}

Here is the Screenshot of permissions I gave to the AD APP 这是我给AD APP 的权限屏幕截图

在此输入图像描述

So, Can anybody guide me where I am going wrong 所以,任何人都可以指导我出错的地方

Based on your screenshot, you haven't grant admin consent to Mail.Send application permission. 根据您的屏幕截图,您尚未授予Mail.Send应用程序权限的管理员同意权。

Click the grant admin consent button under api permissions. 单击api权限下的grant admin consent按钮。

在此输入图像描述

Update: 更新:

Interactive provider: 互动提供者:

            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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法通过 Microsoft Graph API(C# 控制台)发送电子邮件 - Unable to send email via Microsoft Graph API (C# Console) Microsoft Graph API来获取Outlook邮件,C#控制台应用程序 - Microsoft Graph API to fetch Outlook Mails, C# Console Application "在 c# 中使用 Microsoft Graph API 获取所有电子邮件" - Get all email message using Microsoft Graph API in c# 无法使用 Microsoft.Graph 发送电子邮件 - Unable to send email using Microsoft.Graph 无法使用 Microsoft Graph SDK 发送电子邮件 - Unable to send an email with the Microsoft Graph SDK 无法在 C# 中发送 email - unable to send email in C# 如何通过 microsoft graph API C# 向 Microsoft Teams 中的 1:1 聊天或群聊发送消息 - How to send message to 1:1 chat or to group chat in Microsoft Teams via microsoft graph API C# 无法解决 C# Active Directory 控制台应用程序中的 Nuget 包 Microsoft.Graph.Auth 问题 - Unable to resolve Nuget package Microsoft.Graph.Auth issue in C# Active Directory console app C# Microsoft Graph - 如何使用来自 msal 浏览器的访问令牌发送 email - C# Microsoft Graph - How to send email with access token from msal-browser 尝试使用 Microsoft Graph 从公司代理和防火墙后面使用 C# 发送 email - Trying to send email using Microsoft Graph using C# from behind Corporate proxy and firewall
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM