Resolved #546: Initial implementation of "Create a service to send emails in background jobs"

pull/556/head
Halil ibrahim Kalkan 7 years ago
parent 22184fbc40
commit 4875d2e716

@ -55,6 +55,11 @@ namespace Volo.Abp.BackgroundJobs
return _jobConfigurationsByArgsType.Values.ToImmutableList();
}
public void AddJob<TJob>()
{
AddJob(typeof(TJob));
}
public void AddJob(Type jobType)
{
AddJob(new BackgroundJobConfiguration(jobType));

@ -19,6 +19,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Volo.Abp.BackgroundJobs.Abstractions\Volo.Abp.BackgroundJobs.Abstractions.csproj" />
<ProjectReference Include="..\Volo.Abp.Localization\Volo.Abp.Localization.csproj" />
<ProjectReference Include="..\Volo.Abp.Settings\Volo.Abp.Settings.csproj" />
<ProjectReference Include="..\Volo.Abp.VirtualFileSystem\Volo.Abp.VirtualFileSystem.csproj" />

@ -1,4 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.Emailing.Templates;
using Volo.Abp.Emailing.Templates.Virtual;
using Volo.Abp.Localization;
@ -27,6 +28,11 @@ namespace Volo.Abp.Emailing
options.FileSets.AddEmbedded<AbpEmailingModule>();
});
Configure<BackgroundJobOptions>(options =>
{
options.AddJob<BackgroundEmailSendingJob>();
});
context.Services.Configure<EmailTemplateOptions>(options =>
{
options.Templates

@ -0,0 +1,19 @@
using Volo.Abp.BackgroundJobs;
namespace Volo.Abp.Emailing
{
public class BackgroundEmailSendingJob : BackgroundJob<BackgroundEmailSendingJobArgs>
{
protected IEmailSender EmailSender { get; }
public BackgroundEmailSendingJob(IEmailSender emailSender)
{
EmailSender = emailSender;
}
public override void Execute(BackgroundEmailSendingJobArgs args)
{
EmailSender.Send(args.To, args.Subject, args.Body, args.IsBodyHtml);
}
}
}

@ -0,0 +1,21 @@
using System;
namespace Volo.Abp.Emailing
{
[Serializable]
public class BackgroundEmailSendingJobArgs
{
public string To { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
/// <summary>
/// Default: true.
/// </summary>
public bool IsBodyHtml { get; set; } = true;
//TODO: Add other properties and attachments
}
}

@ -2,6 +2,7 @@ using System;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.BackgroundJobs;
namespace Volo.Abp.Emailing
{
@ -10,15 +11,17 @@ namespace Volo.Abp.Emailing
/// </summary>
public abstract class EmailSenderBase : IEmailSender
{
public IEmailSenderConfiguration Configuration { get; }
protected IEmailSenderConfiguration Configuration { get; }
protected IBackgroundJobManager BackgroundJobManager { get; }
/// <summary>
/// Constructor.
/// </summary>
/// <param name="configuration">Configuration</param>
protected EmailSenderBase(IEmailSenderConfiguration configuration)
protected EmailSenderBase(IEmailSenderConfiguration configuration, IBackgroundJobManager backgroundJobManager)
{
Configuration = configuration;
BackgroundJobManager = backgroundJobManager;
}
public virtual async Task SendAsync(string to, string subject, string body, bool isBodyHtml = true)
@ -63,6 +66,19 @@ namespace Volo.Abp.Emailing
await SendEmailAsync(mail);
}
public virtual async Task QueueAsync(string to, string subject, string body, bool isBodyHtml = true)
{
await BackgroundJobManager.EnqueueAsync(
new BackgroundEmailSendingJobArgs
{
To = to,
Subject = subject,
Body = body,
IsBodyHtml = isBodyHtml
}
);
}
public virtual void Send(MailMessage mail, bool normalize = true)
{
if (normalize)

@ -47,5 +47,12 @@ namespace Volo.Abp.Emailing
/// If true, it sets sender address/name if it's not set before and makes mail encoding UTF-8.
/// </param>
Task SendAsync(MailMessage mail, bool normalize = true);
/// <summary>
/// Adds an email to queue to send via background jobs.
/// </summary>
Task QueueAsync(string to, string subject, string body, bool isBodyHtml = true);
//TODO: Add other Queue methods too. Problem: MailMessage is not serializable so can not be used in background jobs.
}
}

Loading…
Cancel
Save