I'm creating a C# .Net Core 2.0 console application to read a specific user's email. I successfully got this sample console application working. So authentication is working. I added permissions to Read all User's email. I looked at the API docs and I can't see examples of reading a user's email. Plenty of send examples. Any help appreciated.
2 Answers
Thanks for posting. I got this to work if I comment out the Filter:
GraphServiceClient client = GetAuthenticatedClient();
string subject = "RE: ACTION NEEDED:";
string dt = "2018-10-5T00:00:00";
IUserMessagesCollectionPage msgs = client.Users["[email protected]"].Messages.Request()
//.Filter($"receivedDateTime ge '{dt}'") // Invalid filter
.Filter($"startswith(subject, '{subject}') and receivedDateTime gt {dt}")
.Select(m => new { m.Subject, m.ReceivedDateTime, m.From, m.Body })
.Top(100)
.GetAsync().Result;
int msgCnt = msgs.Count;
I posted something about getting filter to work. startswith works but the date filter fails.
Comments
To get each one's mail.
var users = graphClient.Users.Request().GetAsync().Result;
To get the specific user's mail:
List<QueryOption> options = new List<QueryOption>
{
new QueryOption("$filter", "startswith(displayName,'the specific user's mail')")
};
var users= await graphClient.Users.Request(options).GetAsync();
//Or
var users = await graphClient.Users.Request().Filter("startswith(displayName,'help')").GetAsync();
Get Mail:
foreach (var item in users)
{
string currentMail=item.Mail;
}
Update 2018-10-7 For your updated post:
You can use the following API to filter the mails:
https://graph.microsoft.com/v1.0/me/mailFolders/inbox/messages?$filter=ReceivedDateTime ge 2018-10-04 and startswith(subject,'{subject}')