从服务帐户使用 Graph API 发送电子邮件
我正在 ASP.NET Core 5 (C#) 中执行任务,需要使用 Graph API 发送电子邮件,我参考了以下文章并在 Azure 试用帐户上进行了配置,并且能够发送电子邮件。
使用 .NET 通过 Microsoft Graph 发送电子邮件
这是发送电子邮件代码:
//send email
var client = await GetAuthenticatedGraphClient();
await client.Users[senderObjectId]
.SendMail(graphMailMessage, true)
.Request()
.PostAsync();
senderObjectId
- 来自配置的对象 ID
我们在客户端的 Azure 帐户上部署了相同的代码,我们需要我们将用作的服务帐户的用户对象 ID发件人的电子邮件 ID。然而,客户回来说该帐户不是 Azure AD 的一部分,而是一个服务帐户。有没有一种方法可以在不使用用户对象 ID 的情况下发送电子邮件。
I am working on task in ASP.NET Core 5 (C#) which requires to send an email using Graph API, I have referred to following article and did the configuration on the Azure trial account and was able to send the emails.
Sending e-mails with Microsoft Graph using .NET
This is the send email code:
//send email
var client = await GetAuthenticatedGraphClient();
await client.Users[senderObjectId]
.SendMail(graphMailMessage, true)
.Request()
.PostAsync();
senderObjectId
- Object Id coming from config
We deployed the same code on the client's Azure account we needed the User Object Id for the service account that we are going to use as a sender's email id. However, the client came back saying that the account is not part of the Azure AD and its a service account. Is there a way of sending emails without using the user object id.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是采用邮件发送参数的方法。另外,它将邮件分开(逗号)并将其发送给多个用户
Here's the method which takes parameters for mail sending. Also, it separates (comma) the mail and sends it to multiple users
您只需在下面的 SendEmail 函数中将 clientId、tenantId、clientSecret 更改为您的 ID。确保安装了 Microsoft.Graph 和 Azure.Identity nuget 包。
您需要在该函数之前添加以下代码:
如何使用:
您可以发送给多个人并附加多个文件,只需确保每个字符串之间用分号“;”分隔即可。它接受除 emFrom 和 emTo 变量之外的空字符串参数。
You just need to change clientId, tenantId, clientSecret to your IDs inside the SendEmail function below. Make sure to have Microsoft.Graph and Azure.Identity nuget packages installed.
You need the following code preceding the function:
How to use:
You can send to multiple people and attach multiple files, just make sure to separate each string with semi colon ";". It accepts empty string arguments except for the emFrom and emTo variables.