-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathProgram.cs
More file actions
74 lines (60 loc) · 2.79 KB
/
Copy pathProgram.cs
File metadata and controls
74 lines (60 loc) · 2.79 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
//https://github.com/jstedfast/MailKit/blob/master/ExchangeOAuth2.md
using MailKit;
using MailKit.Net.Imap;
using MailKit.Security;
using Microsoft.Identity.Client;
var clientId = "<clientId>";
var tenantId = "<tenantId>";
//var options = new PublicClientApplicationOptions
//{
// ClientId = "7dc85a6b-127b-45d2-ba1d-837f95aba0c7",
// TenantId = "2aaa4b86-cc65-43c7-b348-89f4cb2b5c69",
// RedirectUri = "https://picturestore.codewrecks.com/OAuth2/get-token"
//};
//var publicClientApplication = PublicClientApplicationBuilder
// .CreateWithApplicationOptions(options)
// .Build();
//var scopes = new string[] {
// "email",
// "offline_access",
// "https://outlook.office.com/IMAP.AccessAsUser.All", // Only needed for IMAP
// //"https://outlook.office.com/POP.AccessAsUser.All", // Only needed for POP
// //"https://outlook.office.com/SMTP.Send", // Only needed for SMTP
//};
//var authToken = await publicClientApplication.AcquireTokenInteractive(scopes).ExecuteAsync();
//var oauth2 = new SaslMechanismOAuth2(authToken.Account.Username, authToken.AccessToken);
//using (var client = new ImapClient())
//{
// await client.ConnectAsync("outlook.office365.com", 993, SecureSocketOptions.SslOnConnect);
// await client.AuthenticateAsync(oauth2);
// await client.DisconnectAsync(true);
//}
var scopes = new string[] {
//"email",
//"offline_access",
"https://outlook.office365.com/.default",
//"https://outlook.office.com/IMAP.AccessAsUser.All", // Only needed for IMAP
//"https://outlook.office.com/POP.AccessAsUser.All", // Only needed for POP
//"https://outlook.office.com/SMTP.Send", // Only needed for SMTP
//"https://graph.microsoft.com/.default",
};
var confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithClientSecret("<client-secret>")
.WithAuthority(new Uri("https://login.microsoftonline.com/" + tenantId + "/v2.0"))
.Build();
var authenticationResult = await confidentialClientApplication.AcquireTokenForClient(scopes).ExecuteAsync();
var authToken = authenticationResult;
var oauth2 = new SaslMechanismOAuth2("<email address to access>", authToken.AccessToken);
using (var client = new ImapClient(new ProtocolLogger("imapLog.txt")))
{
client.Connect("outlook.office365.com", 993, SecureSocketOptions.SslOnConnect);
//client.AuthenticationMechanisms.Remove("XOAUTH2");
client.Authenticate(oauth2);
var inbox = client.Inbox;
inbox.Open(MailKit.FolderAccess.ReadOnly);
Console.WriteLine("Total messages: {0}", inbox.Count);
Console.WriteLine("Recent messages: {0}", inbox.Recent);
client.Disconnect(true);
}
Console.ReadKey();