Skip to main content

AWS Email Sending & Templating with .NET

Required Packages

dotnet add package AWSSDK.SimpleEmail AWSSDK.Extensions.NETCore.Setup
dotnet add package Mjml.Net mustache-sharp
Startup.cs
var awsOptions = Configuration.GetAWSOptions();

services.AddAWSService<IAmazonSimpleEmailService>();
Template.mjml
<mjml>
<mj-head>
<mj-font name="Quicksand" href="https://fonts.googleapis.com/css?family=Quicksand" />
<mj-title>{{AppTypeName}} Contact</mj-title>
<mj-attributes>
<mj-all font-family="Quicksand,helvetica" />
</mj-attributes>
</mj-head>
<mj-body background-color="#ffffff">
<mj-section background-color="#ffffff">
<mj-column>
<mj-image width="150px" align="center" src="https://cdn.example.com/logo@2x.png" container-background-color="#ffffff" />
</mj-column>
</mj-section>
<mj-section background-color="#ffffff">
<mj-column>
<mj-text font-size="24px" color="#454FD0">{{AppTypeName}} Contact</mj-text>
<mj-divider border-color="#454FD0"></mj-divider>
<mj-text font-size="18px" color="#38428B">From: {{FromName}}</mj-text>
<mj-text font-size="18px" color="#38428B">Email Address: {{FromEmail}}</mj-text>
<mj-text font-size="18px" color="#38428B">Organisation: {{FromOrganization}}</mj-text>
<mj-text font-size="18px" color="#38428B">Message: {{Message}}</mj-text>
</mj-column>
</mj-section>
<mj-section background-color="#ffffff">
<mj-column>
<mj-divider border-color="#454FD0"></mj-divider>
<mj-text font-size="18px" color="#38428B">At: {{SentAt}}</mj-text>
</mj-column>
</mj-section>
</mj-body>
</mjml>
SendEmail.cs
try  
{
var rawMjml = ResourceExtensions.ReadManifestData<SendContactHandler>("Template.mjml");

var mjmlRenderer = new MjmlRenderer();
var mjmlOptions = new MjmlOptions
{
Beautify = false,
Minify = true,
KeepComments = false
};
var renderedMjml = mjmlRenderer.Render(rawMjml, mjmlOptions);
if (renderedMjml.Errors.Any()) _logger.Error("Error rendering MJML: {@Errors}", renderedMjml.Errors);
var renderedMjmlHtml = renderedMjml.Html;

var compiler = new Mustache.FormatCompiler();
var generator = compiler.Compile(renderedMjmlHtml);
var mergedHtml = generator.Render(new
{
FromId = user.UserId,
FromName = user.FirstName,
FromEmail = user.EmailAddress,
FromOrganization = user.Organization.Name,
Message = contactMessage.Message,
SentAt = contactMessage.CreatedAt
});

var senderEmail = "sender@example.com";
var recipientEmail = "recipient@example.com";
var emailSubject = "Contact";

var emailResponse = await _emailService.SendEmailAsync(new SendEmailRequest
{
Source = $"{senderEmail}",
SourceArn = $"arn:aws:ses:{awsRegion}:{awsAccount}:identity/{senderEmail}",
Destination = new Destination
{
ToAddresses = new List<string>
{
recpientEmail
}
},
Message = new Message
{
Subject = new Amazon.SimpleEmail.Model.Content(emailSubject),
Body = new Body
{
Html = new Amazon.SimpleEmail.Model.Content(mergedHtml)
}
}
}, cancellationToken);
return emailResponse.HttpStatusCode == HttpStatusCode.OK;
}
catch (Exception ex)
{
_logger.Error(ex, "Error in SendContactHandler");
}