I would like to create the wpf application that should display all mails which is user sent/receive .By using the graph explorer I can retrieve those values then how can I achieve this and How can get access token from user to see their mails
Loading

Sachin SinghRank #66 · 26.2K reputationPosted Jan 3, 2024, 1:20 AM EST
You can do something like this
namespace YourWpfApp { public partial class MainWindow : Window { private static readonly string clientId = "YourClientId"; private static readonly string tenantId = "YourTenantId"; private static readonly string clientSecret = "YourClientSecret"; private static readonly string[] scopes = { "Mail.Read" }; private readonly IPublicClientApplication publicClientApp; private IConfidentialClientApplication confidentialClientApp; private IEnumerable emails; public MainWindow() { InitializeComponent(); publicClientApp = PublicClientApplicationBuilder .Create(clientId) .WithAuthority(new Uri($"https://login.microsoftonline.com/{tenantId}")) .Build(); AuthenticateAndLoadEmails(); } private async void AuthenticateAndLoadEmails() { try { var result = await publicClientApp .AcquireTokenForClient(scopes) .ExecuteAsync(); confidentialClientApp = ConfidentialClientApplicationBuilder .Create(clientId) .WithClientSecret(clientSecret) .WithAuthority(new Uri($"https://login.microsoftonline.com/{tenantId}")) .Build(); var authProvider = new ClientCredentialProvider(confidentialClientApp, scopes); var graphClient = new GraphServiceClient(authProvider); // Fetch user's emails var messages = await graphClient.Me.Messages.Request().GetAsync(); emails = messages.CurrentPage; DisplayEmails(); } catch (Exception ex) { MessageBox.Show($"Error: {ex.Message}"); } } private void DisplayEmails() { // Populate your ListBox or DataGrid with email details // For example, if you have a ListBox named listBoxEmails: listBoxEmails.ItemsSource = emails.Select(e => $"{e.Subject} - {e.From?.EmailAddress?.Address}"); } } }Jayraj ChhayaRank #304 · 8.2K reputationPosted Jan 3, 2024, 1:18 AM EST
To create a WPF application that displays user emails using Graph Explorer, you will need to follow these steps:
Register your application in Azure Active Directory (AAD):
Mail.Read,Mail.ReadWrite, etc.).Obtain an access token:
Use the Graph API to retrieve email data:
/me/messagesendpoint to retrieve the user's emails.Display the emails in your WPF application:
Here's an example of how you can obtain an access token using MSAL in a WPF application:
using Microsoft.Identity.Client; string clientId = "Your_Application_Client_Id"; string tenantId = "Your_Azure_Tenant_Id"; string[] scopes = { "Mail.Read" }; public async Task GetAccessTokenAsync() { var app = PublicClientApplicationBuilder.Create(clientId) .WithAuthority(AzureCloudInstance.AzurePublic, tenantId) .WithDefaultRedirectUri() .Build(); var authResult = await app.AcquireTokenInteractive(scopes) .WithPrompt(Prompt.SelectAccount) .ExecuteAsync(); return authResult.AccessToken; }Remember to handle authentication and token refreshing in your application to ensure a seamless user experience.