private void SendEmails(EmailTemplate emailId, List <int> userIds, Dictionary <string, string> customKeys, List <int> coursesInAssignment, int assignmentId) { Email template = db.Emails.FirstOrDefault(e => e.emailId == (int)emailId); foreach (int userId in userIds) { //if user has completed this assigned activity then skip List <int?> coursesCompleted = db.Assignment_CoursesCompletedGet(assignmentId, userId).ToList(); IEnumerable <int?> coursesIncomplete = coursesInAssignment.Cast <int?>().Except(coursesCompleted); if (coursesIncomplete.Count() == 0) { continue; } //this user has not completed the assigned set... spam him/her User userInfo = db.Users.FirstOrDefault(u => u.userId == userId); string userEmail = userInfo.email; string body = template.body; string subject = template.subject; //handle name out of loop body = Utilities.ReplaceStringCaseInsensitive(body, "{FirstName}", userInfo.firstName); body = Utilities.ReplaceStringCaseInsensitive(body, "{LastName}", userInfo.lastName); foreach (KeyValuePair <string, string> code in customKeys) { body = Utilities.ReplaceStringCaseInsensitive(body, code.Key, code.Value); subject = Utilities.ReplaceStringCaseInsensitive(subject, code.Key, code.Value); } body = "<span style=\"font-family: Arial;font-size: 10pt;\">" + body + "</span>"; WriteMsg("Sending email " + emailId + " to " + userEmail); if (req.Url.Port == 443 || (req.Url.Port == 80 && req.Url.Host != "localhost")) { Utilities.SendEmail(ConfigurationManager.AppSettings.Get("DoNotReplyEmail"), userEmail, subject, body); } } }