簡體   English   中英

無法使用 Microsoft Graph SDK 發送電子郵件

[英]Unable to send an email with the Microsoft Graph SDK

我按照以下步驟操作:

  1. 我在 Azure 中創建了一個應用注冊。
  2. 進入證書和機密並創建一個新的客戶端機密。
  3. 在 API 權限中,賦予所有 Mail.* 權限名稱的委派權限。
  4. 然后我點擊Grant admin consent for {my org}

在我的代碼(.net core 控制台應用程序)中,我寫了以下內容:

var clientId = "23fff856-***";
var tenantId = "6f541345-***";
var clientSecret = "_Sir**(";

IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
    .Create(clientId)
    .WithTenantId(tenantId)
    .WithClientSecret(clientSecret)
    .Build();

var authProvider = new ClientCredentialProvider(confidentialClientApplication);
var graphClient = new GraphServiceClient(authProvider);

var message = new Message
{
    Subject = "my subject",
    Body = new ItemBody {ContentType = BodyType.Text, Content = "body"},
    ToRecipients = new List<Recipient>
    {
        new Recipient
        {
            EmailAddress = new EmailAddress{Address = "foo@gmail.com"}
        }
    }
};

await GraphClient.Me
                .SendMail(message, true)
                .Request()
                .PostAsync();

這會導致錯誤:

"code":"NoPermissionsInAccessToken",
"message":"The token contains no permissions, or permissions can not be understood."

我已經給了所有權限。 我錯過了什么?

由於您使用的是客戶端憑據流,因此您需要在 Azure 門戶上使用應用程序權限。 請添加 Mail.Send 應用程序權限,它會起作用。

刪除 'Me' 並添加 Users['Userid'],因為 'Me' 談論的是登錄用戶,而我們這里沒有任何用戶。

Userid 是 Azure AD 中的用戶對象 ID。 您還可以使用 userPrincipalname 進行測試。 您可以通過使用/users 端點並使用 displayname 或 userPrincipalname 進行過濾來獲取用戶的用戶 ID。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM