我正在使用 microsoft graph 發送電子郵件。我想從 Active Directory 中存在的任何電子郵件發送此電子郵件。我已經獲得了 Mail.Send 的權限,并獲得了 Azure 的管理員同意。因此,所有這些都設定在 Azure 級別以獲得訪問和權限。
現在談到代碼。我已經搜索過,但我無法弄清楚如何呼叫 Microsoft graph api 來發送電子郵件。以下是我在搜索時找到的代碼。如何替換以下代碼以將電子郵件從 Azure AD 中的任何人發送給 Azure AD 中的任何人。還有發送電子郵件“Send AS”的代碼。
await graphClient.Me.Messages
.Request()
.AddAsync(message);
uj5u.com熱心網友回復:
目的是登錄用戶不會從他的電子郵件地址發送電子郵件,電子郵件通知將通過其他人的名字發送給某人。
然后我想你想給你的用戶提供一個發送電子郵件,用戶可以選擇誰收到了電子郵件,但是所有的電子郵件都應該是一個特定的帳戶發送的,比如[email protected],那么你應該對發送電子郵件 api 有所了解。
正如@user2250152 提到的await graphClient.Users["userId"],這里userId是發送電子郵件的方式,因為您的要求是從一個特定的電子郵件地址發送所有電子郵件,因此它應該硬編碼為[email protected].
接下來是如何發送電子郵件,呼叫 ms graph api 應該提供一個訪問令牌,因為您的要求是由應用程式而不是每個用戶發送電子郵件,所以我擔心客戶端憑證流是一個更好的選擇,這樣當場景來了sending email from several specific email addresses,你不需要改變流程。現在您需要要求租戶管理員Mail.Send Application在 azure ad 中添加 api 權限才能使用這種流。

這是代碼:
using Azure.Identity;
using Microsoft.Graph;
var mesg = 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
{
//who will receive the email
Address = "[email protected]"
}
}
},
Attachments = new MessageAttachmentsCollectionPage()
};
var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "your_tenant_name.onmicrosoft.com";
var clientId = "azure_ad_app_client_id";
var clientSecret = "client_secret_for_the_azuread_app";
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
await graphClient.Users["user_id_which_you_wanna_used_for_sending_email"].SendMail(mesg, false).Request().PostAsync();
uj5u.com熱心網友回復:
您可以通過這種方式從其他用戶發送郵件。
var message = new Message
{
Subject = "Subject",
Body = new ItemBody
{
ContentType = BodyType.Text,
Content = "Content"
},
ToRecipients = new List<Recipient>()
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = "[email protected]"
}
}
}
};
var saveToSentItems = false;
await graphClient.Users["userId"]
.SendMail(message,saveToSentItems)
.Request()
.PostAsync();
userId是用戶的唯一識別符號。而不是userId你可以使用userPrincipalName. UPN 是基于 Internet 標準 RFC 822 的用戶的 Internet 樣式登錄名。按照慣例,這應該映射到用戶的電子郵件名稱。
資源:
發送郵件
用戶資源
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/419199.html
標籤:
