/// <summary> /// Helper function /// </summary> public static async Task <IEnumerable <EmailToSend> > FromEntities(int tenantId, IEnumerable <EmailForSave> emails, IBlobService blobService, CancellationToken cancellation) { var result = new List <EmailToSend>(); foreach (var emailForSave in emails) { string body = null; if (!string.IsNullOrWhiteSpace(emailForSave.BodyBlobId)) { var bodyBlobName = EmailUtil.EmailBodyBlobName(emailForSave.BodyBlobId); var bodyContent = bodyBlobName == null ? null : await blobService.LoadBlobAsync(tenantId, bodyBlobName, cancellation); body = Encoding.UTF8.GetString(bodyContent); } var attachments = new List <EmailAttachmentToSend>(); result.Add(new EmailToSend() { To = emailForSave.To?.Split(';'), Cc = emailForSave.Cc?.Split(';'), Bcc = emailForSave.Bcc?.Split(';'), EmailId = emailForSave.Id, Subject = emailForSave.Subject, Body = body, Attachments = attachments, TenantId = tenantId }); foreach (var att in emailForSave?.Attachments ?? new List <EmailAttachmentForSave>()) { if (!string.IsNullOrWhiteSpace(att.ContentBlobId)) { var attBlobName = EmailUtil.EmailAttachmentBlobName(att.ContentBlobId); var attContent = await blobService.LoadBlobAsync(tenantId, attBlobName, cancellation); attachments.Add(new EmailAttachmentToSend { Name = att.Name, Contents = attContent }); } } } return(result); }
public async Task <FileResult> GetAttachment(int emailId, int attachmentId, CancellationToken cancellation) { await Initialize(cancellation); // This enforces read permissions string attachments = nameof(EmailForQuery.Attachments); var result = await GetById(emailId, new GetByIdArguments { Select = $"{attachments}.{nameof(EmailAttachment.ContentBlobId)},{attachments}.{nameof(EmailAttachment.Name)}" }, cancellation); // Get the blob name var attachment = result.Entity?.Attachments?.FirstOrDefault(att => att.Id == attachmentId); if (attachment != null && !string.IsNullOrWhiteSpace(attachment.ContentBlobId)) { try { // Get the bytes string blobName = EmailUtil.EmailAttachmentBlobName(attachment.ContentBlobId); var bytes = await _blobService.LoadBlobAsync(_behavior.TenantId, blobName, cancellation); // Get the content type var name = attachment.Name; return(new FileResult(bytes, name)); } catch (BlobNotFoundException) { throw new NotFoundException <int>(attachmentId); } } else { throw new NotFoundException <int>(attachmentId); } }