/// <summary> /// Add items from source to dest /// </summary> /// <param name="source">A collection of messages</param> /// <param name="dest">The destination collection</param> public static void AddTo(this IUserMessagesCollectionPage source, ObservableCollection <Graph.Message> dest) { foreach (var item in source) { dest.Add(item); } }
/// <summary> /// 列出所有訊息API /// </summary> /// <param name="graphClient"></param> /// <returns></returns> private static async Task <IUserMessagesCollectionPage> ListMessageAsync(IGraphServiceClient graphClient) { IUserMessagesCollectionPage messages = await graphClient.Me.Messages .Request() .Select(e => new { e.Sender, e.Subject }) .GetAsync(); return(messages); }
public void OnGet() { string[] scopes = new[] { "mail.read" }; GraphServiceClient graphClient = GraphServiceClientFactory.GetAuthenticatedGraphClient(async() => { string result = await tokenAcquisition.GetAccessTokenForUserAsync(scopes); return(result); }, "https://graph.microsoft.com/beta/"); Messages = graphClient.Me.Messages.Request().Top(10).GetAsync().Result; }
private static int DisplayMessages(IUserMessagesCollectionPage messages, int counter) { Console.WriteLine(messages.Count); foreach (var message in messages) { counter++; Console.WriteLine($"{counter:000} | {message.Id} => {message.Subject}"); } return(counter); }
/// <summary> /// Retrieves uemail messages from Microsoft Graph /me endpoint. /// https://docs.microsoft.com/en-us/graph/api/user-list-messages?view=graph-rest-1.0&tabs=csharp /// </summary> /// <returns></returns> private async Task GetMessages() { try { _messages = await GraphClient.Me.Messages .Request() .GetAsync(); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public async Task <IActionResult> Index() { // graphClient = GraphClient; userMessages = await _graphClient.Me.Messages.Request().Top(20) .Select("sender, from, subject, importance,body,receivedDateTime") .GetAsync(); MyMessages = new List <Email>(); foreach (var message in userMessages) { var BodyText = GetPlainTextFromHtml(message.Body.Content); MyMessages.Add(new Email { Id = message.Id, ReceivedDateTime = message.ReceivedDateTime.ToString(), Body = BodyText, Sender = (message.Sender != null) ? message.Sender.EmailAddress.Name : "Unknown name", From = (message.Sender != null) ? message.Sender.EmailAddress.Address : "Unknown email", Subject = message.Subject ?? "No subject", Importance = message.Importance.ToString() }); // var fileName = Path.ChangeExtension(Path.GetTempFileName(), ".html"); // var fs = System.IO.File.CreateText(fileName); // fs.Write(message.Body.Content); // fs.Flush(); // fs.Close(); // System.Diagnostics.Process.Start(fileName); } // return View(); return(View(MyMessages)); }
/// <summary> /// Retrieve current connected user's emails. /// <para>(default=10)</para> /// <para>Permission Scope : Mail.Read (Read user mail)</para> /// </summary> /// <param name="cancellationToken">The <see cref="CancellationToken"/> for the request.</param> /// <param name="top">The number of items to return in a result set.</param> /// <param name="selectFields">array of fields Microsoft Graph has to include in the response.</param> /// <returns>a Collection of Pages containing the messages</returns> public async Task <IUserMessagesCollectionPage> GetEmailsAsync(CancellationToken cancellationToken, int top = 10, MicrosoftGraphMessageFields[] selectFields = null) { IUserMessagesCollectionPage messages = null; if (selectFields == null) { messages = await _graphProvider.Me.Messages.Request().Top(top).OrderBy(OrderBy).GetAsync(cancellationToken); } else { string selectedProperties = MicrosoftGraphHelper.BuildString <MicrosoftGraphMessageFields>(selectFields); messages = await _graphProvider.Me.Messages.Request().Top(top).OrderBy(OrderBy).Select(selectedProperties).GetAsync(); } _nextPageRequest = messages.NextPageRequest; return(messages); }
// Get messages in all the current user's mail folders. public async Task <List <ResultsItem> > GetMyMessages() { List <ResultsItem> items = new List <ResultsItem>(); // Get messages from all mail folders. IUserMessagesCollectionPage messages = await graphClient.Me.Messages.Request(requestOptions).WithUserAccount(ClaimsPrincipal.Current.ToGraphUserAccount()).GetAsync(); if (messages?.Count > 0) { foreach (Message message in messages) { items.Add(new ResultsItem { Display = message.Subject, Id = message.Id }); } } return(items); }
// Get messages in all the current user's mail folders. public async Task <List <ResultsItem> > GetMyMessages(GraphServiceClient graphClient) { List <ResultsItem> items = new List <ResultsItem>(); // Get messages from all mail folders. IUserMessagesCollectionPage messages = await graphClient.Me.Messages.Request().GetAsync(); if (messages?.Count > 0) { foreach (Message message in messages) { items.Add(new ResultsItem { Display = message.Subject, Id = message.Id }); } } return(items); }
private static async Task <IUserMessagesCollectionPage> GetMessagesAsync() { IUserMessagesCollectionPage messages = null; try { graphClient = GraphServiceClientProvider.GetAuthenticatedClient(); messages = await graphClient.Me .Messages .Request() .Select("From,Subject,IsRead,IsDraft,HasAttachments,CreatedDateTime") .GetAsync(); return(messages); } catch (ServiceException e) { MessageBox.Show("We could not get the any messages: " + e.Error.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error); return(null); } }
public async Task <IViewComponentResult> InvokeAsync() { userMessages = await _graphClient.Me.Messages.Request().Top(1) .Select("body") .GetAsync(); var ComponentMessage = new List <Models.Email>(); foreach (var message in userMessages) { var BodyText = GetPlainTextFromHtml(message.Body.Content); ComponentMessage.Add(new Models.Email { Body = BodyText }); } return(View(ComponentMessage)); }
public async Task <IEnumerable <Message> > GetPagedItemsAsync(int pageIndex, int pageSize, CancellationToken cancellationToken = default(CancellationToken)) { IUserMessagesCollectionPage messages = null; if (isFirstCall) { messages = await MicrosoftGraphService.Instance.User.Message.GetEmailsAsync(cancellationToken, pageSize); isFirstCall = false; } else { if (cancellationToken.IsCancellationRequested) { return(messages); } messages = await MicrosoftGraphService.Instance.User.Message.NextPageEmailsAsync(); } return(messages); }
private async void GetMessagesButton_Click(Object sender, RoutedEventArgs e) { try { // Get all messages, whether or not they are in the Inbox //userMessages = await graphClient.Me.Messages.Request().Top(20) // .Select("sender, from, subject, importance") // .GetAsync(); userMessages = await graphClient.Me.Messages.Request().Top(20) .Select("sender, from, subject, importance") .GetAsync(); MyMessages = new ObservableCollection <Models.Message>(); foreach (var message in userMessages) { MyMessages.Add(new Models.Message { Id = message.Id, Sender = (message.Sender != null) ? message.Sender.EmailAddress.Name : "Unknown name", From = (message.Sender != null) ? message.Sender.EmailAddress.Address : "Unknown email", Subject = message.Subject ?? "No subject", Importance = message.Importance.ToString() }); } MessageCountTextBlock.Text = "Here are your first 20 email messages."; MessagesDataGrid.ItemsSource = MyMessages; } catch (ServiceException ex) { MessageCountTextBlock.Text = $"We could not get messages: {ex.Error.Message}"; } }
// Returns the first page of the signed-in user's messages. public static async Task <IUserMessagesCollectionPage> GetMessagesAsync() { IUserMessagesCollectionPage messages = null; try { var graphClient = AuthenticationHelper.GetAuthenticatedClient(); messages = await graphClient.Me.Messages.Request().GetAsync(); foreach (var message in messages) { Debug.WriteLine("Got message: " + message.Subject); } return(messages); } catch (ServiceException e) { Debug.WriteLine("We could not get messages: " + e.Error.Message); return(null); } }
private async void GetMessagesButton_Click(object sender, RoutedEventArgs e) { if (!await Tools.CheckInternetConnection()) { return; } string txtTop = TopText.Text; int top = 0; if (string.IsNullOrEmpty(txtTop)) { top = 10; } else { top = Convert.ToInt32(txtTop); } bool isFirstCall = true; var incrementalCollectionMessages = new IncrementalCollection <Message>((CancellationToken cts, uint count) => { return(Task.Run <ObservableCollection <Message> >(async() => { IUserMessagesCollectionPage messages = null; ObservableCollection <Message> intermediateList = new ObservableCollection <Message>(); if (isFirstCall) { try { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, new DispatchedHandler(() => { Shell.Current.DisplayWaitRing = true; })); messages = await MicrosoftGraphService.Instance.User.Message.GetEmailsAsync(cts, top); } catch (Microsoft.Graph.ServiceException ex) { if (!Dispatcher.HasThreadAccess) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, new DispatchedHandler(async() => { await DisplayAuthorizationErrorMessage(ex, "Read user mail"); })); } } finally { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, new DispatchedHandler(() => { Shell.Current.DisplayWaitRing = false; })); } isFirstCall = false; } else { if (cts.IsCancellationRequested) { return intermediateList; } messages = await MicrosoftGraphService.Instance.User.Message.NextPageEmailsAsync(); } if (messages != null) { messages.AddTo(intermediateList); } return intermediateList; })); }); MessagesList.ItemsSource = incrementalCollectionMessages; }
public static void AddTo(this IUserMessagesCollectionPage source, ObservableCollection <Graph.Message> dest) { Toolkit.Services.MicrosoftGraph.MicrosoftGraphExtensions.AddTo(source, dest); }