Implemented SmtpEmailSender for Identity

This commit is contained in:
Mia Rose Winter 2024-01-18 15:24:58 +01:00
parent a0d5264870
commit 5b68e1d9eb
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E
4 changed files with 75 additions and 2 deletions

View file

@ -0,0 +1,11 @@
namespace Wave.Data;
public class SmtpConfiguration {
public required string Host { get; init; }
public required int Port { get; init; }
public required string Username { get; init; }
public required string Password { get; init; }
public required string SenderEmail { get; init; }
public required string SenderName { get; init; }
public bool Ssl { get; init; } = true;
}

View file

@ -59,8 +59,6 @@
.AddDefaultTokenProviders()
.AddClaimsPrincipalFactory<UserClaimsFactory>();
builder.Services.AddSingleton<IEmailSender<ApplicationUser>, IdentityNoOpEmailSender>();
#endregion
#region Services
@ -74,6 +72,14 @@
builder.Services.AddCascadingValue("TitlePrefix",
sf => (sf.GetService<IOptions<Customization>>()?.Value.AppName ?? "Wave") + " - ");
var smtpConfig = builder.Configuration.GetSection("Email:Smtp");
if (smtpConfig.Exists()) {
builder.Services.Configure<SmtpConfiguration>(smtpConfig);
builder.Services.AddScoped<IEmailSender<ApplicationUser>, SmtpEmailSender>();
} else {
builder.Services.AddSingleton<IEmailSender<ApplicationUser>, IdentityNoOpEmailSender>();
}
#endregion
var app = builder.Build();

View file

@ -0,0 +1,55 @@
using MailKit.Net.Smtp;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.Extensions.Options;
using MimeKit;
using Wave.Data;
namespace Wave.Services;
public class SmtpEmailSender(IOptions<SmtpConfiguration> config, ILogger<SmtpEmailSender> logger) : IEmailSender<ApplicationUser>, IEmailSender {
private SmtpConfiguration Configuration { get; } = config.Value;
private ILogger<SmtpEmailSender> Logger { get; } = logger;
public Task SendConfirmationLinkAsync(ApplicationUser user, string email, string confirmationLink) =>
SendEmailAsync(email, "Confirm your email",
$"Please confirm your account by <a href='{confirmationLink}'>clicking here</a>.");
public Task SendPasswordResetLinkAsync(ApplicationUser user, string email, string resetLink) =>
SendEmailAsync(email, "Reset your password",
$"Please reset your password by <a href='{resetLink}'>clicking here</a>.");
public Task SendPasswordResetCodeAsync(ApplicationUser user, string email, string resetCode) =>
SendEmailAsync(email, "Reset your password",
$"Please reset your password using the following code: {resetCode}");
public Task SendEmailAsync(string email, string subject, string htmlMessage) {
return SendEmailAsync(email, null, subject, htmlMessage);
}
public async Task SendEmailAsync(string email, string? name, string subject, string htmlMessage) {
try {
var message = new MimeMessage {
From = {new MailboxAddress(Configuration.SenderName, Configuration.SenderEmail)},
To = { new MailboxAddress(name, email) },
Subject = subject
};
var builder = new BodyBuilder {
HtmlBody = htmlMessage
};
message.Body = builder.ToMessageBody();
using var client = new SmtpClient();
await client.ConnectAsync(Configuration.Host, Configuration.Port);
await client.AuthenticateAsync(Configuration.Username, Configuration.Password);
await client.SendAsync(message);
await client.DisconnectAsync(true);
} catch (Exception ex) {
Logger.LogError(ex, "Error sending E-Mail");
throw;
}
}
}

View file

@ -14,6 +14,7 @@
<PackageReference Include="Humanizer.Core.de" Version="2.14.1" />
<PackageReference Include="Humanizer.Core.uk" Version="2.14.1" />
<PackageReference Include="Magick.NET-Q8-AnyCPU" Version="13.5.0" />
<PackageReference Include="MailKit" Version="4.3.0" />
<PackageReference Include="Markdig" Version="0.34.0" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.0" />