Skip to content

Instantly share code, notes, and snippets.

@ngohungphuc
Created December 19, 2018 03:09
Show Gist options
  • Save ngohungphuc/1fe320aae7b1549c3c2ac0253d268415 to your computer and use it in GitHub Desktop.
Save ngohungphuc/1fe320aae7b1549c3c2ac0253d268415 to your computer and use it in GitHub Desktop.
using System.Threading.Tasks;
namespace AwesomeCMSCore.Modules.Scheduled
{
public interface IScheduledEmailService
{
Task SendEmailBackground();
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using AwesomeCMSCore.Modules.Email;
using AwesomeCMSCore.Modules.Entities.Entities;
using AwesomeCMSCore.Modules.Repositories;
using Hangfire;
using Microsoft.EntityFrameworkCore;
namespace AwesomeCMSCore.Modules.Scheduled
{
public class ScheduledEmailService: IScheduledEmailService
{
private readonly IEmailSender _emailSender;
private readonly IUnitOfWork _unitOfWork;
public ScheduledEmailService(IEmailSender emailSender, IUnitOfWork unitOfWork)
{
_emailSender = emailSender;
_unitOfWork = unitOfWork;
}
public async Task SendEmailBackground()
{
try
{
var emailList = await _unitOfWork.Repository<NewsLetter>().GetAllAsync();
if (emailList.Count > 0)
{
foreach (var email in emailList)
{
await _emailSender.SendEmailAsync(email.Email, "", null, EmailType.SubscriptionEmail);
}
}
}
catch (Exception ex)
{
throw;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment