我正在使用微软图形发送电子邮件。这封电子邮件,我想从任何电子邮件中存在的活动目录。我已经获得了Mail.Send上的权限,Azure.So上的管理员同意都设置在Azure级别上进行访问和权限。
现在轮到密码了。我已经搜索过了,但我想不出如何调用Microsoft图形api来发送电子邮件。下面是我在搜索时发现的代码。我如何可以取代下面的代码发送电子邮件给任何人,从任何人在Azure AD到任何人在Azure AD。另外,发送电子邮件的代码'Send‘。
await graphClient.Me.Messages
.Request()
.AddAsync(message);发布于 2022-01-20 03:12:00
其意图是用户不会从他的电子邮件地址发送电子邮件,该电子邮件通知将被其他人的名字缓慢地发送给某人。
然后我想你想给你的用户提供一个发送邮件,用户可以选择谁收到了电子邮件,但所有的电子邮件应该是一个特定的帐户,如admin@xxx.onmicrosoft.com,然后你应该了解发送电子邮件api。
正如@user2250152所提到的,await graphClient.Users["userId"],这里的userId意思是发送电子邮件的人,因为您的要求是从一个特定的电子邮件地址发送所有电子邮件,它应该硬编码为admin@xxx.onmicrosoft.com。
接下来是如何发送电子邮件,调用ms应该提供一个访问令牌,因为您的需求是由应用程序发送电子邮件,而不是每个用户发送电子邮件,因此恐怕客户端凭据流是一个更好的选择,因此当场景转到sending email from several specific email addresses时,您就不需要更改流程了。现在,您需要要求您的租户管理员在azure中添加Mail.Send Application 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 = "xxx@gmail.com"
}
}
},
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();发布于 2022-01-19 14:07:50
您可以通过这种方式从其他用户发送邮件。
var message = new Message
{
Subject = "Subject",
Body = new ItemBody
{
ContentType = BodyType.Text,
Content = "Content"
},
ToRecipients = new List<Recipient>()
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = "john.doe@contoso.onmicrosoft.com"
}
}
}
};
var saveToSentItems = false;
await graphClient.Users["userId"]
.SendMail(message,saveToSentItems)
.Request()
.PostAsync();userId是用户的唯一标识符。而不是userId,您可以使用userPrincipalName。UPN是基于因特网标准RFC 822的用户的Internet样式登录名.按照惯例,这应该映射到用户的电子邮件名。
资源:
https://stackoverflow.com/questions/70771954
复制相似问题