无法使用 Graph API 发送电子邮件

时间:2021-05-05 12:46:48

标签: c# asp.net-core microsoft-graph-api

我在尝试从 Graph API 发送电子邮件时遇到问题,

在我的创业课程中,我有以下内容:

services
    .AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"))
    .EnableTokenAcquisitionToCallDownstreamApi(initialScopes)
    .AddMicrosoftGraph(Configuration.GetSection("DownstreamApi"))
    .AddInMemoryTokenCaches();

我有一个用于控制图形访问的类:

public class GraphApiService
{
    private readonly GraphServiceClient _graphServiceClient;

    // Inject Microsfot Graphi API client into GraphApiService
    public GraphApiService(GraphServiceClient graphServiceClient)
    {
        _graphServiceClient = graphServiceClient;
    }

    // Gets the Display Name of the interactive user
    public async Task<string> GetCurrentDisplayName()
    {
        var user = await _graphServiceClient.Me.Request().GetAsync();

        string displayName = user.DisplayName;
        return displayName;
    }
        // Send email from the interactive user
    public async void SendEmailFromInteractiveUser()
    {
        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 = "myemail@email.com"
                    }
                }
            }
        };

        await _graphServiceClient.Me.SendMail(message, null).Request().PostAsync();

    }
}

我能够收到显示名称、姓名、电子邮件等。 任何来自 .Me

但是,我试图从

发送电子邮件
bool saveToSent = true;
await graphServiceClient.Me.SendMail(message, saveToSent).Request().PostAsync();

我在 azure 中拥有所有适当的权限,(user.read user.readbasic.all mail.send)

我错过了什么吗?我被告知要使用

await graphServiceClient.Users[username].SendMail(message, false).Request().PostAsync();

但是我已经能够使用 _graphServiceClient.Me.Request().GetAsync(); 提取基本的用户信息是没有意义的;

请帮忙。

谢谢

1 个答案:

答案 0 :(得分:0)

解决方案:

我忘记将范围更新到我的 appsettings.Development.json

我的所有范围都已正确列出,但它们仅在我的 appsettings.json 文件中,而不在 appsettings.Development.json 中