繁体   English   中英

Email 发送服务(非用户不活动) - OAuth2 microsoft c# 发送邮件包

[英]Email sending service (non user inactive) - OAuth2 microsoft c# send mailkit

我正在尝试用 Microsoft 帐户(在企业内)替换通过 smtp 为用户发送电子邮件的 windows 服务

微软将在月底关闭旧的身份验证方法

我正在使用 c# 和邮件工具包并且可以获得令牌,但是我目前无法在没有用户交互的情况下发送电子邮件以“通过网页登录”每次授予权限

当然,另一个盒子上的 windows 服务无法与用户交互

我已经通过 azure 注册了我的应用程序,这部分似乎完成了

我搜索了 web 发现很多混合的结果

有谁知道,目前是否有可能拥有 windows 服务(无需用户交互)代表用户发送电子邮件(我有他们的用户/通行证等)但看不到使用 mailkit 的明确方法

谢谢你

似乎 user:sendmail 具有应用程序权限。 https://docs.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0&tabs=http

这意味着您可以使用客户端密码注册您的应用程序,然后使用客户端密码来调用图形,而无需用户交互。

您可以从图表中快速入门 https://developer.microsoft.com/en-us/graph/quick-start

我正在做类似的事情,我通过 c# api 进行图形调用,但可能类似。

我在 appsettings 中有我的注册信息。

 "Settings": {
    "clientId": "7d202de8-ccd8-xxxxxxxx-a5e4-101df736dd6a",
    "clientSecret": "ctV8Q~U3qYHpd_xxxxxxOeHB08TXxxxxxxxxU_.ag9",
    "tenantId": "44fxxxxxx5-327a-4d5a-86d5-cxxxxxxxxx97d7e4"
  },

我有一个助手 class 可以进行 api 调用(这个得到了一些用户)

namespace Application
{
    using Azure.Identity;
    using Microsoft.Graph;
    public class GraphHelper
    {
        private static Settings _settings;

        public static void InitializeGraph(Settings settings,
    Func<DeviceCodeInfo, CancellationToken, Task> deviceCodePrompt)
        {
            _settings = settings;
        }

        private static ClientSecretCredential _clientSecretCredential;
        private static GraphServiceClient _appClient;

        private static void EnsureGraphForAppOnlyAuth()
        {
            _ = _settings ??
                throw new System.NullReferenceException("Settings cannot be null");

            if (_clientSecretCredential == null)
            {
                _clientSecretCredential = new ClientSecretCredential(
                    _settings.TenantId, _settings.ClientId, _settings.ClientSecret);
            }

            if (_appClient == null)
            {
                _appClient = new GraphServiceClient(_clientSecretCredential,
                    new[] { "https://graph.microsoft.com/.default" });
            }
        }

        public static Task<IGraphServiceUsersCollectionPage> GetUsersAsync()
        {
            EnsureGraphForAppOnlyAuth();
            _ = _appClient ??
                throw new System.NullReferenceException("Graph has not been initialized for app-only auth");

            return _appClient.Users
                .Request()
                .Select(u => new
                {
                    // Only request specific properties
                    u.DisplayName,
                    u.Id,
                    u.Mail
                })
                // Get at most 25 results
                .Top(25)
                // Sort by display name
                .OrderBy("DisplayName")
                .GetAsync();
        }
}
}
 

然后我可以像这样打电话给它

public async Task<IGraphServiceUsersCollectionPage> Handle(Query request, CancellationToken cancellationToken)
            {
                Settings s = new Settings();
                var settings = s.LoadSettings(_config);
                GraphHelper.InitializeGraph(settings, (info, cancel) => Task.FromResult(0));
                var result = await GraphHelper.GetUsersAsync();
                return result;
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM