public bool SendEmailToParent(SendEmailToParentDto sendEmailToParentDto) { try { if (sendEmailToParentDto == null) { throw new ArgumentNullException($"Dto is null"); } var teacher = _dbContext.Users.OfType <Teacher>() .FirstOrDefault(x => x.Id == sendEmailToParentDto.SenderId); if (teacher == null || _userManager.IsInRoleAsync(teacher, "Teacher").Result == false) { throw new InvalidOperationException("sender is not teacher"); } var student = _dbContext.Users.OfType <Student>().FirstOrDefault(x => x.Id == sendEmailToParentDto.StudentId); if (student == null || !_userManager.IsInRoleAsync(student, "Student").Result) { throw new InvalidOperationException("given user is not student"); } var mailMessage = new MailMessage(to: student.Parent.Email, subject: sendEmailToParentDto.Title, body: sendEmailToParentDto.Content, from: teacher.Email); _smtpClient.Send(mailMessage); return(true); } catch (Exception) { return(false); } }
public IActionResult SendEmailToParent(SendEmailToParentDto sendEmailToParentDto) { if (ModelState.IsValid) { _teacherService.SendEmailToParent(sendEmailToParentDto); return(RedirectToAction("SendEmailToParent")); } return(View()); }
public IActionResult SendEmailToParent(SendEmailToParentDto sendEmailToParentDto) { var teacher = _userManager.GetUserAsync(User).Result; sendEmailToParentDto.SenderId = teacher.Id; if (_teacherService.SendEmailToParent(sendEmailToParentDto)) { return(RedirectToAction("Index", "Student")); } return(View("Error")); }