private Attachment[] GetAttachmentsWithManagedApi(string[] attachmentIds, string authToken, string ewsUrl) { ExchangeService service = new ExchangeService(); service.Credentials = new OAuthCredentials(authToken); service.Url = new Uri(ewsUrl); ServiceResponseCollection <GetAttachmentResponse> responses = service.GetAttachments( attachmentIds, null, new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.MimeContent)); List <Attachment> attachments = new List <Attachment>(); foreach (GetAttachmentResponse response in responses) { Attachment attachment = new Attachment(); attachment.AttachmentName = response.Attachment.Name; if (response.Attachment is FileAttachment) { FileAttachment file = response.Attachment as FileAttachment; attachment.AttachmentBytes = file.Content; } else if (response.Attachment is ItemAttachment) { // Skip item attachments for now // TODO: Add code to extract the MIME content of the item // and build a .EML file to save to OneDrive. } attachments.Add(attachment); } return(attachments.ToArray()); }
private AttachmentResponse GetAttachmentsFromExchangeServerUsingEWSManagedApi(AttachmentRequest request) { var attachmentsProcessedCount = 0; var attachmentNames = new List <string>(); // Creo un oggetto di tipo ExchangeService ExchangeService service = new ExchangeService(); //imposto il token di autenticazione ricevuto dall'add-in service.Credentials = new OAuthCredentials(request.attachmentToken); //imposto la url del server Exchange service.Url = new Uri(request.ewsUrl); // Richiede gli allegati al server var getAttachmentsResponse = service.GetAttachments( request.attachments.Select(a => a.id).ToArray(), null, new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.MimeContent)); if (getAttachmentsResponse.OverallResult == ServiceResult.Success) { foreach (var attachmentResponse in getAttachmentsResponse) { attachmentNames.Add(attachmentResponse.Attachment.Name); if (attachmentResponse.Attachment is FileAttachment) { //mette il contenuto dell'allegato in uno stream FileAttachment fileAttachment = attachmentResponse.Attachment as FileAttachment; Stream s = new MemoryStream(fileAttachment.Content); // Qui è possibile processare il contenuto dell'allegato } if (attachmentResponse.Attachment is ItemAttachment) { //mette il contenuto dell'allegato in uno stream ItemAttachment itemAttachment = attachmentResponse.Attachment as ItemAttachment; Stream s = new MemoryStream(itemAttachment.Item.MimeContent.Content); // Qui è possibile processare il contenuto dell'allegato } attachmentsProcessedCount++; } } // La risposta contiene il nome ed il numero degli allegati che sono stati // processati dal servizio var response = new AttachmentResponse(); response.attachmentNames = attachmentNames.ToArray(); response.attachmentsProcessed = attachmentsProcessedCount; return(response); }
public IEnumerable <string> Get([FromBody] AttachmentRequest request) { ExchangeService service = new ExchangeService(); service.Credentials = new OAuthCredentials(request.AttachmentToken); service.Url = new Uri(request.EWSURL); List <string> attachmentIds = request.Attachments.Select(x => x.ID).ToList(); List <string> attachmentNames = new List <string>(); var response = service.GetAttachments(attachmentIds.ToArray(), null, new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.MimeContent)); if (response.OverallResult == ServiceResult.Success) { foreach (var attachmentResponse in response) { attachmentNames.Add(attachmentResponse.Attachment.Name); if (attachmentResponse.Attachment is FileAttachment) { FileAttachment a = attachmentResponse.Attachment as FileAttachment; using (Stream ms = new MemoryStream(a.Content)) using (FileStream fs = new FileStream($@"C:\Users\Af\Desktop\{attachmentResponse.Attachment.Name}", FileMode.Create, FileAccess.Write)) { byte[] bytes = new byte[ms.Length]; ms.Read(bytes, 0, (int)ms.Length); fs.Write(bytes, 0, bytes.Length); } } if (attachmentResponse.Attachment is ItemAttachment) { ItemAttachment attachment = attachmentResponse.Attachment as ItemAttachment; } } } return(attachmentNames); }