Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

c# - Unable to send email using Mail.Send Delegated Permissions

I created a C# console application to send email using Microsoft Graph API. On adding Mail.Send Delegated Permission to my application, I see the following exception:

enter image description here

I have enabled 'Allow public client flows':

enter image description here

The application has Mail.Send permission:

enter image description here

Here is my code:

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

        }

What am I missing?

question from:https://stackoverflow.com/questions/65894429/unable-to-send-email-using-mail-send-delegated-permissions

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to meet the following points:

  1. You must have Mail.Send delegation permissions, you can use jwt.ms to parse your access token to view scp claims:

enter image description here

2.Ensure that your account has an Exchange online license under O365 subscription. See: assign licenses to one user.

enter image description here

My code for your reference:

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

print:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...