簡體   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