public static async Task Main() { // get your 30-day trial key at https://www.rebex.net/support/trial/ Rebex.Licensing.Key = LicenseKey.Value; try { // make sure we have an Azure application client ID and a Rebex key (feel free to remove these checks once configured) if (ClientId.Contains("00000000-")) { throw new ApplicationException("Please configure ClientId in MainWindow.xaml.cs file."); } if (Rebex.Licensing.Key.Contains("_TRIAL_KEY_")) { throw new ApplicationException("Please set a license key in LicenseKey.cs file."); } // get an instanca of 'CCA' API var cca = ConfidentialClientApplicationBuilder .Create(ClientId) .WithClientSecret(ClientSecretValue) .WithTenantId(TenantId) .Build(); // authenticate interactively for the scopes we need Console.WriteLine("Authenticating via Office365..."); AuthenticationResult result = await cca.AcquireTokenForClient(Scopes).ExecuteAsync(); // keep the access token and account info string accessToken = result.AccessToken; // retrieve the list of recent messages const int PageSize = 5; const int MessageMaxCount = 20; // connect using Rebex EWS and retrieve list of messages using (var client = new Ews()) { // communication logging (enable if needed) //client.LogWriter = new FileLogWriter("ews-oauth.log", LogLevel.Debug); /// impersonate for a mailbox client.Settings.Impersonation = new EwsImpersonation() { SmtpAddress = SmtpAddress }; // connect to the server Console.WriteLine("Connecting to EWS..."); await client.ConnectAsync("outlook.office365.com", SslMode.Implicit); // authenticate using the OAuth 2.0 access token Console.WriteLine("Authenticating to EWS..."); await client.LoginAsync(accessToken, EwsAuthentication.OAuth20); // list recent messages in the 'Inbox' folder Console.WriteLine("Listing folder contents..."); EwsFolderInfo folderInfo = await client.GetFolderInfoAsync(EwsFolderId.Inbox); for (int i = 0; i < Math.Min(MessageMaxCount, folderInfo.ItemsTotal); i += PageSize) { EwsMessageCollection list = await client.GetMessageListAsync(EwsFolderId.Inbox, EwsItemFields.Envelope, EwsPageView.CreateIndexed(i, Math.Min(PageSize, folderInfo.ItemsTotal - i))); foreach (EwsMessageInfo item in list) { Console.WriteLine($"{item.ReceivedDate:yyyy-MM-dd} {item.From}: {item.Subject}"); } } } Console.WriteLine("Finished successfully!"); } catch (Exception ex) { Console.WriteLine($"Error: {ex}"); return; } }
private void GetMessageList() { const int PageSize = 5; const int MessageMaxCount = 20; lvItems.Items.Clear(); // connect using Rebex EWS and retrieve list of messages using (var client = new Ews()) { // communication logging (enable if needed) //client.LogWriter = new FileLogWriter("ews-oauth.log", LogLevel.Debug); // connect to the server statusLabel.Content = "Connecting to EWS..."; client.Connect("outlook.office365.com", SslMode.Implicit); // authenticate using the OAuth 2.0 access token statusLabel.Content = "Authenticating to EWS..."; client.Login(_credentials.AccessToken, EwsAuthentication.OAuth20); // list recent messages in the 'Inbox' folder statusLabel.Content = "Listing folder contents..."; EwsFolderInfo folderInfo = client.GetFolderInfo(EwsFolderId.Inbox); for (int i = 0; i < Math.Min(MessageMaxCount, folderInfo.ItemsTotal); i += PageSize) { EwsMessageCollection list = client.GetMessageList(EwsFolderId.Inbox, EwsItemFields.Envelope, EwsPageView.CreateIndexed(i, Math.Min(PageSize, folderInfo.ItemsTotal - i))); foreach (EwsMessageInfo item in list) { lvItems.Items.Add($"{item.ReceivedDate:yyyy-MM-dd} {item.From}: {item.Subject}"); } } } statusLabel.Content = "Finished successfully!"; }