无法使用 Mail.Send 委托权限发送电子邮件

时间:2021-01-26 00:46:09

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

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

enter image description here

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

enter image description here

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

enter image description here

这是我的代码:

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

        }

我错过了什么?

1 个答案:

答案 0 :(得分:0)

您需要满足以下几点:

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

enter image description here

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

enter image description here

我的代码供您参考:

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

打印:

enter image description here

相关问题