C#:如何阅读Microsoft Office电子邮件。不同的方法

发布于 2025-01-22 06:51:50 字数 1985 浏览 2 评论 0原文

我需要使用C#阅读电子邮件(具体任务 - 按范围按接收/发送电子邮件的计数)。 理想情况下,用户可以在网页上输入其Microsoft Office/Exchange电子邮件的凭据,并接收此信息。

我看到了以下实施方法。

  1. Nuget package fiversangeService

使用Nuget软件包microsoft.exchange.webservices我们可以访问电子邮件配置文件,例如

    ExchangeService _service;

        _service = new ExchangeService(ExchangeVersion.Exchange2013_SP1)
        {
            Credentials = new WebCredentials("[email protected]", "mypassword"),
        };
        _service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
        _service.TraceEnabled = true;
        _service.TraceFlags = TraceFlags.All;

        var items = _service.FindItems(WellKnownFolderName.Inbox, searchFilter: new SearchFilter.IsGreaterThan(TaskSchema.DateTimeCreated, DateTime.Now.AddDays(-10)), new ItemView(int.MaxValue));

有时此方法有时(例如,如果使用2向身份验证)

  1. 使用 我们可以访问电子邮件配置文件。 iauthentication -provider和Active Directory tenant ID

有一种方法可以使自己的iAthenticationProvider用实现authenticateCateRequestAsync

    public async Task AuthenticateRequestAsync(HttpRequestMessage request)
    {
        var token = await GetTokenAsync();

        request.Headers.Authorization = new AuthenticationHeaderValue(token.TokenType, token.AccessToken);
    }

之后,我们可以创建我们可以创建/code>像这样:

        GraphServiceClient graphServiceClient =
            new GraphServiceClient(_authenticationProvider, _graphHttpProvider);

可以发送电子邮件,例如:

            await graphServiceClient.Users[fromAddress]
                .SendMail(message, false)
                .Request()
                .PostAsync();

也可以访问

该方法的方式,我想,我需要获取tenantid等(我有点困惑)

我想,我 实施稳定的解决方案以阅读Microsoft Office/Exchange电子邮件?

I need to read emails using C# (concrete task - count of received/sent emails by range).
Ideally, user enters credentials of his Microsoft Office/Exchange email on webpage and receive this info.

I see the following ways to implement it.

  1. ExchangeService

using nuget package Microsoft.Exchange.WebServices we can get access to email profile, e.g.

    ExchangeService _service;

        _service = new ExchangeService(ExchangeVersion.Exchange2013_SP1)
        {
            Credentials = new WebCredentials("[email protected]", "mypassword"),
        };
        _service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
        _service.TraceEnabled = true;
        _service.TraceFlags = TraceFlags.All;

        var items = _service.FindItems(WellKnownFolderName.Inbox, searchFilter: new SearchFilter.IsGreaterThan(TaskSchema.DateTimeCreated, DateTime.Now.AddDays(-10)), new ItemView(int.MaxValue));

sometimes this approach works, but for some cases does not work (for example, if 2-way authentication is enabled)

  1. using IAuthenticationProvider and Active Directory Tenant Id

there is a way to impement own IAuthenticationProvider with implementation AuthenticateRequestAsync like this

    public async Task AuthenticateRequestAsync(HttpRequestMessage request)
    {
        var token = await GetTokenAsync();

        request.Headers.Authorization = new AuthenticationHeaderValue(token.TokenType, token.AccessToken);
    }

after this we can create GraphServiceClient like this:

        GraphServiceClient graphServiceClient =
            new GraphServiceClient(_authenticationProvider, _graphHttpProvider);

and can send email, for example:

            await graphServiceClient.Users[fromAddress]
                .SendMail(message, false)
                .Request()
                .PostAsync();

I suppose, similar way I can get access to folders also

For this approach, I assume, I need to get tenantId etc (I'm a little confused with it)

How to implement stable solution to read Microsoft Office/Exchange emails ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光磨忆 2025-01-29 06:51:50

我会避免使用第一种方法EWS现在是遗产,在这个示例中,您使用基本身份验证,该验证将在今年的10月(如果还没有)。

对于2,您需要创建一个Azure应用程序注册,请参见 https:// .microsoft.com/en-us/graph/use-the-api 有一些详细的文档和有关如何做的文档。还有 https://learn.microsoft.com/en-en-en-us/ Graph/tutorials/aspnet核心这是一个很好的示例,它可以构建一个带您完成所有步骤的WebApp。

I would avoid the first approach EWS is now legacy and in that example your using Basic Authentication which is depreciated and will be disabled in October this year (if it hasn't already been).

For 2 you need to create a Azure Application registration see https://learn.microsoft.com/en-us/graph/use-the-api which has some detailed documentation and walkthroughs on how to do that. There's also https://learn.microsoft.com/en-us/graph/tutorials/aspnet-core which is a pretty good example of building a webapp that takes you through all the steps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文