public void SendVotingAlert(int electionId, IJobCancellationToken cancellationToken) { Election election = GetElection(electionId); int eligibleForVoting = CountEligible(election, entry => entry.CanVote); int alreadyVoted = Db.StudentVoteRecords .Where(record => record.Position.ElectionId == election.Id) .Select(record => record.Username) .Distinct() .Count(); float participationRatio = alreadyVoted / (float)eligibleForVoting; // Check that we actually need to send the alert if (!election.Voting.ShouldSendAlert(participationRatio)) { return; } cancellationToken.ThrowIfCancellationRequested(); StudentSupportEmail email = GenerateAlertEmail(election, participationRatio); Db.StudentSupportEmails.Add(email); Db.SaveChanges(); ScheduleJobsInTransaction( _userRepository.GetAllStudentSupport(), (user, jobClient) => jobClient.Enqueue <StudentSupportMailerJob>( mailer => mailer.SendSingleEmail(email.Id, user.InternalEmail, user.Fullname) ) ); }
public void SendSingleEmail(Guid emailId, string recipientAddress, string recipientName) { StudentSupportEmail email = _db.StudentSupportEmails.Find(emailId); if (email == null) { throw new ArgumentOutOfRangeException(nameof(emailId), "No student support email with such id"); } _smtpClient.Send(new MailMessage { Subject = email.Subject, Body = email.Body, IsBodyHtml = false, From = EmailHelpers.DefaultSender, To = { new MailAddress(recipientAddress, recipientName) } }); }