/// <summary> /// Creates the automailer tasks for sending emails /// This should be a daily task /// </summary> /// <returns></returns> public async Task CreateMailTask() { //gets the list of active campaigns for processing List <CampaignSetting> campaignSettings = _automailerContext.CampaignSettings.Where(x => x.Active == true).ToList(); foreach (CampaignSetting campaignSetting in campaignSettings) { CampaignTrackingModel trackingModel = CreateAutomailerTrackingModels(campaignSetting); AutomailerTask automailerTask = AutomailerTask.Create(trackingModel.AutomailerModels); _automailerContext.AutomailerTasks.Add(automailerTask); _automailerContext.CampaignTrackings.AddRange(trackingModel.CampaignTrackings); await _automailerContext.SaveChangesAsync(); } }
public IActionResult GetEmailTaskById(Guid id) { try { AutomailerTask automailerTask = _automailerContext.AutomailerTasks .Include(x => x.AutomailerModels) .FirstOrDefault(x => x.AutomailerTaskId == id); return(Ok(automailerTask)); } catch (Exception ex) { _logger.LogError(ex.Message); return(Problem(ex.Message)); } }
public async Task SendEmail(Guid id) { AutomailerTask automailerTask = _automailerContext.AutomailerTasks .Include(x => x.AutomailerModels) .FirstOrDefault(x => x.AutomailerTaskId == id); if (automailerTask != null) { foreach (AutomailerModel item in automailerTask.AutomailerModels) { await _fluentEmailFactory.Create() .To(item.Email) .Subject(item.Subject) .UsingTemplate(item.Content, item) .SendAsync(); } automailerTask.IsCompleted = true; _automailerContext.AutomailerTasks.Update(automailerTask); await _automailerContext.SaveChangesAsync(); } }