C#:如何阅读Microsoft Office电子邮件。不同的方法
我需要使用C#阅读电子邮件(具体任务 - 按范围按接收/发送电子邮件的计数)。 理想情况下,用户可以在网页上输入其Microsoft Office/Exchange电子邮件的凭据,并接收此信息。
我看到了以下实施方法。
- 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向身份验证)
- 使用 我们可以访问电子邮件配置文件。
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.
- 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)
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会避免使用第一种方法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.