Skip to content

Commit

Permalink
Merge pull request #428 from Laixer/feature/email-service
Browse files Browse the repository at this point in the history
Feature/email service
  • Loading branch information
yorickdewid authored Jan 18, 2021
2 parents 76740e6 + 0cbf104 commit e7bda31
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/FunderMaps.AspNetCore/FunderMaps.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/FunderMaps.Core/FunderMaps.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="5.0.0" />
<PackageReference Include="Scriban" Version="3.2.2" />
<PackageReference Include="Scriban" Version="3.3.3" />
</ItemGroup>

</Project>
60 changes: 24 additions & 36 deletions src/FunderMaps.Infrastructure/Email/EmailService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,29 @@
namespace FunderMaps.Infrastructure.Email
{
// FUTURE: Catch ex.
// FUTURE: Turn this into a factory
// FUTURE: We can configure much more and we will in a next release.
// TODO: Check input.
internal class EmailService : IEmailService, IDisposable
/// <summary>
/// Send email to the MTA.
/// </summary>
/// <remarks>
/// Keep the connection to the remote host succinct. MTA's are
/// typically not prepared to deal with long living connections.
/// Also its unclear if <seealso cref="SmtpClient"/> is designed
/// to reuse transport connections.
/// </remarks>
internal class EmailService : IEmailService
{
private readonly EmailOptions _options;
private readonly ISmtpClient emailClient = new SmtpClient();
private bool disposedValue;

/// <summary>
/// Logger.
/// </summary>
public ILogger Logger { get; }

/// <summary>
/// Create new instance.
/// </summary>
public EmailService(IOptions<EmailOptions> options, ILogger<EmailService> logger)
{
_options = options?.Value ?? throw new ArgumentNullException(nameof(options));
Expand Down Expand Up @@ -85,10 +94,12 @@ public async Task SendAsync(EmailMessage emailMessage)

Logger.LogDebug($"Message prepared, try sending message to MTA");

await emailClient.ConnectAsync(_options.SmtpServer, _options.SmtpPort, _options.SmtpTls);
await emailClient.AuthenticateAsync(_options.SmtpUsername, _options.SmtpPassword);
await emailClient.SendAsync(message);
await emailClient.DisconnectAsync(quit: true);
using SmtpClient client = new();

await client.ConnectAsync(_options.SmtpServer, _options.SmtpPort, _options.SmtpTls);
await client.AuthenticateAsync(_options.SmtpUsername, _options.SmtpPassword);
await client.SendAsync(message);
await client.DisconnectAsync(quit: true);

Logger.LogInformation($"Message sent with success");
}
Expand All @@ -99,36 +110,13 @@ public async Task SendAsync(EmailMessage emailMessage)
/// </summary>
public async Task TestService()
{
await emailClient.ConnectAsync(_options.SmtpServer, _options.SmtpPort, _options.SmtpTls);
await emailClient.AuthenticateAsync(_options.SmtpUsername, _options.SmtpPassword);
await emailClient.DisconnectAsync(quit: true);
}

#region Disposable Pattern

protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
emailClient.Dispose();
}

// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
disposedValue = true;
}
}
using SmtpClient client = new();

public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
await client.ConnectAsync(_options.SmtpServer, _options.SmtpPort, _options.SmtpTls);
await client.AuthenticateAsync(_options.SmtpUsername, _options.SmtpPassword);
await client.NoOpAsync();
await client.DisconnectAsync(quit: true);
}

#endregion Disposable Pattern
}
}
#pragma warning restore CA1812 // Internal class is never instantiated
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.14.0" />
<PackageReference Include="Grpc.Net.Client" Version="2.34.0" />
<PackageReference Include="Grpc.Tools" Version="2.34.0">
<PackageReference Include="Grpc.Tools" Version="2.34.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="System.Linq.Async" Version="5.0.0" />
<PackageReference Include="AWSSDK.S3" Version="3.5.7.3" />
<PackageReference Include="MailKit" Version="2.10.0" />
<PackageReference Include="AWSSDK.S3" Version="3.5.7.6" />
<PackageReference Include="MailKit" Version="2.10.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion tests/FunderMaps.Core.Tests/FunderMaps.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="Moq" Version="4.15.2" />
<PackageReference Include="Moq" Version="4.16.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="Bogus" Version="32.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="5.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="5.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="System.Collections" Version="4.3.0" />
<PackageReference Include="System.Net.Http.Json" Version="5.0.0" />
Expand Down

0 comments on commit e7bda31

Please sign in to comment.