电子邮件无法通过Microsoft Graph

时间:2020-09-21 18:29:20

标签: c# microsoft-graph-api microsoft-graph-sdks microsoft-graph-mail

我正在建立一个从我的应用程序发送电子邮件的过程。我注册了Azure中的Office 365组织来进行测试。在我的App注册中,我授予了以下权限。已获得这些权限的管理员同意。

Configured permissions

接下来,我编写了这段代码,该代码基于github项目。

        using Microsoft.Graph;
    using Microsoft.Graph.Auth;
    using Microsoft.Identity.Client;
    using System;
    using System.Collections.Generic;
    using System.Web.UI;

namespace DemoOutlookMail
{
    public partial class _Default : Page
    {
        protected void send_Click(object sender, System.EventArgs e)
        {

            const string tenantId = "APP REGISTRATION TENANT ID";
            const string redirectUri = "https://localhost:44316/";
            const string clientSecret = "APP REGISTRATION SECRET";
            const string clientId = "APP REGISTRATION CLIENT ID";

            const string AuthorityFormat = "https://login.microsoftonline.com/{0}/v2.0";
            IConfidentialClientApplication daemonClient;
            daemonClient = ConfidentialClientApplicationBuilder.Create(clientId)
                .WithAuthority(string.Format(AuthorityFormat, tenantId))
                .WithRedirectUri(redirectUri)
                .WithClientSecret(clientSecret)
                .Build();

            ClientCredentialProvider authProvider = new ClientCredentialProvider(daemonClient,"Mail.Send");

            GraphServiceClient graphClient = new GraphServiceClient(authProvider);
            SendEmail(graphClient);
        }

        private void SendEmail(GraphServiceClient graphClient)
        {
            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 = "AdeleV@testariesllc.onmicrosoft.com"
                            }
                        }
            },
                CcRecipients = new List<Recipient>()
                {
                    new Recipient
                    {
                        EmailAddress = new EmailAddress
                        {
                            Address = "AlexW@testariesllc.onmicrosoft.com"
                        }
                    }
                }
            };

            var saveToSentItems = false;
            try
            {
                graphClient.Me.SendMail(message, saveToSentItems).Request().PostAsync();
            }
            catch(Exception e)
            {
                Label1.Text = e.Message;
            }
        }

    }
}

此代码运行,但不发送任何电子邮件,不引发任何异常。 我想念什么? 原始的github项目是https://github.com/Azure-Samples/ms-identity-aspnet-daemon-webapp

1 个答案:

答案 0 :(得分:0)

因为您具有“应用程序”权限,所以支持/ me端点

调用/ me端点需要登录用户,因此需要委派权限。 使用/ me端点时,不支持应用程序权限。 https://docs.microsoft.com/en-us/graph/api/user-get?view=graph-rest-1.0&tabs=http

因此,在您的代码中,您需要指定要发送的用户,因为这应该很容易解决,例如

 graphClient.Users["user@domain.com"].SendMail(message, saveToSentItems).Request().PostAsync();