/// <summary> /// Compose and send a new email. /// </summary> /// <param name="subject">The subject line of the email.</param> /// <param name="bodyContent">The body of the email.</param> /// <param name="recipients">A semicolon-separated list of email addresses.</param> /// <returns></returns> public async Task <byte[]> ComposeAndSendMailAsync(string subject, string bodyContent, string recipients) { // Get current user photo Stream photoStream = await GetCurrentUserPhotoStreamAsync(); // If the user doesn't have a photo, or if the user account is MSA, we use a default photo if (photoStream == null) { var assembly = typeof(MailHelper).GetTypeInfo().Assembly; photoStream = assembly.GetManifestResourceStream("XamarinConnect.test.jpg"); } MemoryStream photoStreamMS = new MemoryStream(); // Copy stream to MemoryStream object so that it can be converted to byte array. photoStream.CopyTo(photoStreamMS); //get ready to return this to the model/UI //ImageSource imageSource = ImageSource.FromStream(() => photoStreamMS); byte[] image = photoStreamMS.ToArray(); DriveItem photoFile = await UploadFileToOneDriveAsync(photoStreamMS.ToArray()); MessageAttachmentsCollectionPage attachments = new MessageAttachmentsCollectionPage(); attachments.Add(new FileAttachment { ODataType = "#microsoft.graph.fileAttachment", ContentBytes = photoStreamMS.ToArray(), ContentType = "image/png", Name = "me.png" }); // Get the sharing link and insert it into the message body. Permission sharingLink = await GetSharingLinkAsync(photoFile.Id); string bodyContentWithSharingLink = String.Format(bodyContent, sharingLink.Link.WebUrl); // Prepare the recipient list string[] splitter = { ";" }; var splitRecipientsString = recipients.Split(splitter, StringSplitOptions.RemoveEmptyEntries); List <Recipient> recipientList = new List <Recipient>(); foreach (string recipient in splitRecipientsString) { recipientList.Add(new Recipient { EmailAddress = new EmailAddress { Address = recipient.Trim() } }); } try { var graphClient = AuthenticationHelper.GetAuthenticatedClient(); var email = new Message { Body = new ItemBody { Content = bodyContentWithSharingLink, ContentType = BodyType.Html, }, Subject = subject, ToRecipients = recipientList, Attachments = attachments }; try { await graphClient.Me.SendMail(email, true).Request().PostAsync(); } catch (ServiceException exception) { throw new Exception("We could not send the message: " + exception.Error == null ? "No error message returned." : exception.Error.Message); } } catch (Exception e) { throw new Exception("We could not send the message: " + e.Message); } return(image); }
public async void GetPeople() { //Debug.WriteLine("GetAuthClient..."); var graphClient = AuthenticationHelper.GetAuthenticatedClient(); try { //Debug.WriteLine("Attempting request..."); //var me = await graphClient.Me.Request().GetAsync(); /* Version 1.2.1 of Microsoft.Graph * The Microsoft.Graph .NET Library doesn't have a method to list people * The People list will return the people relevent to a person based on thier emails and other documents..a score is attached among other properties * * To call this API with the assistance of the Microsoft.Graph API we will get the Me request URL for the signed in user and * obtain the people list for them by appending /people onto the URL and calling the API with it. * The JSON object is handled dynamically since the returned JSON has three objects and we are only concerned with one of them--the list of people. * * the latest version of Microsoft.Graph has a Me.People call built in. */ //string requestUrl = graphClient.Me.Request().RequestUrl; string requestUrl = graphClient.Users.Request().RequestUrl; requestUrl = requestUrl + "('" + CurrentUser.Id + "')/people"; Debug.WriteLine(requestUrl); HttpRequestMessage hrm = new HttpRequestMessage(HttpMethod.Get, requestUrl); await graphClient.AuthenticationProvider.AuthenticateRequestAsync(hrm); var http = graphClient.HttpProvider; HttpResponseMessage response = await graphClient.HttpProvider.SendAsync(hrm); if (response.IsSuccessStatusCode) { var content = await response.Content.ReadAsStringAsync(); //var people = graphClient.HttpProvider.Serializer.DeserializeObject<People>(content); dynamic dyn = JsonConvert.DeserializeObject(content); foreach (var obj in dyn.value) { People.Add(new User() { DisplayName = obj.displayName, Id = obj.id, Mail = obj.mail, UserPrincipalName = obj.userPrincipalName }); } //Debug.WriteLine(content); } } catch (Exception exp) { Debug.WriteLine(exp.Message); //<sarcasm> some awesome, fine-grained error handling </sarcasm> } }