mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-07-16 17:47:22 +00:00
code style cleanup
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
namespace Infrastructure.Email;
|
||||
|
||||
/// <summary>
|
||||
/// Service for sending emails via SMTP.
|
||||
/// Service for sending emails via SMTP.
|
||||
/// </summary>
|
||||
public interface IEmailProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Sends an email to a single recipient.
|
||||
/// Sends an email to a single recipient.
|
||||
/// </summary>
|
||||
/// <param name="to">Recipient email address</param>
|
||||
/// <param name="subject">Email subject line</param>
|
||||
@@ -15,7 +15,7 @@ public interface IEmailProvider
|
||||
Task SendAsync(string to, string subject, string body, bool isHtml = true);
|
||||
|
||||
/// <summary>
|
||||
/// Sends an email to multiple recipients.
|
||||
/// Sends an email to multiple recipients.
|
||||
/// </summary>
|
||||
/// <param name="to">List of recipient email addresses</param>
|
||||
/// <param name="subject">Email subject line</param>
|
||||
@@ -27,4 +27,4 @@ public interface IEmailProvider
|
||||
string body,
|
||||
bool isHtml = true
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<RootNamespace>Infrastructure.Email</RootNamespace>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<RootNamespace>Infrastructure.Email</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MailKit" Version="4.15.1" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MailKit" Version="4.15.1"/>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -5,37 +5,54 @@ using MimeKit;
|
||||
namespace Infrastructure.Email;
|
||||
|
||||
/// <summary>
|
||||
/// SMTP email service implementation using MailKit.
|
||||
/// Configured via environment variables.
|
||||
/// SMTP email service implementation using MailKit.
|
||||
/// Configured via environment variables.
|
||||
/// </summary>
|
||||
public class SmtpEmailProvider : IEmailProvider
|
||||
{
|
||||
private readonly string _host;
|
||||
private readonly int _port;
|
||||
private readonly string? _username;
|
||||
private readonly string? _password;
|
||||
private readonly bool _useSsl;
|
||||
private readonly string _fromEmail;
|
||||
private readonly string _fromName;
|
||||
private readonly string _host;
|
||||
private readonly string? _password;
|
||||
private readonly int _port;
|
||||
private readonly string? _username;
|
||||
private readonly bool _useSsl;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of <see cref="SmtpEmailProvider"/>, reading SMTP configuration
|
||||
/// from environment variables.
|
||||
/// Initializes a new instance of <see cref="SmtpEmailProvider" />, reading SMTP configuration
|
||||
/// from environment variables.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Reads the following environment variables:
|
||||
/// <list type="bullet">
|
||||
/// <item><description><c>SMTP_HOST</c> (required) - the SMTP server hostname.</description></item>
|
||||
/// <item><description><c>SMTP_PORT</c> (optional, default "587") - the SMTP server port, must be a valid integer.</description></item>
|
||||
/// <item><description><c>SMTP_USERNAME</c> (optional) - the username used for authentication.</description></item>
|
||||
/// <item><description><c>SMTP_PASSWORD</c> (optional) - the password used for authentication.</description></item>
|
||||
/// <item><description><c>SMTP_USE_SSL</c> (optional, default "true") - whether to use StartTls when connecting.</description></item>
|
||||
/// <item><description><c>SMTP_FROM_EMAIL</c> (required) - the email address used as the sender.</description></item>
|
||||
/// <item><description><c>SMTP_FROM_NAME</c> (optional, default "The Biergarten") - the display name used as the sender.</description></item>
|
||||
/// </list>
|
||||
/// Reads the following environment variables:
|
||||
/// <list type="bullet">
|
||||
/// <item>
|
||||
/// <description><c>SMTP_HOST</c> (required) - the SMTP server hostname.</description>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <description><c>SMTP_PORT</c> (optional, default "587") - the SMTP server port, must be a valid integer.</description>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <description><c>SMTP_USERNAME</c> (optional) - the username used for authentication.</description>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <description><c>SMTP_PASSWORD</c> (optional) - the password used for authentication.</description>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <description><c>SMTP_USE_SSL</c> (optional, default "true") - whether to use StartTls when connecting.</description>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <description><c>SMTP_FROM_EMAIL</c> (required) - the email address used as the sender.</description>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <description>
|
||||
/// <c>SMTP_FROM_NAME</c> (optional, default "The Biergarten") - the display name used as the
|
||||
/// sender.
|
||||
/// </description>
|
||||
/// </item>
|
||||
/// </list>
|
||||
/// </remarks>
|
||||
/// <exception cref="InvalidOperationException">
|
||||
/// Thrown when <c>SMTP_HOST</c> or <c>SMTP_FROM_EMAIL</c> is not set, or when <c>SMTP_PORT</c> is not a valid integer.
|
||||
/// Thrown when <c>SMTP_HOST</c> or <c>SMTP_FROM_EMAIL</c> is not set, or when <c>SMTP_PORT</c> is not a valid integer.
|
||||
/// </exception>
|
||||
public SmtpEmailProvider()
|
||||
{
|
||||
@@ -45,19 +62,17 @@ public class SmtpEmailProvider : IEmailProvider
|
||||
"SMTP_HOST environment variable is not set"
|
||||
);
|
||||
|
||||
var portString =
|
||||
string portString =
|
||||
Environment.GetEnvironmentVariable("SMTP_PORT") ?? "587";
|
||||
if (!int.TryParse(portString, out _port))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"SMTP_PORT '{portString}' is not a valid integer"
|
||||
);
|
||||
}
|
||||
|
||||
_username = Environment.GetEnvironmentVariable("SMTP_USERNAME");
|
||||
_password = Environment.GetEnvironmentVariable("SMTP_PASSWORD");
|
||||
|
||||
var useSslString =
|
||||
string useSslString =
|
||||
Environment.GetEnvironmentVariable("SMTP_USE_SSL") ?? "true";
|
||||
_useSsl = bool.Parse(useSslString);
|
||||
|
||||
@@ -73,7 +88,7 @@ public class SmtpEmailProvider : IEmailProvider
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends an email to a single recipient by delegating to the multi-recipient overload.
|
||||
/// Sends an email to a single recipient by delegating to the multi-recipient overload.
|
||||
/// </summary>
|
||||
/// <param name="to">Recipient email address</param>
|
||||
/// <param name="subject">Email subject line</param>
|
||||
@@ -91,15 +106,18 @@ public class SmtpEmailProvider : IEmailProvider
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends an email to multiple recipients using MailKit's <see cref="SmtpClient"/>.
|
||||
/// Connects using StartTls when SSL is enabled (or no encryption otherwise), authenticates
|
||||
/// if credentials were configured, sends the message, and disconnects.
|
||||
/// Sends an email to multiple recipients using MailKit's <see cref="SmtpClient" />.
|
||||
/// Connects using StartTls when SSL is enabled (or no encryption otherwise), authenticates
|
||||
/// if credentials were configured, sends the message, and disconnects.
|
||||
/// </summary>
|
||||
/// <param name="to">List of recipient email addresses</param>
|
||||
/// <param name="subject">Email subject line</param>
|
||||
/// <param name="body">Email body (HTML or plain text)</param>
|
||||
/// <param name="isHtml">Whether the body is HTML (default: true)</param>
|
||||
/// <exception cref="InvalidOperationException">Thrown when connecting, authenticating, or sending via SMTP fails. The original exception is included as the inner exception.</exception>
|
||||
/// <exception cref="InvalidOperationException">
|
||||
/// Thrown when connecting, authenticating, or sending via SMTP fails. The
|
||||
/// original exception is included as the inner exception.
|
||||
/// </exception>
|
||||
public async Task SendAsync(
|
||||
IEnumerable<string> to,
|
||||
string subject,
|
||||
@@ -107,34 +125,27 @@ public class SmtpEmailProvider : IEmailProvider
|
||||
bool isHtml = true
|
||||
)
|
||||
{
|
||||
var message = new MimeMessage();
|
||||
MimeMessage message = new();
|
||||
message.From.Add(new MailboxAddress(_fromName, _fromEmail));
|
||||
|
||||
foreach (var recipient in to)
|
||||
{
|
||||
message.To.Add(MailboxAddress.Parse(recipient));
|
||||
}
|
||||
foreach (string recipient in to) message.To.Add(MailboxAddress.Parse(recipient));
|
||||
|
||||
message.Subject = subject;
|
||||
|
||||
var bodyBuilder = new BodyBuilder();
|
||||
BodyBuilder bodyBuilder = new();
|
||||
if (isHtml)
|
||||
{
|
||||
bodyBuilder.HtmlBody = body;
|
||||
}
|
||||
else
|
||||
{
|
||||
bodyBuilder.TextBody = body;
|
||||
}
|
||||
|
||||
message.Body = bodyBuilder.ToMessageBody();
|
||||
|
||||
using var client = new SmtpClient();
|
||||
using SmtpClient client = new();
|
||||
|
||||
try
|
||||
{
|
||||
// Determine the SecureSocketOptions based on SSL setting
|
||||
var secureSocketOptions = _useSsl
|
||||
SecureSocketOptions secureSocketOptions = _useSsl
|
||||
? SecureSocketOptions.StartTls
|
||||
: SecureSocketOptions.None;
|
||||
|
||||
@@ -145,9 +156,7 @@ public class SmtpEmailProvider : IEmailProvider
|
||||
!string.IsNullOrEmpty(_username)
|
||||
&& !string.IsNullOrEmpty(_password)
|
||||
)
|
||||
{
|
||||
await client.AuthenticateAsync(_username, _password);
|
||||
}
|
||||
|
||||
await client.SendAsync(message);
|
||||
await client.DisconnectAsync(true);
|
||||
@@ -160,4 +169,4 @@ public class SmtpEmailProvider : IEmailProvider
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user