/// <summary> /// Converts attachments to AttachedFile(s) /// </summary> /// <param name="message"></param> private async void HandleAttachmentsAsync(MimeMessage message) { byte[] byteArray; //If Attachments is not empty if (message.Attachments.Count() != 0) { AttachedFilesListView.Visibility = Visibility.Visible; foreach (var attachment in message.Attachments) { var fileName = attachment.ContentDisposition?.FileName ?? attachment.ContentType.Name; byteArray = HelperUtils.ConvertAttachmentToByteArray(attachment); if (byteArray == null) { DisplayErrorDialog("Attachment error", "Error displaying attachments"); return; } //Converts byte[] to StorageFile StorageFile storageFile = await HelperUtils.ConvertsToStorageFileAsync(byteArray, fileName); if (storageFile != null) { //Creates new AttachedFile and adds it to AttachedFileList AttachedFileList.Add(new AttachedFile { FileName = storageFile.Name, Thumbnail = await storageFile.GetThumbnailAsync(ThumbnailMode.ListView) }); } } } else { //Makes the listview invisible, since there are no attachments to show AttachedFilesListView.Visibility = Visibility.Collapsed; } }
/// <summary> /// Saves the selected attachment to file /// </summary> private async Task <bool> SaveAttachment() { if (SelectedIndex != -1) { byte[] byteArray; List <MimeEntity> list = CurrentMessage.Attachments.ToList(); var attachment = list[SelectedIndex]; var fileName = attachment.ContentDisposition?.FileName ?? attachment.ContentType.Name; //Converts the attachment to a byte array byteArray = HelperUtils.ConvertAttachmentToByteArray(attachment); if (byteArray == null) { return(false); } var savePicker = new Windows.Storage.Pickers.FileSavePicker(); savePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop; //Gets file extension int index = fileName.LastIndexOf("."); string fileType = fileName.Substring(index); savePicker.FileTypeChoices.Add(fileType.Substring(1), new List <string>() { fileType }); savePicker.DefaultFileExtension = fileType; savePicker.SuggestedFileName = fileName; //Lets the user choose how to save the file StorageFile file = await savePicker.PickSaveFileAsync(); if (file != null) { Windows.Storage.CachedFileManager.DeferUpdates(file); //Writes byte array to Storagefile await Windows.Storage.FileIO.WriteBytesAsync(file, byteArray); Windows.Storage.Provider.FileUpdateStatus status = await Windows.Storage.CachedFileManager.CompleteUpdatesAsync(file); if (status == Windows.Storage.Provider.FileUpdateStatus.Complete) { return(true); } else { Debug.WriteLine("File " + file.Name + " couldn't be saved"); return(false); } } else { Debug.WriteLine("Operation cancelled"); return(false); } } else { return(false); } }