【问题标题】:Emails not getting through Microsoft Graph电子邮件无法通过 Microsoft Graph
【发布时间】: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


    【解决方案1】:

    因为您使用应用程序权限,所以支持 /me 端点

    调用 /me 端点需要登录用户,因此需要委派权限。 使用 /me 端点时不支持应用程序权限。 https://docs.microsoft.com/en-us/graph/api/user-get?view=graph-rest-1.0&tabs=http

    因此,您需要在代码中指定要发送的用户,因为这应该是一个简单的修复方法,例如

     graphClient.Users["user@domain.com"].SendMail(message, saveToSentItems).Request().PostAsync();
    

    【讨论】:

    • 我确实尝试过这种方法,但还没有运气。我在 Azure 中创建了一个域来进行测试。我尝试输入用户的电子邮件地址,但仍然没有看到任何邮件被发送。
    • 对此的先决条件是您输入的用户需要拥有许可的 Exchange Online 邮箱。 (例如,他们应该能够登录到outlook.office365.com 并发送电子邮件)。这些 Graph 端点使您可以访问 Exchange Online 后端,它们不是通用 SMTP 类型的端点。
    猜你喜欢
    • 2021-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    相关资源
    最近更新 更多