public IEnumerable <MassEmailRecipient> GetRecipients(MassEmailRecipientArgs args) { var list = new List <MassEmailRecipient>(); var mgr = new GroupEmailManager(Session); if (args.Privs > 0) { list.AddRange(mgr.GetEmailListByPrivilege(args.Privs)); } if (args.Communities > 0) { list.AddRange(mgr.GetEmailListByCommunity(args.Communities)); } if (args.Manager > 0) { list.AddRange(mgr.GetEmailListByManagerID(args.Manager)); } if (args.Tools != null && args.Tools.Count() > 0) { list.AddRange(mgr.GetEmailListByTools(args.Tools.ToArray())); } if (args.InLab != null && args.InLab.Count() > 0) { list.AddRange(mgr.GetEmailListByInLab(args.InLab.ToArray())); } var comparer = new MassEmailRecipientComparer(); var invalid = Session.Query <InvalidEmailList>().Where(x => x.IsActive).Select(x => x.EmailAddress).ToArray(); var result = list.Distinct(comparer).Where(x => !invalid.Contains(x.Email)).ToList(); return(result); }
public SendMessageArgs CreateSendMessageArgs(MassEmailSendArgs args) { if (string.IsNullOrEmpty(args.Group)) { throw new Exception("Group must not be null or empty."); } if (args.Values == null || args.Values.Count() == 0) { throw new Exception("Values must not be null or empty."); } if (string.IsNullOrEmpty(args.Caller)) { throw new Exception("Caller is required."); } if (string.IsNullOrEmpty(args.From)) { throw new Exception("From address is required."); } if (string.IsNullOrEmpty(args.DisplayName)) { throw new Exception("Display name is required."); } if (string.IsNullOrEmpty(args.Subject)) { throw new Exception("Subject is required."); } if (string.IsNullOrEmpty(args.Body)) { throw new Exception("Body is required."); } // an array of file paths var attachments = GetAttachments(args.Attachments); var gs = GlobalSettings.Current; string footer = "\n\n--------------------------------------------------\nThis email has been sent to the following group(s) : " + GetGroup(args) + "."; footer += $"\nYou are receiving this email message because you are associated with the {gs.CompanyName}.\nTo unsubscribe, please go to:\nhttp://ssel-sched.eecs.umich.edu/sselonline/Unsubscribe.aspx"; SendMessageArgs sma = new SendMessageArgs { ClientID = args.ClientID, Caller = args.Caller, Subject = args.Subject, Body = args.Body + footer, From = args.From, DisplayName = args.DisplayName, Attachments = attachments, IsHtml = false }; var recipients = new List <MassEmailRecipient>(); var mgr = new GroupEmailManager(Session); switch (args.Group) { case Groups.Privilege: recipients.AddRange(mgr.GetEmailListByPrivilege(Combine(args.Values))); break; case Groups.Community: recipients.AddRange(mgr.GetEmailListByCommunity(Combine(args.Values))); break; case Groups.Manager: recipients.AddRange(mgr.GetEmailListByManagerID(args.Values.First())); break; case Groups.Tools: recipients.AddRange(mgr.GetEmailListByTools(args.Values.ToArray())); break; case Groups.InLab: recipients.AddRange(mgr.GetEmailListByInLab(args.Values.ToArray())); break; default: throw new NotImplementedException($"Unknown group: {args.Group}"); } if (recipients.Count > 0) { var to = new List <string>(); var cc = new List <string>(); var bcc = new List <string>(); int staffCount = 0; int totalCount = 0; foreach (var recip in recipients) { if (recip.IsStaff) { // we have to expose staff member's emails, so adding to To (not Bcc) AddRecipient(to, recip.Email); staffCount++; } else { AddRecipient(bcc, recip.Email); } totalCount++; } if (!string.IsNullOrEmpty(args.CC)) { AddRecipients(cc, args.CC.Split(',')); } IClient client = Client.GetClient(args.ClientID); bool isStaff = client.HasPriv(ClientPrivilege.Staff); bool recipientsIncludeNonStaff = staffCount != totalCount; if (isStaff && recipientsIncludeNonStaff) { // messages only sent here if staff is sending email to non-staff AddRecipient(bcc, "*****@*****.**"); } // all messages are sent here AddRecipient(bcc, "*****@*****.**"); // apparently we always send to the from address also AddRecipient(to, args.From); sma.To = to.Count == 0 ? null : to; sma.Bcc = bcc.Count == 0 ? null : bcc; sma.Cc = cc.Count == 0 ? null : cc; } return(sma); }