mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-07-16 17:47:22 +00:00
Format ./web/backend/Infrastructure/Infrastructure.Email
This commit is contained in:
@@ -21,10 +21,5 @@ public interface IEmailProvider
|
|||||||
/// <param name="subject">Email subject line</param>
|
/// <param name="subject">Email subject line</param>
|
||||||
/// <param name="body">Email body (HTML or plain text)</param>
|
/// <param name="body">Email body (HTML or plain text)</param>
|
||||||
/// <param name="isHtml">Whether the body is HTML (default: true)</param>
|
/// <param name="isHtml">Whether the body is HTML (default: true)</param>
|
||||||
Task SendAsync(
|
Task SendAsync(IEnumerable<string> to, string subject, string body, bool isHtml = true);
|
||||||
IEnumerable<string> to,
|
|
||||||
string subject,
|
|
||||||
string body,
|
|
||||||
bool isHtml = true
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
@@ -7,6 +7,6 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="MailKit" Version="4.15.1"/>
|
<PackageReference Include="MailKit" Version="4.15.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -58,22 +58,16 @@ public class SmtpEmailProvider : IEmailProvider
|
|||||||
{
|
{
|
||||||
_host =
|
_host =
|
||||||
Environment.GetEnvironmentVariable("SMTP_HOST")
|
Environment.GetEnvironmentVariable("SMTP_HOST")
|
||||||
?? throw new InvalidOperationException(
|
?? throw new InvalidOperationException("SMTP_HOST environment variable is not set");
|
||||||
"SMTP_HOST environment variable is not set"
|
|
||||||
);
|
|
||||||
|
|
||||||
string portString =
|
string portString = Environment.GetEnvironmentVariable("SMTP_PORT") ?? "587";
|
||||||
Environment.GetEnvironmentVariable("SMTP_PORT") ?? "587";
|
|
||||||
if (!int.TryParse(portString, out _port))
|
if (!int.TryParse(portString, out _port))
|
||||||
throw new InvalidOperationException(
|
throw new InvalidOperationException($"SMTP_PORT '{portString}' is not a valid integer");
|
||||||
$"SMTP_PORT '{portString}' is not a valid integer"
|
|
||||||
);
|
|
||||||
|
|
||||||
_username = Environment.GetEnvironmentVariable("SMTP_USERNAME");
|
_username = Environment.GetEnvironmentVariable("SMTP_USERNAME");
|
||||||
_password = Environment.GetEnvironmentVariable("SMTP_PASSWORD");
|
_password = Environment.GetEnvironmentVariable("SMTP_PASSWORD");
|
||||||
|
|
||||||
string useSslString =
|
string useSslString = Environment.GetEnvironmentVariable("SMTP_USE_SSL") ?? "true";
|
||||||
Environment.GetEnvironmentVariable("SMTP_USE_SSL") ?? "true";
|
|
||||||
_useSsl = bool.Parse(useSslString);
|
_useSsl = bool.Parse(useSslString);
|
||||||
|
|
||||||
_fromEmail =
|
_fromEmail =
|
||||||
@@ -82,9 +76,7 @@ public class SmtpEmailProvider : IEmailProvider
|
|||||||
"SMTP_FROM_EMAIL environment variable is not set"
|
"SMTP_FROM_EMAIL environment variable is not set"
|
||||||
);
|
);
|
||||||
|
|
||||||
_fromName =
|
_fromName = Environment.GetEnvironmentVariable("SMTP_FROM_NAME") ?? "The Biergarten";
|
||||||
Environment.GetEnvironmentVariable("SMTP_FROM_NAME")
|
|
||||||
?? "The Biergarten";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -95,12 +87,7 @@ public class SmtpEmailProvider : IEmailProvider
|
|||||||
/// <param name="body">Email body (HTML or plain text)</param>
|
/// <param name="body">Email body (HTML or plain text)</param>
|
||||||
/// <param name="isHtml">Whether the body is HTML (default: true)</param>
|
/// <param name="isHtml">Whether the body is HTML (default: true)</param>
|
||||||
/// <exception cref="InvalidOperationException">Thrown when connecting, authenticating, or sending via SMTP fails.</exception>
|
/// <exception cref="InvalidOperationException">Thrown when connecting, authenticating, or sending via SMTP fails.</exception>
|
||||||
public async Task SendAsync(
|
public async Task SendAsync(string to, string subject, string body, bool isHtml = true)
|
||||||
string to,
|
|
||||||
string subject,
|
|
||||||
string body,
|
|
||||||
bool isHtml = true
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
await SendAsync([to], subject, body, isHtml);
|
await SendAsync([to], subject, body, isHtml);
|
||||||
}
|
}
|
||||||
@@ -128,7 +115,8 @@ public class SmtpEmailProvider : IEmailProvider
|
|||||||
MimeMessage message = new();
|
MimeMessage message = new();
|
||||||
message.From.Add(new MailboxAddress(_fromName, _fromEmail));
|
message.From.Add(new MailboxAddress(_fromName, _fromEmail));
|
||||||
|
|
||||||
foreach (string recipient in to) message.To.Add(MailboxAddress.Parse(recipient));
|
foreach (string recipient in to)
|
||||||
|
message.To.Add(MailboxAddress.Parse(recipient));
|
||||||
|
|
||||||
message.Subject = subject;
|
message.Subject = subject;
|
||||||
|
|
||||||
@@ -152,10 +140,7 @@ public class SmtpEmailProvider : IEmailProvider
|
|||||||
await client.ConnectAsync(_host, _port, secureSocketOptions);
|
await client.ConnectAsync(_host, _port, secureSocketOptions);
|
||||||
|
|
||||||
// Authenticate if credentials are provided
|
// Authenticate if credentials are provided
|
||||||
if (
|
if (!string.IsNullOrEmpty(_username) && !string.IsNullOrEmpty(_password))
|
||||||
!string.IsNullOrEmpty(_username)
|
|
||||||
&& !string.IsNullOrEmpty(_password)
|
|
||||||
)
|
|
||||||
await client.AuthenticateAsync(_username, _password);
|
await client.AuthenticateAsync(_username, _password);
|
||||||
|
|
||||||
await client.SendAsync(message);
|
await client.SendAsync(message);
|
||||||
@@ -163,10 +148,7 @@ public class SmtpEmailProvider : IEmailProvider
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException(
|
throw new InvalidOperationException($"Failed to send email: {ex.Message}", ex);
|
||||||
$"Failed to send email: {ex.Message}",
|
|
||||||
ex
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user