【发布时间】:2020-09-21 18:29:20
【问题描述】:
我正在构建一个从我的应用程序发送电子邮件的流程。我在 Azure 中注册了一个 Office 365 组织来测试它。在我的应用注册中,我授予了以下权限。这些权限已获得管理员同意。
接下来我编写了这段代码,它基于一个 github 项目。
using Microsoft.Graph;
using Microsoft.Graph.Auth;
using Microsoft.Identity.Client;
using System;
using System.Collections.Generic;
using System.Web.UI;
namespace DemoOutlookMail
{
public partial class _Default : Page
{
protected void send_Click(object sender, System.EventArgs e)
{
const string tenantId = "APP REGISTRATION TENANT ID";
const string redirectUri = "https://localhost:44316/";
const string clientSecret = "APP REGISTRATION SECRET";
const string clientId = "APP REGISTRATION CLIENT ID";
const string AuthorityFormat = "https://login.microsoftonline.com/{0}/v2.0";
IConfidentialClientApplication daemonClient;
daemonClient = ConfidentialClientApplicationBuilder.Create(clientId)
.WithAuthority(string.Format(AuthorityFormat, tenantId))
.WithRedirectUri(redirectUri)
.WithClientSecret(clientSecret)
.Build();
ClientCredentialProvider authProvider = new ClientCredentialProvider(daemonClient,"Mail.Send");
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
SendEmail(graphClient);
}
private void SendEmail(GraphServiceClient graphClient)
{
var message = 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
{
Address = "AdeleV@testariesllc.onmicrosoft.com"
}
}
},
CcRecipients = new List<Recipient>()
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = "AlexW@testariesllc.onmicrosoft.com"
}
}
}
};
var saveToSentItems = false;
try
{
graphClient.Me.SendMail(message, saveToSentItems).Request().PostAsync();
}
catch(Exception e)
{
Label1.Text = e.Message;
}
}
}
}
此代码运行但不发送任何电子邮件,不抛出任何异常。 我错过了什么? 原github项目为https://github.com/Azure-Samples/ms-identity-aspnet-daemon-webapp
【问题讨论】:
标签: c# microsoft-graph-api microsoft-graph-sdks microsoft-graph-mail