public IActionResult SendInvestigationEmail([FromBody] InvestiationEmailRequest emailreq) { if (Common.IsValidEmail(emailreq.RecipientEmail) != true) { return(BadRequest("Invalid Email")); } if (emailreq.Subject.Trim() == "" || emailreq.Message.Trim() == "" || emailreq.AppUserID == 0 || emailreq.IndexFromWorkTable == 0) { return(BadRequest("Invalid Request, Subject, Body or User cannot be empty ")); } // From User var thisUser = _context.AppUser .Where(x => x.AppUserID == emailreq.AppUserID).FirstOrDefault(); if (thisUser == null) { return(BadRequest("User does not Exist ")); } var SentFrom = thisUser.Email; // Investigation Entry var thisInvestigation = _context.Investigation .Where(x => x.InvestigationID == emailreq.IndexFromWorkTable).FirstOrDefault(); if (thisInvestigation == null) { return(BadRequest("Investigation Record does not Exist ")); } var SMTPServer = _configuration.GetSection("EmailSettings:SMTPServer").Value; var SMTPPort = _configuration.GetSection("EmailSettings:SMTPPort").Value; var InvestigationToEmail = _configuration.GetSection("EmailSettings:InvestigationToEmail").Value; var InvestigationCCEmail = _configuration.GetSection("EmailSettings:InvestigationCCEmail").Value; var InvestigationBCCEmail = _configuration.GetSection("EmailSettings:InvestigationBCCEmail").Value; var DevEmailAddress = _configuration.GetSection("EmailSettings:DevEmailAddress").Value; var StatusOrErrorMessage = _configuration.GetSection("EmailSettings:StatusOrErrorMessage").Value; var ResearchersEmailAddress = _configuration.GetSection("EmailSettings:ResearchersEmailAddress").Value; var InvestigationEmailSendFromEmailAddress = _configuration.GetSection("EmailSettings:InvestigationEmailSendFromEmailAddress").Value; var IsDevelopment = _configuration.GetSection("EmailSettings:Development").Value; // TO (recipients) var SentTo = emailreq.RecipientEmail; if (InvestigationToEmail != "") { SentTo += ";" + InvestigationToEmail; } if (InvestigationCCEmail != "") { SentTo += ";" + InvestigationCCEmail; } if (InvestigationBCCEmail != "") { SentTo += ";" + InvestigationBCCEmail; } var InvestigationEmailEntry = new InvestigationEmails(); var SentDateUTC = DateTime.UtcNow; var DateCreatedUTC = DateTime.UtcNow; var LastUpdatedUTC = DateTime.UtcNow; var CreatedBy = emailreq.AppUserID.ToString(); var UpdatedBy = emailreq.AppUserID.ToString(); var Subject = emailreq.Subject; var EmailBody = emailreq.Message; // Log Entry in email table InvestigationEmailEntry.InvestigationID = emailreq.IndexFromWorkTable; InvestigationEmailEntry.AppUserID = emailreq.AppUserID; InvestigationEmailEntry.SentTo = SentTo; InvestigationEmailEntry.SentFrom = SentFrom; InvestigationEmailEntry.Subject = Subject; InvestigationEmailEntry.EmailBody = EmailBody; InvestigationEmailEntry.StatusOrErrorMessage = StatusOrErrorMessage; InvestigationEmailEntry.SentDateUTC = SentDateUTC; InvestigationEmailEntry.CreatedBy = CreatedBy; InvestigationEmailEntry.DateCreatedUTC = DateCreatedUTC; InvestigationEmailEntry.UpdatedBy = UpdatedBy; InvestigationEmailEntry.LastUpdatedUTC = LastUpdatedUTC; _context.InvestigationEmails.Add(InvestigationEmailEntry); // Log Activity var thisActivity = new InvestigationActivity(); thisActivity.InvestigationID = thisInvestigation.InvestigationID; thisActivity.ActivityTypeID = 8; //Email Sent Activity Type thisActivity.AppUserID = thisUser.AppUserID; thisActivity.FromValue = ""; thisActivity.ToValue = "Sent Email"; thisActivity.CreatedBy = CreatedBy; thisActivity.DateCreatedUTC = DateCreatedUTC; thisActivity.UpdatedBy = UpdatedBy; thisActivity.LastUpdatedUTC = LastUpdatedUTC; _context.InvestigationActivity.Add(thisActivity); ReturnData retValue; retValue = _context.SaveData(); if (retValue.Message != "Success") { return(Json(retValue)); } // remove after SentFrom = "*****@*****.**"; SentTo = "*****@*****.**"; var emailSent = Common.SendMail(SMTPServer, SentFrom, SentTo, "", "", Subject, EmailBody, _logger); if (emailSent != "Success") { return(BadRequest(emailSent)); } return(Ok()); }
public object SendInvestigationEmail(InvestiationEmailRequest emailrequest, IConfiguration configuration) { if (Helper.ValidEmail(emailrequest.RecipientEmail) == false) { return(null); } if (emailrequest.Subject.Trim() == "" || emailrequest.Message.Trim() == "" || emailrequest.AppUserID == 0 || emailrequest.IndexFromWorkTable == 0) { return(null); } var fromEditorialUser = _context.AppUser.Where(x => x.AppUserID == emailrequest.AppUserID).FirstOrDefault(); if (fromEditorialUser == null) { return(null); } var SentFrom = fromEditorialUser.Email; // Investigation Entry var thisInvestigation = _context.Investigation .Where(x => x.InvestigationID == emailrequest.IndexFromWorkTable).FirstOrDefault(); if (thisInvestigation == null) { return(null); } var SMTPServer = configuration.GetSection("EmailSettings:SMTPServer").Value; var SMTPPort = configuration.GetSection("EmailSettings:SMTPPort").Value; var InvestigationToEmail = configuration.GetSection("EmailSettings:ToEmail").Value; var InvestigationCCEmail = configuration.GetSection("EmailSettings:CCEmail").Value; var InvestigationBCCEmail = configuration.GetSection("EmailSettings:BCCEmail").Value; var DevEmailAddress = configuration.GetSection("EmailSettings:DevEmailAddress").Value; var StatusOrErrorMessage = configuration.GetSection("EmailSettings:StatusOrErrorMessage").Value; var ResearchersEmailAddress = configuration.GetSection("EmailSettings:ResearchersEmailAddress").Value; var InvestigationEmailSendFromEmailAddress = configuration.GetSection("EmailSettings:SendFromEmailAddress").Value; var IsDevelopment = configuration.GetSection("EmailSettings:Development").Value; // TO (recipients) var SentTo = emailrequest.RecipientEmail; if (InvestigationToEmail != "") { SentTo += ";" + InvestigationToEmail; } if (InvestigationCCEmail != "") { SentTo += ";" + InvestigationCCEmail; } if (InvestigationBCCEmail != "") { SentTo += ";" + InvestigationBCCEmail; } var InvestigationEmailEntry = new InvestigationEmails(); var SentDateUTC = DateTime.UtcNow; var DateCreatedUTC = DateTime.UtcNow; var LastUpdatedUTC = DateTime.UtcNow; var CreatedBy = emailrequest.AppUserID.ToString(); var UpdatedBy = emailrequest.AppUserID.ToString(); var Subject = emailrequest.Subject; var EmailBody = emailrequest.Message; InvestigationEmailEntry.InvestigationID = emailrequest.IndexFromWorkTable; InvestigationEmailEntry.AppUserID = emailrequest.AppUserID; InvestigationEmailEntry.SentTo = SentTo; InvestigationEmailEntry.SentFrom = SentFrom; InvestigationEmailEntry.Subject = Subject; InvestigationEmailEntry.EmailBody = EmailBody; InvestigationEmailEntry.StatusOrErrorMessage = StatusOrErrorMessage; InvestigationEmailEntry.SentDateUTC = SentDateUTC; InvestigationEmailEntry.CreatedBy = CreatedBy; InvestigationEmailEntry.DateCreatedUTC = DateCreatedUTC; InvestigationEmailEntry.UpdatedBy = UpdatedBy; InvestigationEmailEntry.LastUpdatedUTC = LastUpdatedUTC; _context.InvestigationEmails.Add(InvestigationEmailEntry); // Log Activity var thisActivity = new InvestigationActivity { InvestigationID = thisInvestigation.InvestigationID, ActivityTypeID = 8, //Email Sent Activity Type AppUserID = fromEditorialUser.AppUserID, FromValue = "", ToValue = "Sent Email", CreatedBy = CreatedBy, DateCreatedUTC = DateCreatedUTC, UpdatedBy = UpdatedBy, LastUpdatedUTC = LastUpdatedUTC }; _context.SaveChanges(); _context.InvestigationActivity.Add(thisActivity); // dummy, comment/delete when ready SentFrom = "*****@*****.**"; SentTo = "*****@*****.**"; Helper.SendMail(SMTPServer, SentFrom, SentTo, "", "", Subject, EmailBody); return(EmailBody); }
public IActionResult SendInvestigationEmail([FromBody] InvestiationEmailRequest emailrequest) { var result = _repository.SendInvestigationEmail(emailrequest, _configuration); return(Helper.CheckResult(result)); }