Skip to content

Commit

Permalink
attachments + status
Browse files Browse the repository at this point in the history
  • Loading branch information
coronabytes committed Jan 18, 2024
1 parent b7e6d08 commit d66f83b
Show file tree
Hide file tree
Showing 21 changed files with 221 additions and 69 deletions.
2 changes: 1 addition & 1 deletion Core.Email.Abstractions/Core.Email.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions Core.Email.Abstractions/CoreEmailAttachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

public class CoreEmailAttachment
{
public string Name { get; set; } = string.Empty;
public string ContentType { get; set; } = string.Empty;
public byte[] Content { get; set; } = Array.Empty<byte>();
}
3 changes: 3 additions & 0 deletions Core.Email.Abstractions/CoreEmailStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

public class CoreEmailStatus
{
public Guid Id { get; set; }
public bool IsSuccess { get; set; }
public string Error { get; set; } = string.Empty;
}
2 changes: 1 addition & 1 deletion Core.Email.Abstractions/ICoreEmail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public interface ICoreEmail
{
public Task SendAsync(CoreEmailMessage message, CancellationToken cancellationToken = default);
public Task<CoreEmailStatus> SendAsync(CoreEmailMessage message, CancellationToken cancellationToken = default);
}
5 changes: 4 additions & 1 deletion Core.Email.Abstractions/ICoreEmailProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
public interface ICoreEmailProvider
{
public string Name { get; }
public Task SendBatchAsync(List<CoreEmailMessage> messages, CancellationToken cancellationToken = default);
public long MaxSize { get; }

public Task<List<CoreEmailStatus>> SendBatchAsync(List<CoreEmailMessage> messages,
CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
Expand Down
29 changes: 22 additions & 7 deletions Core.Email.Provider.Mailjet/MailjetProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace Core.Email.Provider.Mailjet;

internal class MailjetProvider : ICoreEmailProvider
{
private readonly Options _options = new();
private readonly MailjetClient _mailjet;
private readonly Options _options = new();

public MailjetProvider(IConfiguration configuration, [ServiceKey] string key)
{
Expand All @@ -19,22 +19,37 @@ public MailjetProvider(IConfiguration configuration, [ServiceKey] string key)

public string Name => "Mailjet";

public async Task SendBatchAsync(List<CoreEmailMessage> messages, CancellationToken cancellationToken = default)
public long MaxSize => 15 * 1024 * 1024;

public async Task<List<CoreEmailStatus>> SendBatchAsync(List<CoreEmailMessage> messages,
CancellationToken cancellationToken = default)
{
await _mailjet.SendTransactionalEmailsAsync(messages.Select(x => new TransactionalEmail
var res = await _mailjet.SendTransactionalEmailsAsync(messages.Select(x => new TransactionalEmail
{
CustomID = x.Id.ToString("N"),
To = x.To.Select(y => new SendContact(y)).ToList(),
Cc = x.Cc.Select(y => new SendContact(y)).ToList(),
Bcc = x.Bcc.Select(y => new SendContact(y)).ToList(),
Subject = x.Subject,
TextPart = x.TextBody,
HTMLPart = x.HtmlBody
}));
HTMLPart = x.HtmlBody,
Attachments = x.Attachments
.Select(y => new Attachment(y.Name, y.ContentType, Convert.ToBase64String(y.Content)))
.ToList()
})).ConfigureAwait(false);

return res.Messages.Select(x => new CoreEmailStatus
{
Id = Guid.Parse(x.CustomID),
IsSuccess = x.Errors.Count == 0,
Error = string.Join("\n", x.Errors.Select(y => y.ErrorMessage))
}).ToList();
}

[Serializable]
private class Options
{
public string ApiKey { get; } = string.Empty;
public string ApiSecret { get; } = string.Empty;
public string ApiKey { get; set; } = string.Empty;
public string ApiSecret { get; set; } = string.Empty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
Expand Down
22 changes: 19 additions & 3 deletions Core.Email.Provider.Postmark/PostmarkProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ public PostmarkProvider(IConfiguration configuration, [ServiceKey] string key)

public string Name => "Postmark";

public async Task SendBatchAsync(List<CoreEmailMessage> messages, CancellationToken cancellationToken = default)
public long MaxSize => 10 * 1024 * 1024;

public async Task<List<CoreEmailStatus>> SendBatchAsync(List<CoreEmailMessage> messages,
CancellationToken cancellationToken = default)
{
var res = await _postmark.SendMessagesAsync(messages.Select(x => new PostmarkMessage
{
Expand All @@ -29,8 +32,21 @@ public async Task SendBatchAsync(List<CoreEmailMessage> messages, CancellationTo
Subject = x.Subject,
TextBody = x.TextBody,
HtmlBody = x.HtmlBody,
MessageStream = _options.MessageStream
}));
MessageStream = _options.MessageStream,
Attachments = x.Attachments.Select(y => new PostmarkMessageAttachment
{
Content = Convert.ToBase64String(y.Content),
ContentType = y.ContentType,
Name = y.Name
}).ToList()
})).ConfigureAwait(false);

return res.Select(x => new CoreEmailStatus
{
Id = x.MessageID, // TODO: match order?
IsSuccess = x.Status == PostmarkStatus.Success,
Error = x.Message
}).ToList();
}

[Serializable]
Expand Down
2 changes: 1 addition & 1 deletion Core.Email.Provider.SES/Core.Email.Provider.SES.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
Expand Down
75 changes: 54 additions & 21 deletions Core.Email.Provider.SES/SimpleEmailServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,68 @@ public SimpleEmailServiceProvider(IConfiguration configuration, [ServiceKey] str

public string Name => "SES";

public async Task SendBatchAsync(List<CoreEmailMessage> messages, CancellationToken cancellationToken = default)
public long MaxSize => 40 * 1024 * 1024;

public async Task<List<CoreEmailStatus>> SendBatchAsync(List<CoreEmailMessage> messages,
CancellationToken cancellationToken = default)
{
var list = new List<CoreEmailStatus>();

foreach (var message in messages)
{
var m = new MimeMessage();
m.From.Add(new MailboxAddress("", message.From));
try
{
var m = new MimeMessage();
m.From.Add(new MailboxAddress("", message.From));

foreach (var to in message.To)
m.To.Add(new MailboxAddress(string.Empty, to));
foreach (var to in message.To)
m.To.Add(new MailboxAddress(string.Empty, to));

foreach (var cc in message.Cc)
m.Cc.Add(new MailboxAddress(string.Empty, cc));
foreach (var cc in message.Cc)
m.Cc.Add(new MailboxAddress(string.Empty, cc));

foreach (var bcc in message.Bcc)
m.Bcc.Add(new MailboxAddress(string.Empty, bcc));
foreach (var bcc in message.Bcc)
m.Bcc.Add(new MailboxAddress(string.Empty, bcc));

m.Subject = message.Subject;
m.Body = new BodyBuilder
{
HtmlBody = message.HtmlBody,
TextBody = message.TextBody
}.ToMessageBody();
m.Subject = message.Subject;

var bodyBuilder = new BodyBuilder
{
HtmlBody = message.HtmlBody,
TextBody = message.TextBody
};

using var stream = new MemoryStream();
await m.WriteToAsync(stream, cancellationToken);
stream.Position = 0;
foreach (var attachment in message.Attachments)
bodyBuilder.Attachments.Add(attachment.Name, attachment.Content,
ContentType.Parse(attachment.ContentType));

m.Body = bodyBuilder.ToMessageBody();

using var stream = new MemoryStream();
await m.WriteToAsync(stream, cancellationToken);
stream.Position = 0;

var res = await _ses
.SendRawEmailAsync(new SendRawEmailRequest(new RawMessage(stream)), cancellationToken)
.ConfigureAwait(false);

list.Add(new CoreEmailStatus
{
Id = message.Id,
IsSuccess = (int)res.HttpStatusCode >= 200 && (int)res.HttpStatusCode < 300,
Error = string.Empty // TODO: ?
});
}
catch (Exception e)
{
list.Add(new CoreEmailStatus
{
Id = message.Id,
IsSuccess = false,
Error = e.Message
});
}

await _ses.SendRawEmailAsync(new SendRawEmailRequest(new RawMessage(stream)), cancellationToken);
}
return list;
}

[Serializable]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using Core.Email.Abstractions;
using Core.Email.Provider.SES;
using Microsoft.Extensions.DependencyInjection;

namespace Core.Email.Provider.SES;

public static class SendGridProviderExtensions
public static class SimpleEmailServiceProviderExtensions
{
public static void AddSimpleEmailServiceProvider(this IServiceCollection collection, string? key = null)
{
Expand Down
2 changes: 1 addition & 1 deletion Core.Email.Provider.SMTP/Core.Email.Provider.SMTP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
Expand Down
68 changes: 56 additions & 12 deletions Core.Email.Provider.SMTP/SmtpProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,71 @@ public SmtpProvider(IConfiguration configuration, [ServiceKey] string key)
}

public string Name => "SMTP";
public long MaxSize => 0;

public async Task SendBatchAsync(List<CoreEmailMessage> messages, CancellationToken cancellationToken = default)
public async Task<List<CoreEmailStatus>> SendBatchAsync(List<CoreEmailMessage> messages,
CancellationToken cancellationToken = default)
{
using var client = new SmtpClient();

await client.ConnectAsync(_options.Host, _options.Port,
_options.Tls ? SecureSocketOptions.StartTls : SecureSocketOptions.Auto, cancellationToken);
await client.AuthenticateAsync(_options.Username, _options.Password, cancellationToken);
_options.Tls ? SecureSocketOptions.StartTls : SecureSocketOptions.Auto, cancellationToken)
.ConfigureAwait(false);
await client.AuthenticateAsync(_options.Username, _options.Password, cancellationToken).ConfigureAwait(false);

var list = new List<CoreEmailStatus>();

foreach (var message in messages)
{
var m = new MimeMessage();
m.From.Add(new MailboxAddress("", message.From));
m.To.Add(new MailboxAddress("", message.To.FirstOrDefault()));
m.Subject = message.Subject;
m.Body = new TextPart("plain") { Text = message.TextBody };
try
{
var m = new MimeMessage();
m.From.Add(new MailboxAddress(string.Empty, message.From));

foreach (var to in message.To)
m.To.Add(new MailboxAddress(string.Empty, to));

foreach (var cc in message.Cc)
m.Cc.Add(new MailboxAddress(string.Empty, cc));

foreach (var bcc in message.Bcc)
m.Bcc.Add(new MailboxAddress(string.Empty, bcc));

m.Subject = message.Subject;

var bodyBuilder = new BodyBuilder
{
HtmlBody = message.HtmlBody,
TextBody = message.TextBody
};

foreach (var attachment in message.Attachments)
bodyBuilder.Attachments.Add(attachment.Name, attachment.Content,
ContentType.Parse(attachment.ContentType));

m.Body = bodyBuilder.ToMessageBody();

var res = await client.SendAsync(m, cancellationToken).ConfigureAwait(false);

list.Add(new CoreEmailStatus
{
Id = message.Id,
IsSuccess = true, // TODO: ?
Error = res
});
}
catch (Exception e)
{
list.Add(new CoreEmailStatus
{
Id = message.Id,
IsSuccess = false,
Error = e.Message
});
}

await client.SendAsync(m, cancellationToken);
}
await client.DisconnectAsync(true, cancellationToken).ConfigureAwait(false);

await client.DisconnectAsync(true, cancellationToken);
return list;
}

[Serializable]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit d66f83b

Please sign in to comment.