private static bool SendOwnerEmail(MainObject main, string subject, string body, bool includeAuthors, bool includeContacts, ref string errorMsg) { bool allMailSent = false; List <string> recipientNames = new List <string>(); try { Email email = new Email(); string addInfo = string.Empty; email.SendTo = new List <string>(); email.Subject = subject; foreach (var contact in main.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); recipientNames.Add(user.FullName); } } } } if (email.SendTo.Count == 0 || includeContacts) { if (!string.IsNullOrWhiteSpace(main.OwnerEmail)) { if (!email.SendTo.Exists(n => n.Equals(main.OwnerEmail, StringComparison.InvariantCultureIgnoreCase))) { email.SendTo.Add(main.OwnerEmail); recipientNames.Add(main.OwnerName); } } } if (includeAuthors) { foreach (var author in main.Authors.Where(n => n.AffiliationEnum == AuthorAffilitionEnum.INL)) { if (!string.IsNullOrWhiteSpace(author.EmployeeId)) { var user = UserObject.GetUser(author.EmployeeId); if (user != null && !string.IsNullOrWhiteSpace(user.Email)) { if (!email.SendTo.Exists(n => n.Equals(user.Email, StringComparison.InvariantCultureIgnoreCase))) { email.SendTo.Add(user.Email); recipientNames.Add(user.FullName); } } } } } if (email.SendTo.Count == 0) { email.SendTo.Add(Config.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 Repo. Please review the email and determine appropriate course of action.</strong></p></em><hr />"; } email.Body = addInfo + body.Replace("{RecipientsName}", string.Join(" : ", recipientNames)); AddContactInfo(ref email); email.Send(); allMailSent = true; } catch (Exception ex) { errorMsg = $"Exeption Caught on sending Email: {ex.Message}"; } return(allMailSent); }