azure Microsoft.Graph -发送电子邮件返回“ODataErrors.ODataError”

jhiyze9q  于 2023-08-07  发布在  其他
关注(0)|答案(1)|浏览(180)

当通过Microsoft.Graph发送电子邮件时,我收到“ODataErrs.ODataError”错误。我假设我的AD设置中缺少了一些东西,但我检查了所有的罪魁祸首:租户ID、API权限等。
代码如下:

public async Task<string> SendEmail()
    {
        string tenantId = "0XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX3";
        string clientId = "cXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXb";
        string objectId = "4XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXa";

        string clientSecret = "tXXXX~XXXX~XXXXXXXXXXXXXXXXXXXXXXXXXXXXR";

        var options = new TokenCredentialOptions
        {
            AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
        };
        var creds = new ClientSecretCredential(tenantId, clientId, clientSecret, options);

        string[] scopes = new[] { "https://graph.microsoft.com/.default" };
        var graphClient = new GraphServiceClient(creds, scopes );

        var message = new Message()
        {
            Subject = "My subject",
            Body = new ItemBody
            {
                ContentType = BodyType.Html,
                Content = "My content"
            },
            ToRecipients = new List<Recipient>()
            {
                new Recipient
                {
                    EmailAddress = new EmailAddress
                    {
                        Address = "My@Email.com"
                    }
                }
            }
        };

        var body = new SendMailPostRequestBody();
        body.Message = message;
        body.SaveToSentItems = false;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
        try
        {
            await graphClient.Users[objectId].SendMail.PostAsync(body);
        }
        catch (Exception ex)
        {
            return ex.ToString();
        }
        return null;
    }

字符串
在Panagiotis的帮助下,我能够修复一些令人尴尬的错误,但在这里我又被卡住了。

更新:

因此,我现在从解决方案中删除了每个引用和每个NuGet包,并将它们逐一添加。花了几个小时,但我仍然有同样的情况。
如果没有指定TLS1.2,我会得到一个426错误。
在TLS1.2中,我收到“ODataErrors.ODataError”错误。
Visual Studio 2019.所有项目都是.NET Framework 4.7.2。

更新二:

我在Visual Studio 2022下创建了一个针对.NET 7.0的新空白项目。现在我不需要显式指定TLS版本,但仍然得到以下错误:
Microsoft.Graph.Models.ODataErrors.ODataError:引发了类型为“Microsoft.Graph.Models.ODataErrors.ODataError”的异常。在Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.ThrowIfFailedResponse(HttpResponseMessage response,Dictionary 2 errorMapping, Activity activityForAttributes) at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.SendNoContentAsync(RequestInformation requestInfo, Dictionary 2 errorMapping,CancellationToken cancellationToken)在Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.SendNoContentAsync(RequestInformation requestInfo,Dictionary 2 errorMapping, CancellationToken cancellationToken) at Microsoft.Graph.Users.Item.SendMail.SendMailRequestBuilder.PostAsync(SendMailPostRequestBody body, Action 1 requestConfiguration,CancellationToken cancellationToken)在ConsoleApp.GraphHelper.SendEmail()在C:\ConsoleApp\ConsoleApp\GraphHelper.cs:行53

y0u0uwnf

y0u0uwnf1#

注册了一个单租户Azure AD应用,并授予了**Mail.SendAPI权限:
x1c 0d1x的数据
我在Visual Studio 2022下创建了一个针对.NET 6.0的新项目,并运行了下面的代码,包括
try-catch**方法来获取确切的OData错误:

using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;
using Microsoft.Graph.Models.ODataErrors;

var scopes = new[] { "https://graph.microsoft.com/.default" };

var clientId = "appID";
var tenantId = "tenantID";
var clientSecret = "secret";

var options = new ClientSecretCredentialOptions
{
    AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};

var clientSecretCredential = new ClientSecretCredential(
    tenantId, clientId, clientSecret, options);

var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

var requestBody = new Microsoft.Graph.Users.Item.SendMail.SendMailPostRequestBody
{
    Message = new Message
    {
        Subject = "Send mail - Demo purpose",
        Body = new ItemBody
        {
            ContentType = BodyType.Html,
            Content = "Hi! This is Sri",
        },
        ToRecipients = new List<Recipient>
        {
            new Recipient
            {
                EmailAddress = new EmailAddress
                {
                    Address = "sridevixxxxxxxxx@xxxxx.com",
                },
            },
        },
    },
};
try
{
    await graphClient.Users["demouser@xxxxxxxxxx.onmicrosoft.com"].SendMail.PostAsync(requestBody);
    Console.WriteLine("Successfully sent mail");
}
catch (ODataError odataError)
{
    Console.WriteLine(odataError.Error.Code);
    Console.WriteLine(odataError.Error.Message);
}

字符串
在我的情况下,我得到了以下错误,因为demouser没有分配活动的Office 365许可证:



为了解决错误,我尝试从具有活动Office 365许可证的用户发送邮件:



当我通过改变用户再次运行代码时,成功得到响应

using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;
using Microsoft.Graph.Models.ODataErrors;

var scopes = new[] { "https://graph.microsoft.com/.default" };

var clientId = "appID";
var tenantId = "tenantID";
var clientSecret = "secret";

var options = new ClientSecretCredentialOptions
{
    AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};

var clientSecretCredential = new ClientSecretCredential(
    tenantId, clientId, clientSecret, options);

var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

var requestBody = new Microsoft.Graph.Users.Item.SendMail.SendMailPostRequestBody
{
    Message = new Message
    {
        Subject = "Send mail - Demo purpose",
        Body = new ItemBody
        {
            ContentType = BodyType.Html,
            Content = "Hi! This is Sri",
        },
        ToRecipients = new List<Recipient>
        {
            new Recipient
            {
                EmailAddress = new EmailAddress
                {
                    Address = "sridevixxxxxxxxx@xxxxxx.com",
                },
            },
        },
    },
};
try
{
    await graphClient.Users["xxxxx_rk@xxxxxxxxxx.onmicrosoft.com"].SendMail.PostAsync(requestBody);
    Console.WriteLine("Successfully sent mail");
}
catch (ODataError odataError)
{
    Console.WriteLine(odataError.Error.Code);
    Console.WriteLine(odataError.Error.Message);
}

回复:



为了确认这一点,我检查了发送邮件的用户的**Sent items如下:
x1c4d 1x的
在你的例子中,像我提到的那样,在你的代码中包括
try-catch**块,并检查你得到的确切错误。如果你得到的错误与我的不同,请随意评论。

相关问题