【问题标题】:Sending mail using a daemon application使用守护程序应用程序发送邮件
【发布时间】:2020-01-30 11:35:22
【问题描述】:

我无法从守护程序应用发送电子邮件。我可以使用客户端凭据流获取令牌,但无法使用 Microsoft Graph API 发送电子邮件。我收到以下错误:

    Code: BadRequest
    Message: Found a function 'microsoft.graph.sendMail' on an open property. Functions on open properties are not supported.
    Inner error:
    AdditionalData:
       request-id: e2e3bb60-2212-4c99-8858-d109aaf4f1cd
       date: 2020-01-30T11:18:21
    ClientRequestId: e2e3bb60-2212-4c99-8858-d109aaf4f1cd
}

以下是通过 Microsoft Graph 发送电子邮件的代码。

private readonly IClientCredentialProvider _clientCredentialProvider;

        public MailTransmitter()
        {
            AuthenticationConfig config = AuthenticationConfig.ReadFromJsonFile("appsettings.json"); // contains the tenantId, clientSecret and clientId
            _clientCredentialProvider = new ClientCredentialProvider(config);
        }

        public async Task<bool> SendMail(List<UserEntitlement> sortedListByLastAccessDate)
        {
            //GraphServiceClient graphClient = new GraphServiceClient(_clientCredentialProvider.GetAuthorizationCodeProvider());
            var result = await _clientCredentialProvider.GetClientToken(); // Get token using Client Credentials flow
            var accessToken = result.AccessToken;


            //should I pass the URL to the graphServiceClient like below? Is the URL right?
            var graphServiceClient = new GraphServiceClient("https://graph.microsoft.com/v1.0/0a181b4b-a2fb-4e38-b23b-2c72adc882f2/users/c26d8491-82f8-4f08-990e-35a73ad61ede/memberOf", new DelegateAuthenticationProvider(async (requestMessage) => {
                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
            }));

            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 = "Bla@hotmail.com"
                        }
                    }
                },

                From = new Recipient
                {
                    EmailAddress = new EmailAddress { 
                        Address = "bla.bla@test.nl"
                    }
                }
            };

            var saveToSentItems = false;

            //Error occurs here
            await graphServiceClient.Users["c26d8491-82f8-4f08-990e-35a73ad61ede"]
                .SendMail(message, saveToSentItems)
                .Request()
                .PostAsync();

            return true;
        }

如果您想知道我是如何使用客户端凭据流程的,请查看:https://github.com/Azure-Samples/active-directory-dotnetcore-daemon-v2/tree/master/1-Call-MSGraph/daemon-console

到底是什么问题?

提前致谢!

【问题讨论】:

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


    【解决方案1】:

    由于为GraphServiceClient 提供了无效的 url,因此出现提供的错误,它期望第一个参数是 服务根 url

    public GraphServiceClient(string baseUrl,IAuthenticationProvider authenticationProvider,IHttpProvider httpProvider = null)
    

    如果是 Microsoft Graph API,服务根 url 包括:

    • https://graph.microsoft.com 是 Microsoft Graph API 端点。
    • {version} 是目标服务版本,例如 v1.0 或 beta。

    例如,https://graph.microsoft.com/v1.0。详情请参考Calling the Microsoft Graph API

    示例

    var app = ConfidentialClientApplicationBuilder.Create(clientId)
                .WithClientSecret(clientSecret)
                .WithAuthority(new Uri(authority))
                .Build();
    
    var scopes = new string[] {"https://graph.microsoft.com/.default"};
            var result = await app.AcquireTokenForClient(scopes)
                .ExecuteAsync();
    
    var client = new GraphServiceClient(
                    "https://graph.microsoft.com/v1.0",
                    new DelegateAuthenticationProvider(async (requestMessage) =>
                    {
                        requestMessage.Headers.Authorization =
                            new AuthenticationHeaderValue("Bearer", result.AccessToken);
                    }));
    

    或通过Client credentials provider 联系:

    var app = ConfidentialClientApplicationBuilder.Create(clientId)
                .WithClientSecret(clientSecret)
                .WithAuthority(new Uri(authority))
                .Build();
    
    
    
    var authProvider = new ClientCredentialProvider(app);
    var client = new GraphServiceClient(authProvider);
    

    先决条件:需要Microsoft.Graph.Auth package

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-06
      • 2012-02-11
      • 1970-01-01
      • 2015-06-08
      • 2020-12-17
      • 2023-03-30
      • 2011-08-24
      • 1970-01-01
      相关资源
      最近更新 更多