使用 OutlookServicesClient 编写高效查询 [英] Write efficient queries with the OutlookServicesClient

查看:20
本文介绍了使用 OutlookServicesClient 编写高效查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Office 365 Mail API 和我的目标是获取用户发送 (1.)和接收 (2.) 电子邮件总数今天.

I am using the Office 365 Mail API and my goal is to get the total number of email messages a user sent (1.) and received (2.) today.

为此,我首先在 Office 365 API Playground 中创建并尝试并运行了一些查询:

Todo this, I first created and tried and run some queries in the Office 365 API Playground:

  1. https://outlook.office.com/api/v2.0/me/mailfolders/sentitems/messages?$filter=sentdatetime%20ge%202015-12-10T08:00:00.000Z&$select=Subject,CreatedDateTime,ToRecipients
  2. https://outlook.office.com/api/v2.0/me/messages?$count=true&$filter=receiveddatetime%20ge%202015-12-09T10:00:00.000Z&$select=Subject,CreatedDateTime,ToRecipients

现在,我正在努力使用 OutlookServicesClient API 编写这些查询.我没有找到很多示例,这些示例超出了非常简单的查询......到目前为止我所拥有的:

Now, I am struggling with writing these queries with the OutlookServicesClient API. I didn't find many examples, which go beyond very simple queries... What I have so far:

  1. var mailResults = await client.Me.MailFolders.Where(f => f.DisplayName == "Sent Items").ExecuteAsync(); 这还没有返回 仅消息并针对当前日期过滤它们.

  1. var mailResults = await client.Me.MailFolders.Where(f => f.DisplayName == "Sent Items").ExecuteAsync(); This does not yet return just the messages and filter them for the current date.

var mailResults = await client.Me.Messages.Where(m => m.ReceivedDateTime.Value == date.UtcDateTime).ExecuteAsync(); 不返回任何结果,即使我收到了很多电子邮件.此外,我想排除在集群"、已删除邮件"和垃圾邮件"文件夹中收到的电子邮件.

var mailResults = await client.Me.Messages.Where(m => m.ReceivedDateTime.Value == date.UtcDateTime).ExecuteAsync(); Does not return any results, even though I received many emails. Further, I'd like to exclude emails which I received in folders 'Cluster', 'Deleted Items' and 'Junk Email'.

一般来说,我不确定使用英文文件夹名称过滤是否是个好主意,因为我需要更改其他语言的代码.特殊 Outlook 文件夹(例如已发送邮件、垃圾邮件、集群等)是否有特殊 ID?

Generally, I am not sure if it's a good idea to filter with English folder names, as I would need to change the code for other languages. Are there special Ids for the special Outlook folders, such as Sent Items, Junk Email, Cluster, etc.?

另外,为了解决我的两个请求,我可以自己获取所有电子邮件并处理过滤,但这效率不高,API 已经支持过滤(如原始请求中所示),我只是不确定如何使用 OutlookServicesClient API 编写它们.

Additionally, to solve my two requests, I could just fetch all emails and handle the filtering by myself, but that's not efficient and the API already supports filtering (as can be seen in the raw requests), I am just not sure how to write them with the OutlookServicesClient API.

推荐答案

通常 OutlookServicesClient 使用 LINQ 来构建它的查询,所以你需要使用 Where构建 $filter 查询参数的方法.例如,如果您想获取今天收到的所有消息,您可以执行以下操作:

Generally the OutlookServicesClient uses LINQ to build it's queries, so you'd need to use the Where method to build a $filter query parameter. If you wanted to get all messages received today, for example, you would do something like:

DateTimeOffset startOfDay = DateTimeOffset.Now.Date.ToUniversalTime();

client.Me.Messages.Where(m => m.ReceivedDateTime >= startOfDay).ExecuteAsync();

关于你的问题:

  1. 不要按文件夹名称过滤.API 有常量文件夹收件箱、已删除邮件、已发送邮件和草稿的 ID.所以要得到你会做的已发送邮件文件夹:

  1. Don't filter by the name of the folder. The API has constant folder IDs for Inbox, Deleted Items, Sent Items, and Drafts. So to get the Sent Items folder you would do:

client.Me.MailFolders.GetById("SentItems")

  • 您的查询Where(m => m.ReceivedDateTime.Value ==date.UtcDateTime) 不会返回值,因为您正在测试 datetime 值是否 等于 为常数,这几乎永远不会返回结果.比较会下降到秒级别,因此除非您在 date 变量中的日期和时间准确收到了消息,否则您将找不到匹配项.
  • Your query Where(m => m.ReceivedDateTime.Value == date.UtcDateTime) wouldn't return values because you're testing that the datetime value is equal to a constant, which is pretty much never going to return a result. The comparison goes down to the seconds level, so unless you have messages received exactly at the date and time in your date variable, you'll get no matches.
  • 我写了一些我认为符合您意图的查询:

    I wrote up some queries that I think match your intent:

    DateTimeOffset startOfDay = DateTimeOffset.Now.Date.ToUniversalTime();
    
    var receivedMessages = await client.Me.Messages
      // $orderby=ReceivedDateTime desc
      .OrderByDescending(m => m.ReceivedDateTime)
      // $filter=ReceivedDateTime ge 2015-12-11T05:00:00Z
      .Where(m => m.ReceivedDateTime >= startOfDay)
      // $top=10
      .Take(10)
      // $select=Subject,ReceivedDateTime,From
      .Select(m => new { m.Subject, m.ReceivedDateTime, m.From })
      .ExecuteAsync();
    
    string resultMessage = "";
    foreach (var message in receivedMessages.CurrentPage)
    {
      resultMessage += "Received: " + message.ReceivedDateTime.ToString() + " from " + message.From.EmailAddress.Address
                       + ": " + message.Subject + "
    ";
    }
    
    MessageBox.Show(resultMessage, "Received messages");
    
    var sentMessages = await client.Me.MailFolders.GetById("SentItems").Messages
      // $orderby=SentDateTime desc
      .OrderByDescending(m => m.SentDateTime)
      // $filter=SentDateTime ge 2015-12-11T05:00:00Z
      .Where(m => m.SentDateTime >= startOfDay)
      // $top=10
      .Take(10)
      // $select=Subject,ReceivedDateTime,From
      .Select(m => new { m.Subject, m.SentDateTime, m.ToRecipients })
      .ExecuteAsync();
    
    resultMessage = "";
    foreach (var message in sentMessages.CurrentPage)
    {
      resultMessage += "Sent: " + message.SentDateTime.ToString() + " to " + message.ToRecipients.Count
                     + " recipients: " + message.Subject + "
    ";
    }
    
    MessageBox.Show(resultMessage, "Sent messages");
    

    这篇关于使用 OutlookServicesClient 编写高效查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆