Beispiel #1
0
        public async Task <IActionResult> GetEmail([FromRoute] int id)
        {
            if (!ModelState.isValid)
            {
                return(BadRequest(ModelState));
            }
            var email = await _repo.Find(id);

            if (!email)
            {
                return(NotFound(id));
            }
            return(Ok(email));
        }
Beispiel #2
0
        public Task SendEmailAsync(EmailMessage emailMessage, string accountName = null, string id = null, string direction = null)
        {
            Email emailObject = new Email();
            Guid? emailId     = Guid.NewGuid();

            if (!string.IsNullOrEmpty(id))
            {
                emailId     = Guid.Parse(id);
                emailObject = _emailRepository.Find(null, q => q.Id == emailId)?.Items?.FirstOrDefault();
                if (emailObject == null)
                {
                    emailObject = new Email()
                    {
                        Id     = emailId,
                        Status = StatusType.Unknown.ToString()
                    };
                }
            }

            Email email = new Email();

            if (!string.IsNullOrEmpty(id))
            {
                email.Id = emailId;
            }

            //find email settings and determine is email is enabled/disabled
            var organizationId = Guid.Parse(_organizationManager.GetDefaultOrganization().Id.ToString());
            var emailSettings  = _emailSettingsRepository.Find(null, s => s.OrganizationId == organizationId).Items.FirstOrDefault();
            //check if accountName exists
            var existingAccount = _emailAccountRepository.Find(null, d => d.Name.ToLower(null) == accountName?.ToLower(null))?.Items?.FirstOrDefault();

            if (existingAccount == null)
            {
                existingAccount = _emailAccountRepository.Find(null, d => d.IsDefault && !d.IsDisabled).Items.FirstOrDefault();
            }

            //if there are no records in the email settings table for that organization, email should be disabled
            if (emailSettings == null)
            {
                email.Status = StatusType.Blocked.ToString();
                email.Reason = "Email disabled.  Please configure email settings.";
            }
            //if there are email settings but they are disabled, don't send email
            else if (emailSettings != null && emailSettings.IsEmailDisabled)
            {
                email.Status = StatusType.Blocked.ToString();
                email.Reason = "Email functionality has been disabled.";
            }
            else
            {
                if (existingAccount == null && emailSettings != null)
                {
                    existingAccount = _emailAccountRepository.Find(null, a => a.IsDefault == true && a.IsDisabled == false)?.Items?.FirstOrDefault();
                    if (existingAccount == null)
                    {
                        email.Status = StatusType.Failed.ToString();
                        email.Reason = $"Account '{accountName}' could be found.";
                    }
                    if (existingAccount != null && existingAccount.IsDisabled == true)
                    {
                        email.Status = StatusType.Blocked.ToString();
                        email.Reason = $"Account '{accountName}' has been disabled.";
                    }
                }
                //set from email address
                else if (existingAccount != null)
                {
                    EmailAddress        fromEmailAddress = new EmailAddress(existingAccount.FromName, existingAccount.FromEmailAddress);
                    List <EmailAddress> emailAddresses   = new List <EmailAddress>();

                    foreach (EmailAddress emailAddress in emailMessage.From)
                    {
                        if (!string.IsNullOrEmpty(emailAddress.Address))
                        {
                            emailAddresses.Add(emailAddress);
                        }
                    }
                    emailMessage.From.Clear();
                    foreach (EmailAddress emailAddress in emailAddresses)
                    {
                        emailMessage.From.Add(emailAddress);
                    }
                    emailMessage.From.Add(fromEmailAddress);
                }

                //remove email addresses in to, cc, and bcc lists with domains that are blocked or not allowed
                List <EmailAddress> toList  = new List <EmailAddress>();
                List <EmailAddress> ccList  = new List <EmailAddress>();
                List <EmailAddress> bccList = new List <EmailAddress>();

                if (string.IsNullOrEmpty(emailSettings.AllowedDomains))
                {
                    if (!string.IsNullOrEmpty(emailSettings.BlockedDomains))
                    {
                        //remove any email address that is in blocked domain
                        IEnumerable <string>?denyList = (new List <string>(emailSettings?.BlockedDomains?.Split(','))).Select(s => s.ToLowerInvariant().Trim());
                        foreach (EmailAddress address in emailMessage.To)
                        {
                            if (!string.IsNullOrEmpty(address.Address))
                            {
                                MailboxAddress mailAddress = new MailboxAddress(address.Address);
                                if (!denyList.Contains(mailAddress.Address.Split("@")[1].ToLowerInvariant()))
                                {
                                    toList.Add(address);
                                }
                            }
                        }
                        emailMessage.To.Clear();
                        emailMessage.To = toList;

                        foreach (EmailAddress address in emailMessage.CC)
                        {
                            if (!string.IsNullOrEmpty(address.Address))
                            {
                                MailboxAddress mailAddress = new MailboxAddress(address.Address);
                                if (!denyList.Contains(mailAddress.Address.Split("@")[1].ToLowerInvariant()))
                                {
                                    ccList.Add(address);
                                }
                            }
                        }
                        emailMessage.CC.Clear();
                        emailMessage.CC = ccList;

                        foreach (EmailAddress address in emailMessage.Bcc)
                        {
                            if (!string.IsNullOrEmpty(address.Address))
                            {
                                MailboxAddress mailAddress = new MailboxAddress(address.Address);
                                if (!denyList.Contains(mailAddress.Address.Split("@")[1].ToLowerInvariant()))
                                {
                                    bccList.Add(address);
                                }
                            }
                        }
                        emailMessage.Bcc.Clear();
                        emailMessage.Bcc = bccList;
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(emailSettings.AllowedDomains))
                    {
                        //remove any email address that is not on white list
                        IEnumerable <string> allowList = (new List <string>(emailSettings.AllowedDomains.Split(','))).Select(s => s.ToLowerInvariant().Trim());
                        foreach (EmailAddress address in emailMessage.To)
                        {
                            if (!string.IsNullOrEmpty(address.Address))
                            {
                                MailboxAddress mailAddress = new MailboxAddress(address.Address);
                                if (allowList.Contains(mailAddress.Address.Split("@")[1].ToLowerInvariant()))
                                {
                                    toList.Add(address);
                                }
                            }
                        }
                        emailMessage.To.Clear();
                        emailMessage.To = toList;

                        foreach (EmailAddress address in emailMessage.CC)
                        {
                            if (!string.IsNullOrEmpty(address.Address))
                            {
                                MailboxAddress mailAddress = new MailboxAddress(address.Address);
                                if (allowList.Contains(mailAddress.Address.Split("@")[1].ToLowerInvariant()))
                                {
                                    ccList.Add(address);
                                }
                            }
                        }
                        emailMessage.CC.Clear();
                        emailMessage.CC = ccList;

                        foreach (EmailAddress address in emailMessage.Bcc)
                        {
                            if (!string.IsNullOrEmpty(address.Address))
                            {
                                MailboxAddress mailAddress = new MailboxAddress(address.Address);
                                if (allowList.Contains(mailAddress.Address.Split("@")[1].ToLowerInvariant()))
                                {
                                    bccList.Add(address);
                                }
                            }
                        }
                        emailMessage.Bcc.Clear();
                        emailMessage.Bcc = bccList;
                    }
                }

                if (emailMessage.To.Count == 0)
                {
                    email.Status = StatusType.Blocked.ToString();
                    email.Reason = "No email addresses to send email to.";
                }

                //add any necessary additional email addresses (administrators, etc.)
                if (!string.IsNullOrEmpty(emailSettings.AddToAddress))
                {
                    foreach (string toAddress in emailSettings.AddToAddress.Split(','))
                    {
                        EmailAddress emailAddress = new EmailAddress(toAddress, toAddress);
                        emailMessage.To.Add(emailAddress);
                    }
                }
                if (!string.IsNullOrEmpty(emailSettings.AddCCAddress))
                {
                    foreach (string CCAddress in emailSettings.AddCCAddress.Split(','))
                    {
                        EmailAddress emailAddress = new EmailAddress(CCAddress, CCAddress);
                        emailMessage.CC.Add(emailAddress);
                    }
                }
                if (!string.IsNullOrEmpty(emailSettings.AddBCCAddress))
                {
                    foreach (string BCCAddress in emailSettings.AddBCCAddress.Split(','))
                    {
                        EmailAddress emailAddress = new EmailAddress(BCCAddress);
                        emailMessage.Bcc.Add(emailAddress);
                    }
                }

                //add subject and body prefixes/suffixes
                if (!string.IsNullOrEmpty(emailSettings.SubjectAddPrefix) && !string.IsNullOrEmpty(emailSettings.SubjectAddSuffix))
                {
                    emailMessage.Subject = string.Concat(emailSettings.SubjectAddPrefix, emailMessage.Subject, emailSettings.SubjectAddSuffix);
                }
                if (!string.IsNullOrEmpty(emailSettings.SubjectAddPrefix) && string.IsNullOrEmpty(emailSettings.SubjectAddSuffix))
                {
                    emailMessage.Subject = string.Concat(emailSettings.SubjectAddPrefix, emailMessage.Subject);
                }
                if (string.IsNullOrEmpty(emailSettings.SubjectAddPrefix) && !string.IsNullOrEmpty(emailSettings.SubjectAddSuffix))
                {
                    emailMessage.Subject = string.Concat(emailMessage.Subject, emailSettings.SubjectAddSuffix);
                }
                else
                {
                    emailMessage.Subject = emailMessage.Subject;
                }

                //check if email message body is html or plaintext
                if (emailMessage.IsBodyHtml)
                {
                    if (!string.IsNullOrEmpty(emailSettings.BodyAddPrefix) && !string.IsNullOrEmpty(emailSettings.BodyAddSuffix))
                    {
                        emailMessage.Body = string.Concat(emailSettings.BodyAddPrefix, emailMessage.Body, emailSettings.BodyAddSuffix);
                    }
                    if (!string.IsNullOrEmpty(emailSettings.BodyAddPrefix) && string.IsNullOrEmpty(emailSettings.BodyAddSuffix))
                    {
                        emailMessage.Body = string.Concat(emailSettings.BodyAddPrefix, emailMessage.Body);
                    }
                    if (string.IsNullOrEmpty(emailSettings.BodyAddPrefix) && !string.IsNullOrEmpty(emailSettings.BodyAddSuffix))
                    {
                        emailMessage.Body = string.Concat(emailMessage.Body, emailSettings.BodyAddSuffix);
                    }
                    else
                    {
                        emailMessage.Body = emailMessage.Body;
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(emailSettings.BodyAddPrefix) && !string.IsNullOrEmpty(emailSettings.BodyAddSuffix))
                    {
                        emailMessage.PlainTextBody = string.Concat(emailSettings.BodyAddPrefix, emailMessage.PlainTextBody, emailSettings.BodyAddSuffix);
                    }
                    if (!string.IsNullOrEmpty(emailSettings.BodyAddPrefix) && string.IsNullOrEmpty(emailSettings.BodyAddSuffix))
                    {
                        emailMessage.PlainTextBody = string.Concat(emailSettings.BodyAddPrefix, emailMessage.PlainTextBody);
                    }
                    if (string.IsNullOrEmpty(emailSettings.BodyAddPrefix) && !string.IsNullOrEmpty(emailSettings.BodyAddSuffix))
                    {
                        emailMessage.PlainTextBody = string.Concat(emailMessage.PlainTextBody, emailSettings.BodyAddSuffix);
                    }
                    else
                    {
                        emailMessage.PlainTextBody = emailMessage.Body;
                    }
                }

                //send email
                ISendEmailChore sendEmailChore = null;

                if (existingAccount != null)
                {
                    if (existingAccount.Provider == "SMTP")
                    {
                        sendEmailChore = new SmtpSendEmailChore(existingAccount, emailSettings);
                    }
                    else if (existingAccount.Provider == "Azure")
                    {
                        sendEmailChore = new AzureSendEmailChore(emailSettings, existingAccount);
                    }
                }

                if (sendEmailChore != null)
                {
                    try
                    {
                        if (email.Status != StatusType.Blocked.ToString() || email.Status != StatusType.Failed.ToString())
                        {
                            sendEmailChore.SendEmail(emailMessage);
                            email.Status = StatusType.Sent.ToString();
                            email.Reason = "Email was sent successfully.";
                        }
                    }
                    catch (Exception ex)
                    {
                        email.Status = StatusType.Failed.ToString();
                        email.Reason = "Error: " + ex.Message;
                    }
                }
                else
                {
                    email.Status = StatusType.Failed.ToString();
                    email.Reason = "Email failed to send.";
                }
            }

            //log email and its status
            if (existingAccount != null)
            {
                email.EmailAccountId = Guid.Parse(existingAccount.Id.ToString());
            }
            email.SentOnUTC = DateTime.UtcNow;
            string newEmailMessage = Regex.Replace(emailMessage.Body, @"(<sensitive(\s|\S)*?<\/sensitive>)", "NULL");

            email.EmailObjectJson = newEmailMessage;
            List <string> nameList  = new List <string>();
            List <string> emailList = new List <string>();

            foreach (EmailAddress address in emailMessage.From)
            {
                nameList.Add(address.Name);
                emailList.Add(address.Address);
            }
            email.SenderName    = JsonConvert.SerializeObject(nameList);
            email.SenderAddress = JsonConvert.SerializeObject(emailList);
            email.SenderUserId  = _applicationUser?.PersonId;
            if (string.IsNullOrEmpty(direction))
            {
                email.Direction = Direction.Unknown.ToString();
            }
            else
            {
                email.Direction = direction;
            }

            //TODO: add logic to next two lines of code to allow for assignment of these Guids
            email.ConversationId = null;
            email.ReplyToEmailId = null;

            if (emailObject.Status == StatusType.Unknown.ToString())
            {
                email.Id        = emailObject.Id;
                email.CreatedOn = DateTime.UtcNow;
                email.CreatedBy = _httpContextAccessor.HttpContext.User.Identity.Name;
                _emailRepository.Add(email);
            }
            else if (email.Id != null && email.Id != emailId)
            {
                email.CreatedOn = DateTime.UtcNow;
                email.CreatedBy = _httpContextAccessor.HttpContext.User.Identity.Name;
                _emailRepository.Add(email);
            }
            else
            {
                emailObject.EmailAccountId  = email.EmailAccountId;
                emailObject.SentOnUTC       = email.SentOnUTC;
                emailObject.EmailObjectJson = email.EmailObjectJson;
                emailObject.SenderName      = email.SenderName;
                emailObject.SenderAddress   = email.SenderAddress;
                emailObject.SenderUserId    = email.SenderUserId;
                emailObject.Direction       = email.Direction;
                emailObject.ConversationId  = email.ConversationId;
                emailObject.ReplyToEmailId  = email.ReplyToEmailId;
                emailObject.Status          = email.Status;
                emailObject.Reason          = email.Reason;
                _emailRepository.Update(emailObject);
            }
            return(Task.CompletedTask);
        }
Beispiel #3
0
 public Email GetEmailByIdAsync(string id)
 {
     return(Repository.Find(id));
 }