简体   繁体   English

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

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

I am trying to replace a windows service which sends emails for users via smtp with Microsoft accounts (within the business)我正在尝试用 Microsoft 帐户(在企业内)替换通过 smtp 为用户发送电子邮件的 windows 服务

Microsoft are turning off the old auth method at the end of the month微软将在月底关闭旧的身份验证方法

I am using c# and mail kit and can get a token, however I can not currently send emails without user interaction to 'sign in via a webpage' each time to grant permission我正在使用 c# 和邮件工具包并且可以获得令牌,但是我目前无法在没有用户交互的情况下发送电子邮件以“通过网页登录”每次授予权限

Ofcourse a windows service on another box can not interact with the users当然,另一个盒子上的 windows 服务无法与用户交互

I have registered my app via azure, that part seems done我已经通过 azure 注册了我的应用程序,这部分似乎完成了

I have searched the web and find a lot of mixed results我搜索了 web 发现很多混合的结果

Is anyone aware, Is it currently possible to have a windows service (without user interaction) send emails on a users behalf (I have their user/pass etc) but can not see a clear way to do this using mailkit有谁知道,目前是否有可能拥有 windows 服务(无需用户交互)代表用户发送电子邮件(我有他们的用户/通行证等)但看不到使用 mailkit 的明确方法

Thank You谢谢你

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

meaning you can register your application with a client secret and then use the client secret to make calls to graph without the need for user interaction.这意味着您可以使用客户端密码注册您的应用程序,然后使用客户端密码来调用图形,而无需用户交互。

you can most of this from the graph quick start https://developer.microsoft.com/en-us/graph/quick-start您可以从图表中快速入门 https://developer.microsoft.com/en-us/graph/quick-start

I am working on something similar where I am making graph calls through a c# api but probably similiar.我正在做类似的事情,我通过 c# api 进行图形调用,但可能类似。

I have my registration info in appsettings.我在 appsettings 中有我的注册信息。

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

I have a helper class that makes the api calls (this one is getting some users)我有一个助手 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();
        }
}
}
 

then I can make calls to it like this然后我可以像这样打电话给它

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