【问题标题】:Unable to send email via Microsoft Graph API (C# Console)无法通过 Microsoft Graph API(C# 控制台)发送电子邮件
【发布时间】:2021-01-23 23:07:49
【问题描述】:

我按照这 2 个链接创建了一个控制台应用程序,用于使用 Graph API 发送电子邮件:

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

Microsoft Graph API unable to Send Email C# Console

我已在 Azure AD 应用中添加并授予所需权限:

我确保提供了客户端 ID、租户 ID、客户端密码。

但是,我在运行控制台时看到此错误:

我错过了什么?

这是我从Microsoft Graph API unable to Send Email C# Console尝试的代码

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 = "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;

    }

【问题讨论】:

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


    【解决方案1】:

    根据您生成confidentialClientApplication 的代码,您正在使用Client credentials provider

    但是您发送电子邮件的方式是:

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

    其实是调用https://graph.microsoft.com/v1.0/me/sendMail

    但客户端凭据流不支持/me 端点。在这种情况下,您应该调用 https://graph.microsoft.com/v1.0/users/{id | userPrincipalName}/sendMail 端点。

    所以代码应该是:

    await graphClient.Users["{id or userPrincipalName}"]
        .SendMail(message, saveToSentItems)
        .Request()
        .PostAsync();
    

    或者如果你想使用/me/sendMail,选择Authorization code provider,你应该在这里实现交互式登录。

    您可以了解authorization code flowclient credentials flow之间的场景和区别。

    【讨论】:

    • 谢谢!使用 graph.microsoft.com/v1.0/me/sendMail 时,发件人地址是什么?
    • 另外,有没有办法提供一个随机的发件人地址(例如:abc-noreply@microsoft.com)并使用上述图形 api 调用发送电子邮件?
    • @user989988 抱歉耽搁了。前几天我是OOF。如果您想使用/me/sendMail,您将需要使用Authorization code provider。这意味着您将需要使用您的帐户登录。所以发件人地址在这种情况下是登录用户。
    • @user989988 你能解释更多关于随机发件人地址的信息吗?您的意思是您不想使用真实的电子邮件地址?我不认为这是合理的。 O365 需要交换在线许可证才能发送电子邮件。您需要确保发件人是真实的。
    • @user989988 这意味着用户没有 Exchange Online 许可证。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    • 2015-12-29
    相关资源
    最近更新 更多