-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
119 lines (108 loc) · 3.36 KB
/
Copy pathProgram.cs
File metadata and controls
119 lines (108 loc) · 3.36 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
// See https://aka.ms/new-console-template for more information
using MSGraph.Mail.Client;
using MSGraph.Mail.Client.Authentications;
using MSGraph.Mail.Client.Models;
var settings = EmailApp.Settings.LoadSettings();
IAuthenticationProvider authenticationProvider = new AuthenticationInteractiveProvider(settings);
var user = await authenticationProvider.GetUserProfile(null);
//IAuthenticationProvider authenticationProvider = new AuthenticationClientSecretProvider(settings);
//var user = await authenticationProvider.GetUserProfile(settings.UserEmail);
Console.WriteLine($"Hello, {user?.UserPrincipalName}");
IEmailGraphService emailGraphService = new EmailGraphService(authenticationProvider);
int choice = -1;
while (choice != 0)
{
Console.WriteLine("Please choose one of the following options:");
Console.WriteLine("0. Exit");
Console.WriteLine("1. List my inbox");
Console.WriteLine("2. Send mail");
Console.WriteLine("3. 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:
// List emails from user's inbox
await ListInboxAsync();
break;
case 2:
// Send an email message
await SendMailAsync();
break;
case 3:
// Run any Graph code
await MakeGraphCallAsync();
break;
default:
Console.WriteLine("Invalid choice! Please try again.");
break;
}
}
Task MakeGraphCallAsync()
{
throw new NotImplementedException();
}
async Task SendMailAsync()
{
await emailGraphService.SendEmail(new EmailMessage
{
ToRecipients = new[]
{
"abhishek2185@gmail.com"
},
CcRecipients = new[]
{
"pixelbyaj@gmail.com"
},
Subject = "Test Email Message",
BodyType = EmailBodyType.Html,
BodyContent = "<h1>Hello, Email 1</h1>",
HasAttachments = true,
FileAttachments = new List<EmailFileAttachment>
{
new EmailFileAttachment
{
ContentType = "text/xml",
Name = "camt.053_test_1.xml",
ContentBytes = File.ReadAllBytes("C:\\source\\data\\camt.053_test_1.xml")
}
}
});
}
async Task ListInboxAsync()
{
var messages = await emailGraphService.GetEmailsAsync(2, -1, new MSGraph.Mail.Client.Models.EmailRequestParameterInformation
{
IsRead = false,
IncludeAttachments = true,
});
foreach (var message in messages)
{
Console.WriteLine(message.From);
Console.WriteLine(string.Join(", ", message.ToRecipients ?? new List<string?>()));
Console.WriteLine(message.Subject);
Console.WriteLine(message.BodyPreview);
if (message.HasAttachments == true)
{
if (message.FileAttachments != null)
{
foreach (var attachment in message.FileAttachments)
{
Console.WriteLine(attachment.Name);
Console.WriteLine(attachment.IsInline);
}
}
}
}
}