-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
162 lines (142 loc) · 4.25 KB
/
Program.cs
File metadata and controls
162 lines (142 loc) · 4.25 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
Console.WriteLine("Microsoft .NET Graph Tutorial\n");
var settings = Settings.LoadSettings();
// Initialize Graph
InitializeGraph(settings);
// Greet the user by name
await GreetUserAsync();
int choice = -1;
while (choice != 0)
{
Console.WriteLine("Please choose one of the following options:");
Console.WriteLine("0. Exit");
Console.WriteLine("1. Display access token");
Console.WriteLine("2. List my inbox");
Console.WriteLine("3. Send mail");
Console.WriteLine("4. Make a Graph call");
try
{
choice = int.Parse(Console.ReadLine() ?? string.Empty);
}
catch (System.FormatException)
{
// Set to invalid value
choice = -1;
}
switch(choice)
{
case 0:
// Exit the program
Console.WriteLine("Goodbye...");
break;
case 1:
// Display access token
await DisplayAccessTokenAsync();
break;
case 2:
// List emails from user's inbox
await ListInboxAsync();
break;
case 3:
// Send an email message
await SendMailAsync();
break;
case 4:
// Run any Graph code
await MakeGraphCallAsync();
break;
default:
Console.WriteLine("Invalid choice! Please try again.");
break;
}
}
void InitializeGraph(Settings settings)
{
GraphHelper.InitializeGraphForUserAuth(settings,
(info, cancel) =>
{
// Display the device code message to
// the user. This tells them
// where to go to sign in and provides the
// code to use.
Console.WriteLine(info.Message);
return Task.FromResult(0);
});
}
async Task GreetUserAsync()
{
try
{
var user = await GraphHelper.GetUserAsync();
Console.WriteLine($"Hello, {user?.DisplayName}!");
// For Work/school accounts, email is in Mail property
// Personal accounts, email is in UserPrincipalName
Console.WriteLine($"Email: {user?.Mail ?? user?.UserPrincipalName ?? ""}");
}
catch (Exception ex)
{
Console.WriteLine($"Error getting user: {ex.Message}");
}
}
async Task DisplayAccessTokenAsync()
{
try
{
var userToken = await GraphHelper.GetUserTokenAsync();
Console.WriteLine($"User token: {userToken}");
}
catch (Exception ex)
{
Console.WriteLine($"Error getting user access token: {ex.Message}");
}
}
async Task ListInboxAsync()
{
try
{
var messagePage = await GraphHelper.GetInboxAsync();
// Output each message's details
foreach (var message in messagePage.CurrentPage)
{
Console.WriteLine($"Message: {message.Subject ?? "NO SUBJECT"}");
Console.WriteLine($" From: {message.From?.EmailAddress?.Name}");
Console.WriteLine($" Status: {(message.IsRead!.Value ? "Read" : "Unread")}");
Console.WriteLine($" Received: {message.ReceivedDateTime?.ToLocalTime().ToString()}");
}
// If NextPageRequest is not null, there are more messages
// available on the server
// Access the next page like:
// messagePage.NextPageRequest.GetAsync();
var moreAvailable = messagePage.NextPageRequest != null;
Console.WriteLine($"\nMore messages available? {moreAvailable}");
}
catch (Exception ex)
{
Console.WriteLine($"Error getting user's inbox: {ex.Message}");
}
}
async Task SendMailAsync()
{
try
{
// Send mail to the signed-in user
// Get the user for their email address
var user = await GraphHelper.GetUserAsync();
var userEmail = user?.Mail ?? user?.UserPrincipalName;
if (string.IsNullOrEmpty(userEmail))
{
Console.WriteLine("Couldn't get your email address, canceling...");
return;
}
await GraphHelper.SendMailAsync("Testing Microsoft Graph",
"Hello world!", userEmail);
Console.WriteLine("Mail sent.");
}
catch (Exception ex)
{
Console.WriteLine($"Error sending mail: {ex.Message}");
}
}
async Task MakeGraphCallAsync()
{
await GraphHelper.MakeGraphCallAsync();
}