I am using this library (Microsoft Graph .NET client library) in my project to retrieve emails from my mailbox. However, when I try to filter messages from my mailbox by createdDateTime or body, I get a "Bad Request" error. Here are my requests:
private static IMailFolderMessagesCollectionPage GetMessages(string subject, int count)
{
return new graphServiceClient.Me.MailFolders.Inbox.Messages.Request().Filter($"subject eq '{subject}' and createdDateTime gt '{DateTime.Now.AddMinutes(-10)}'").Top(count).GetAsync().Result;
}
private static IMailFolderMessagesCollectionPage GetMessages(string subject, string bodyPart, int count)
{
return new graphServiceClient.Me.MailFolders.Inbox.Messages.Request().Filter($"subject eq '{subject}' and body contains '{bodyPart}'").Top(count).GetAsync().Result;
}
Do the Microsoft Graph API and the library itself that I use support filtering of emails(messages) by createdDateTime and body? And if so, does anyone know what the problem is?
UPD
Turns out I had the wrong request to filter the emails by body. Below is the correct query, but I can't find any letters for this query yet. Maybe someone will find it useful.
private static IMailFolderMessagesCollectionPage GetMessages(string subject, string bodyPart, int count)
{
return new graphServiceClient.Me.MailFolders.Inbox.Messages.Request().Filter($"subject eq '{subject}' and contains(body/content, '{bodyPart}')").Top(count).GetAsync().Result;
}
For filtering by createdDateTime
, you should remove ''
and use ISO 8601 format. For example: 2021-07-29T14:32:12Z
In your case, you can use ToString("s")
, please find the code as below:
return new graphServiceClient.Me.MailFolders.Inbox.Messages.Request().Filter($"subject eq '{subject}' and createdDateTime gt {DateTime.UtcNow.AddMinutes(-10).ToString("s")}Z").Top(count).GetAsync().Result;