Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions FluentEmail.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30011.22
# Visual Studio Version 17
VisualStudioVersion = 17.14.36429.23
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{6DC215BD-05EF-49A6-ADBE-8AE399952EEC}"
EndProject
Expand Down Expand Up @@ -43,6 +43,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentEmail.Liquid", "src\R
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentEmail.Liquid.Tests", "test\FluentEmail.Liquid.Tests\FluentEmail.Liquid.Tests.csproj", "{C8063CBA-D8F3-467A-A75C-63843F0DE862}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentEmail.SES", "src\Senders\FluentEmail.SES\FluentEmail.SES.csproj", "{86BE04B5-E78B-7808-FF7B-95510C6A1F74}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -97,6 +99,10 @@ Global
{C8063CBA-D8F3-467A-A75C-63843F0DE862}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C8063CBA-D8F3-467A-A75C-63843F0DE862}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C8063CBA-D8F3-467A-A75C-63843F0DE862}.Release|Any CPU.Build.0 = Release|Any CPU
{86BE04B5-E78B-7808-FF7B-95510C6A1F74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{86BE04B5-E78B-7808-FF7B-95510C6A1F74}.Debug|Any CPU.Build.0 = Debug|Any CPU
{86BE04B5-E78B-7808-FF7B-95510C6A1F74}.Release|Any CPU.ActiveCfg = Release|Any CPU
{86BE04B5-E78B-7808-FF7B-95510C6A1F74}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -117,6 +123,7 @@ Global
{0C7819AD-BC76-465D-9B2A-BE2DA75042F2} = {926C0980-31D9-4449-903F-3C756044C28A}
{17100F47-A555-4756-A25F-4F05EDAFA74E} = {12F031E5-8DDC-40A0-9862-8764A6E190C0}
{C8063CBA-D8F3-467A-A75C-63843F0DE862} = {47CB89AC-9615-4FA8-90DE-2D849935C36D}
{86BE04B5-E78B-7808-FF7B-95510C6A1F74} = {926C0980-31D9-4449-903F-3C756044C28A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {23736554-5288-4B30-9710-B4D9880BCF0B}
Expand Down
9 changes: 9 additions & 0 deletions src/Senders/FluentEmail.SES/FluenEmailSESOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace FluentEmail.SES
{
public class FluenEmailSESOptions
{
public string AccessKeyId { get; set; }
public string SecretAccessKey { get; set; }
public string RegionEndpoint { get; set; }
}
}
17 changes: 17 additions & 0 deletions src/Senders/FluentEmail.SES/FluentEmail.SES.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.SimpleEmailV2" Version="4.0.5.3" />
<PackageReference Include="Microsoft.Extensions.Options" Version="9.0.9" />
<PackageReference Include="MimeKit" Version="4.13.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\FluentEmail.Core\FluentEmail.Core.csproj" />
</ItemGroup>

</Project>
48 changes: 48 additions & 0 deletions src/Senders/FluentEmail.SES/FluentEmailSESBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using FluentEmail.Core.Interfaces;
using FluentEmail.SES;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;

namespace Microsoft.Extensions.DependencyInjection
{
public static class FluentEmailSESBuilderExtensions
{
/// <summary>
/// Adds the SES sender services.
/// </summary>
/// <param name="builder">The Fluent Email Service builder.</param>
/// <param name="sesClientOptions">The options to configure the SES client.</param>
/// <returns><see cref="FluentEmailServicesBuilder"/></returns>
public static FluentEmailServicesBuilder AddSESSender(this FluentEmailServicesBuilder builder, FluenEmailSESOptions sesClientOptions)
{
builder.Services.Configure<FluenEmailSESOptions>(options =>
{
options.AccessKeyId = sesClientOptions.AccessKeyId;
options.SecretAccessKey = sesClientOptions.SecretAccessKey;
options.RegionEndpoint = sesClientOptions.RegionEndpoint;
});

builder.Services.TryAddSingleton<ISender, SESSender>();
return builder;
}

/// <summary>
/// Adds the SES sender services.
/// </summary>
/// <param name="builder">The Fluent Email Service builder.</param>
/// <param name="sesClientOptions">The options to configure the SES client.</param>
/// <returns><see cref="FluentEmailServicesBuilder"/></returns>
public static FluentEmailServicesBuilder AddSESSender(this FluentEmailServicesBuilder builder, Action<IServiceProvider, FluenEmailSESOptions> sesClientOptions)
{
builder.Services.AddOptions<FluenEmailSESOptions>()
.Configure<IServiceProvider>((options, provider) =>
{
sesClientOptions(provider, options);
});

builder.Services.TryAddSingleton<ISender, SESSender>();
return builder;
}
}

}
155 changes: 155 additions & 0 deletions src/Senders/FluentEmail.SES/SESSender.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
using Amazon;
using Amazon.SimpleEmailV2;
using Amazon.SimpleEmailV2.Model;
using FluentEmail.Core;
using FluentEmail.Core.Interfaces;
using FluentEmail.Core.Models;
using Microsoft.Extensions.Options;
using MimeKit;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Attachment = FluentEmail.Core.Models.Attachment;

namespace FluentEmail.SES
{
public class SESSender : ISender
{
private readonly AmazonSimpleEmailServiceV2Client _emailClient;

public SESSender(IOptions<FluenEmailSESOptions> options)
{
var config = new AmazonSimpleEmailServiceV2Config()
{
RegionEndpoint = RegionEndpoint.GetBySystemName(options.Value.RegionEndpoint)
};

_emailClient = new AmazonSimpleEmailServiceV2Client(
options.Value.AccessKeyId,
options.Value.SecretAccessKey,
config);
}

public SendResponse Send(IFluentEmail email, CancellationToken? token = null)
{
return SendAsync(email, token).GetAwaiter().GetResult();
}

public async Task<SendResponse> SendAsync(IFluentEmail email, CancellationToken? token = null)
{
var response = new SendResponse();

if (token?.IsCancellationRequested ?? false)
{
response.ErrorMessages.Add("Message was cancelled by cancellation token.");
return response;
}

try
{
var message = new MimeMessage();

if (string.IsNullOrWhiteSpace(email.Data.Subject))
{
response.ErrorMessages.Add("Subject is missing.");
}

if (email.Data.ToAddresses.Any(a => !string.IsNullOrWhiteSpace(a.EmailAddress)))
{
message.To.AddRange(email.Data.ToAddresses.Select(ConvertAddress));
}

if (email.Data.CcAddresses.Any(a => !string.IsNullOrWhiteSpace(a.EmailAddress)))
{
message.Cc.AddRange(email.Data.CcAddresses.Select(ConvertAddress));
}

if (email.Data.BccAddresses.Any(a => !string.IsNullOrWhiteSpace(a.EmailAddress)))
{
message.Bcc.AddRange(email.Data.BccAddresses.Select(ConvertAddress));
}

if (email.Data.ReplyToAddresses.Any(a => !string.IsNullOrWhiteSpace(a.EmailAddress)))
{
message.ReplyTo.AddRange(email.Data.ReplyToAddresses.Select(ConvertAddress));
}

switch (email.Data.Priority)
{
case Priority.High:
message.Priority = MessagePriority.Urgent;
break;

case Priority.Normal:
// Do not set anything.
// Leave default values. It means Normal Priority.
break;

case Priority.Low:
message.Priority = MessagePriority.NonUrgent;
break;
}

message.From.Add(ConvertAddress(email.Data.FromAddress));
message.Subject = email.Data.Subject;

var bodyBuilder = new BodyBuilder();
if (!string.IsNullOrEmpty(email.Data.PlaintextAlternativeBody))
{
bodyBuilder.TextBody = email.Data.PlaintextAlternativeBody;
bodyBuilder.HtmlBody = email.Data.Body;
}
else if (!email.Data.IsHtml)
{
bodyBuilder.TextBody = email.Data.Body;
}
else
{
bodyBuilder.HtmlBody = email.Data.Body;
}

foreach (var attachment in email.Data.Attachments ?? new List<Attachment>())
{
if (attachment.Data != null)
{
bodyBuilder.Attachments.Add(
attachment.Filename,
attachment.Data,
MimeKit.ContentType.Parse(attachment.ContentType ?? "application/octet-stream")
);
}
}

message.Body = bodyBuilder.ToMessageBody();

using var messageStream = new MemoryStream();
await message.WriteToAsync(messageStream);
messageStream.Seek(0, SeekOrigin.Begin);

var emailResponse = await _emailClient.SendEmailAsync(new SendEmailRequest
{
Content = new EmailContent
{
Raw = new RawMessage()
{
Data = messageStream
}
}
});

response.MessageId = emailResponse.MessageId;
}
catch (Exception ex)
{
response.ErrorMessages.Add(ex.Message);
}

return response;
}

private MailboxAddress ConvertAddress(Address address) => new MailboxAddress(address.Name, address.EmailAddress);
}
}
1 change: 1 addition & 0 deletions test/FluentEmail.Core.Tests/FluentEmail.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\FluentEmail.Core\FluentEmail.Core.csproj" />
<ProjectReference Include="..\..\src\Senders\FluentEmail.SES\FluentEmail.SES.csproj" />
<ProjectReference Include="..\..\src\Senders\FluentEmail.Smtp\FluentEmail.Smtp.csproj" />
<ProjectReference Include="..\..\src\Senders\FluentEmail.MailKit\FluentEmail.MailKit.csproj" />
<ProjectReference Include="..\..\src\Senders\FluentEmail.Graph\FluentEmail.Graph.csproj" />
Expand Down
Loading