-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
135 lines (115 loc) · 4.71 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Me.SendMail;
using Microsoft.Graph.Models;
using Microsoft.Graph.Models.ODataErrors;
public class Program
{
public static async Task Main(string[] args)
{
var settings = Settings.LoadSettings();
string[] scopes = {"User.Read", "Mail.Read", "Mail.ReadWrite", "Mail.Send"};
GraphServiceClient graphClient = new Authentication().getGraphClient(scopes, settings);
User? me = await graphClient.Me.GetAsync(
requestConfig => {
//You can also request specific properties
requestConfig.QueryParameters.Select = new string[] {"id", "displayName", "mail", "userPrincipalName", "createdDateTime"};
}
);
Console.WriteLine($"Hello {me?.GivenName}!"); //givenName is null because this properties wasn't requested
Console.WriteLine($"$Hello {me?.DisplayName}! Your email is {me?.Mail}");
//Get the first page of messages - the default page size is 10
//More info: https://learn.microsoft.com/en-us/graph/api/user-list-messages?view=graph-rest-1.0&tabs=csharp#request
var firstPage = await graphClient.Me.Messages.GetAsync(
/*You can also change the page size
requestConfig => {
requestConfig.QueryParameters.Top = 5;
}*/
);
Console.WriteLine($"Fetched {firstPage?.Value?.Count} messages via default request");
var messagesCollected = new List<Message>();
//We can leverage the PageIterator to iterate over the pages
var pageIterator = PageIterator<Message, MessageCollectionResponse>.CreatePageIterator(
graphClient,
firstPage,
message =>
{
messagesCollected.Add(message);
return true;
},//Per item callback
request =>
{
Console.WriteLine($"Requesting new page with url {request.URI.OriginalString}");
return request;
}//Per request/page callback to reconfigure the request
);
//Then iterate over the pages
await pageIterator.IterateAsync();
// Get the messages data;
Console.WriteLine($"Fetched {messagesCollected.Count} messages via page iterator");
Console.WriteLine($"First message subject: {messagesCollected[0].Subject}");
Console.WriteLine($"Last message subject: {messagesCollected[messagesCollected.Count - 1].Subject}");
Console.WriteLine("-----------Done with paged requests-----------");
//Sending a message
var message = new Message
{
Subject = "Hello from Graph!",
Body = new ItemBody{
Content = "Hello from Graph!",
ContentType = BodyType.Text
},
ToRecipients = new List<Recipient>{
new Recipient{
EmailAddress = new EmailAddress{
Address = "[email protected]"
}
}
}
};
var saveToSentItems = true;
var body = new SendMailPostRequestBody
{
Message = message,
SaveToSentItems = saveToSentItems
};
try
{
await graphClient.Me.SendMail.PostAsync(body);
Console.WriteLine("Message sent!");
}
catch (ODataError odataError)
{
Console.WriteLine(odataError.Error.Code);
Console.WriteLine(odataError.Error.Message);
throw;
}
var lastMessage = await graphClient.Me.Messages.GetAsync(
requestConfig => {
requestConfig.QueryParameters.Top = 1;
}
);
Console.WriteLine("lastMessage: " + lastMessage.Value[0].Subject);
Console.WriteLine("lastMessage: " + lastMessage.Value[0].Id);
WaitCallback waitCallback = new WaitCallback((state) => {
Thread.Sleep(5000);
Console.WriteLine("WaitCallback: " + state);
});
try
{
await graphClient.Me.Messages[lastMessage.Value[0].Id].DeleteAsync();
Console.WriteLine("Message deleted!");
}
catch (ODataError odataError)
{
Console.WriteLine(odataError.Error.Code);
Console.WriteLine(odataError.Error.Message);
throw;
}
var lastMessageAfterDeleting = await graphClient.Me.Messages.GetAsync(
requestConfig => {
requestConfig.QueryParameters.Top = 1;
}
);
Console.WriteLine("lastMessageAfterDeleting: " + lastMessageAfterDeleting.Value[0].Subject);
}
}