Esempio n. 1
0
        public async Task <IActionResult> Send(string id, [FromForm] SendEmailViewModel request, string emailAccountName = null)
        {
            try
            {
                EmailMessage emailMessage = JsonConvert.DeserializeObject <EmailMessage>(request.EmailMessageJson);

                Guid emailId = Guid.Parse(id);
                var  email   = repository.GetOne(emailId);

                var emailAttachments = new List <EmailAttachment>();
                var attachments      = emailAttachmentRepository.Find(null, q => q.EmailId == emailId)?.Items;

                if (email.Status.Equals("Draft"))
                {
                    //if file doesn't exist in binary objects: add binary object entity, upload file, and add email attachment entity
                    string      hash       = string.Empty;
                    IFormFile[] filesArray = manager.CheckFiles(request.Files, emailId, hash, attachments);
                    emailAttachments = manager.AddAttachments(filesArray, emailId, hash);
                    if (request.Files == null || request.Files.Length == 0)
                    {
                        emailMessage.Attachments = emailAttachments;
                    }
                    else
                    {
                        emailMessage.Attachments = attachments;
                    }

                    //email account name is nullable, so it needs to be used as a query parameter instead of in the put url
                    //if no email account is chosen, the default organization account will be used
                    await manager.SendEmailAsync(emailMessage, emailAccountName, id, "Outgoing");

                    email = repository.Find(null, q => q.Id == emailId)?.Items?.FirstOrDefault();
                    EmailViewModel emailViewModel = manager.GetEmailViewModel(email, attachments);
                    if (attachments.Count == 0 || attachments == null)
                    {
                        emailViewModel.Attachments = emailAttachments;
                    }
                    return(Ok(emailViewModel));
                }
                else
                {
                    ModelState.AddModelError("Send email", "Email was not able to be sent.  Email is not a draft.");
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception ex)
            {
                return(ex.GetActionResult());
            }
        }