无法通过具有授权权限的Microsoft Graph API发送电子邮件

用户名

我创建了一个C#控制台应用程序,以使用Microsoft Graph API发送电子邮件。添加Mail.Send应用程序权限后,它可以正常工作。但是,由于公司的要求,我被要求改用Mail.Send Delegated Permission,但在该权限下,我认为它没有用,并且出现此错误:

在此处输入图片说明

添加Mail.Send Delegated Permission后,我应该考虑采取任何措施以使其正常工作吗?

在此处输入图片说明

这是我的代码:

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

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

        var authProvider = new ClientCredentialProvider(confidentialClientApplication);       

        var 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 saveToSentItems = true;

         await _graphClient.Users[<userprincipalname>]
                                .SendMail(message, saveToSentItems)
                                .Request()
                                .PostAsync();

        return graphClient;

    }

更新:

根据以下答案,我将代码更新如下:

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

            var authProvider = new UsernamePasswordProvider(publicClientApplication);

var secureString = new NetworkCredential("", "<password>").SecurePassword;

User me = await graphClient.Me.Request()
                .WithUsernamePassword("<username>", secureString)
                .GetAsync();

我启用了“允许公共客户端流”来修复异常。

在此处输入图片说明

现在,我看到另一个异常:没有足够的权限来完成操作。

我想念什么?

您提供的代码显示您使用客户端凭据流进行身份验证。使用“Mail.Send应用程序”权限时,可以使用客户端凭据流程。但是,如果您使用Mail.Send委派权限,则不能使用客户端凭据。您应该使用用户名/密码流进行身份验证。

================================更新================ ===================

下面是我的代码:

using Microsoft.Graph;
using Microsoft.Graph.Auth;
using Microsoft.Identity.Client;
using System;
using System.Collections.Generic;
using System.Security;

namespace ConsoleApp34
{
    class Program
    {
        static async System.Threading.Tasks.Task Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            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 = "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 = "to email address"
                        }
                    }
                }
            };

            var securePassword = new SecureString();
            foreach (char c in "your password")
                securePassword.AppendChar(c);

            var saveToSentItems = true;

            await graphClient.Me
                    .SendMail(message, saveToSentItems)
                    .Request().WithUsernamePassword("your email", securePassword)
                    .PostAsync();
        }
    }
}

出现错误消息的原因Insufficient privileges to complete the operation是您使用以下代码:

User me = await graphClient.Me.Request()
                .WithUsernamePassword("<username>", secureString)
                .GetAsync();

此代码用于获取用户的信息,但不发送电子邮件,您尚未向该应用添加权限。这样就会显示出来Insufficient privileges to complete the operation请删除此代码,并改用我的代码中的代码块:

await graphClient.Me.SendMail(message, saveToSentItems)
                    .Request().WithUsernamePassword("your email", securePassword)
                    .PostAsync();

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

通过MS Graph API发送电子邮件时包括电子邮件签名

来自分类常见问题

无法使用Microsoft.Graph发送电子邮件

来自分类Dev

无法使用Microsoft.Graph发送电子邮件

来自分类Dev

无法使用带有用户名和密码提供者的委派权限的Microsoft Graph API发送电子邮件

来自分类Dev

Graph Api读取电子邮件的权限

来自分类Dev

通过Google API发送电子邮件

来自分类Dev

Smartsheet通过API发送电子邮件失败

来自分类Dev

Trac API无法发送电子邮件

来自分类Dev

通过Mandrill发送电子邮件

来自分类Dev

通过gmail发送电子邮件

来自分类Dev

通过PHP发送电子邮件

来自分类Dev

JavaMail无法发送电子邮件

来自分类Dev

Gitlab无法发送电子邮件

来自分类Dev

无法从PHP发送电子邮件

来自分类Dev

Gitlab无法发送电子邮件

来自分类Dev

Poco无法发送电子邮件

来自分类Dev

Django无法发送电子邮件

来自分类Dev

我无法使用Java Mail API发送电子邮件,但电子邮件已发送但未收到或未显示在已发送的电子邮件中

来自分类Dev

无法通过python发送和发送电子邮件

来自分类Dev

在Android中使用Java Mail API向带有附件的电子邮件发送电子邮件

来自分类Dev

Ansible邮件模块无法发送电子邮件

来自分类Dev

无法通过Gmail API发送电子邮件给多个发件人

来自分类Dev

无法通过NodeJ使用新的v3 Sendgrid API发送电子邮件

来自分类Dev

通过Microsoft Exchange Server发送电子邮件

来自分类Dev

无法通过SSL通过SMTP发送电子邮件

来自分类Dev

使用仅具有访问令牌的Google API发送电子邮件

来自分类Dev

Microsoft Graph API电子邮件附件

来自分类Dev

无法通过openshift发送电子邮件

来自分类Dev

无法通过gmail发送电子邮件

Related 相关文章

热门标签

归档