176 lines
5.5 KiB
C#
176 lines
5.5 KiB
C#
using System.Net.Mail;
|
|
using Amazon.SimpleEmailV2.Model;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using MimeKit;
|
|
using Volo.Abp.BackgroundJobs;
|
|
using Volo.Abp.DependencyInjection;
|
|
using Volo.Abp.Emailing;
|
|
using Volo.Abp.MultiTenancy;
|
|
using Volo.Abp.Settings;
|
|
|
|
namespace Erp.Sender.Mail.AmazonSes;
|
|
|
|
/// <summary>
|
|
/// Used to send emails over Amazon SES SDK.
|
|
/// </summary>
|
|
[Dependency(ServiceLifetime.Transient, ReplaceServices = true)]
|
|
public class AmazonSesEmailSender : EmailSenderBase, IErpEmailSender, ITransientDependency
|
|
{
|
|
private readonly ILogger logger;
|
|
private readonly IAmazonSesClientFactory amazonSesClientFactory;
|
|
private readonly ISettingProvider settingProvider;
|
|
|
|
public AmazonSesEmailSender(
|
|
IEmailSenderConfiguration configuration,
|
|
IBackgroundJobManager backgroundJobManager,
|
|
ILogger<AmazonSesEmailSender> logger,
|
|
IAmazonSesClientFactory amazonSesClientFactory,
|
|
ISettingProvider settingProvider,
|
|
ICurrentTenant currentTenant
|
|
) : base(currentTenant, configuration, backgroundJobManager)
|
|
{
|
|
this.logger = logger;
|
|
this.amazonSesClientFactory = amazonSesClientFactory;
|
|
this.settingProvider = settingProvider;
|
|
}
|
|
|
|
public async Task QueueEmailAsync(
|
|
string to,
|
|
KeyValuePair<string, string>? sender,
|
|
dynamic? @params,
|
|
string textContent,
|
|
string? subject = null,
|
|
Dictionary<string, string>? attachments = null,
|
|
bool IsBodyHtml = false)
|
|
{
|
|
await QueueEmailAsync(new string[] { to }, sender, @params, textContent, subject, attachments, IsBodyHtml);
|
|
}
|
|
|
|
public async Task QueueEmailAsync(
|
|
string[] to,
|
|
KeyValuePair<string, string>? sender,
|
|
dynamic? @params,
|
|
string textContent,
|
|
string? subject = null,
|
|
Dictionary<string, string>? attachments = null,
|
|
bool IsBodyHtml = false)
|
|
{
|
|
if (!BackgroundJobManager.IsAvailable())
|
|
{
|
|
await SendEmailAsync(to, sender, @params, textContent, subject, attachments, IsBodyHtml);
|
|
return;
|
|
}
|
|
|
|
await BackgroundJobManager.EnqueueAsync(
|
|
new ErpBackgroundEmailSendingJobArgs
|
|
{
|
|
To = to,
|
|
Sender = sender,
|
|
Params = @params,
|
|
TextContent = textContent,
|
|
Subject = subject,
|
|
Attachments = attachments,
|
|
}
|
|
);
|
|
}
|
|
|
|
public async Task<EmailSendResult> SendEmailAsync(
|
|
string[] to,
|
|
KeyValuePair<string, string>? sender,
|
|
dynamic? @params,
|
|
string textContent,
|
|
string? subject = null,
|
|
Dictionary<string, string>? attachments = null,
|
|
bool IsBodyHtml = false)
|
|
{
|
|
var body = new BodyBuilder();
|
|
|
|
if (IsBodyHtml)
|
|
{
|
|
body.HtmlBody = textContent;
|
|
}
|
|
else
|
|
{
|
|
body.TextBody = textContent;
|
|
}
|
|
|
|
if (attachments != null)
|
|
{
|
|
foreach (var attachment in attachments)
|
|
{
|
|
body.Attachments.Add(attachment.Key, Convert.FromBase64String(attachment.Value));
|
|
}
|
|
}
|
|
|
|
var message = new MimeMessage();
|
|
|
|
sender ??= new KeyValuePair<string, string>(
|
|
await settingProvider.GetOrNullAsync(EmailSettingNames.DefaultFromDisplayName),
|
|
await settingProvider.GetOrNullAsync(EmailSettingNames.DefaultFromAddress));
|
|
|
|
message.From.Add(new MailboxAddress(sender.Value.Key, sender.Value.Value));
|
|
|
|
to.ToList().ForEach(t => message.To.Add(new MailboxAddress("", t)));
|
|
message.Subject = subject;
|
|
message.Body = body.ToMessageBody();
|
|
|
|
using var messageStream = new MemoryStream();
|
|
message.WriteTo(messageStream);
|
|
|
|
var request = new SendEmailRequest
|
|
{
|
|
Content = new EmailContent
|
|
{
|
|
Raw = new RawMessage
|
|
{
|
|
Data = messageStream
|
|
}
|
|
}
|
|
};
|
|
|
|
request.ConfigurationSetName = "ConfigSet"; // AWS mail tracking icin dogru configset adi girilmeli
|
|
|
|
using var client = await amazonSesClientFactory.GetAmazonSesClient();
|
|
var response = await client.SendEmailAsync(request);
|
|
var result = new EmailSendResult();
|
|
if (response == null)
|
|
{
|
|
result.Success = false;
|
|
result.ErrorMessage = "Mail not sent: Empty response";
|
|
}
|
|
else
|
|
{
|
|
if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
|
|
{
|
|
result.Success = true;
|
|
result.MessageId = response.MessageId;
|
|
}
|
|
else
|
|
{
|
|
result.Success = false;
|
|
result.ErrorMessage = $"Mail not sent. {response.HttpStatusCode}.";
|
|
//TODO: Inspect response
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public async Task<EmailSendResult> SendEmailAsync(
|
|
string to,
|
|
KeyValuePair<string, string>? sender,
|
|
dynamic? @params,
|
|
string textContent,
|
|
string? subject = null,
|
|
Dictionary<string, string>? attachments = null,
|
|
bool IsBodyHtml = false)
|
|
{
|
|
return await SendEmailAsync(new string[] { to }, sender, @params, textContent, subject, attachments, IsBodyHtml);
|
|
}
|
|
|
|
protected override async Task SendEmailAsync(MailMessage mail)
|
|
{
|
|
await SendEmailAsync(mail.To.Select(a => a.Address).ToArray(), null, null, mail.Body, mail.Subject, null, mail.IsBodyHtml);
|
|
}
|
|
}
|
|
|