问题
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.
Todo this, I first created and tried and run some queries in the Office 365 API Playground:
https://outlook.office.com/api/v2.0/me/mailfolders/sentitems/messages?$filter=sentdatetime%20ge%202015-12-10T08:00:00.000Z&$select=Subject,CreatedDateTime,ToRecipients
https://outlook.office.com/api/v2.0/me/messages?$count=true&$filter=receiveddatetime%20ge%202015-12-09T10:00:00.000Z&$select=Subject,CreatedDateTime,ToRecipients
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:
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();
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'.
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.?
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.
回答1:
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();
On your questions:
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")
- 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 yourdate
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 + "\n";
}
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 + "\n";
}
MessageBox.Show(resultMessage, "Sent messages");
来源:https://stackoverflow.com/questions/34212568/write-efficient-queries-with-the-outlookservicesclient