Esempio n. 1
0
        private static bool SendOwnerEmail(SortMainObject sort, string subject, string body, bool includeAuthors, EmailTypeEnum emailType, ref string errorMsg)
        {
            bool   allMailSent = false;
            string ownerEmail  = ConfigurationManager.AppSettings["OwnerEmail"].ToString();

            try
            {
                Email  email   = new Email();
                string addInfo = string.Empty;
                email.SendTo  = new List <string>();
                email.Subject = subject;


                foreach (var contact in sort.Contacts)
                {
                    if (!string.IsNullOrWhiteSpace(contact.EmployeeId))
                    {
                        var user = UserObject.GetUser(contact.EmployeeId);
                        if (user != null && !string.IsNullOrWhiteSpace(user.Email))
                        {
                            if (!email.SendTo.Exists(n => n.Equals(user.Email, StringComparison.InvariantCultureIgnoreCase)))
                            {
                                email.SendTo.Add(user.Email);
                            }
                        }
                    }
                }

                if (email.SendTo.Count == 0)
                {
                    if (!string.IsNullOrWhiteSpace(sort.OwnerEmail))
                    {
                        if (!email.SendTo.Exists(n => n.Equals(sort.OwnerEmail, StringComparison.InvariantCultureIgnoreCase)))
                        {
                            email.SendTo.Add(sort.OwnerEmail);
                        }
                    }
                }

                if (includeAuthors)
                {
                    foreach (var author in sort.Authors.Where(n => n.AffiliationEnum == AffiliationEnum.INL))
                    {
                        if (!string.IsNullOrWhiteSpace(author.EmployeeId))
                        {
                            var user = UserObject.GetUser(author.EmployeeId);
                            if (user != null && !string.IsNullOrWhiteSpace(user.Email))
                            {
                                email.SendTo.Add(user.Email);
                            }
                        }
                    }
                }

                if (email.SendTo.Count == 0)
                {
                    email.SendTo.Add(ownerEmail);
                    addInfo = "<em><p><strong>This message has been sent to you in lieu of the intended target.  Their email was not found in Employee Data. Please review the email and determine appropriate course of action.</strong></p></em><hr />";
                }

                email.Body  = addInfo + body;
                email.Body += "<hr /><p> You are being contacted because you are either the Owner or a Contact for the Artifact.</p>";
                email.Body += "<p> If you believe this has been in error, please contact the admin: " + ownerEmail + "</p>";
                email.Send(emailType);

                allMailSent = true;
            }
            catch (Exception ex)
            {
                ErrorLogObject.LogError("Email::SendOwnerEmail", ex);
                errorMsg = $"Exeption Caught on sending Email: {ex.Message}";
            }
            return(allMailSent);
        }