Implemented SmtpEmailSender for Identity
This commit is contained in:
parent
a0d5264870
commit
5b68e1d9eb
11
Wave/Data/SmtpConfiguration.cs
Normal file
11
Wave/Data/SmtpConfiguration.cs
Normal 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;
|
||||||
|
}
|
|
@ -59,8 +59,6 @@
|
||||||
.AddDefaultTokenProviders()
|
.AddDefaultTokenProviders()
|
||||||
.AddClaimsPrincipalFactory<UserClaimsFactory>();
|
.AddClaimsPrincipalFactory<UserClaimsFactory>();
|
||||||
|
|
||||||
builder.Services.AddSingleton<IEmailSender<ApplicationUser>, IdentityNoOpEmailSender>();
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Services
|
#region Services
|
||||||
|
@ -74,6 +72,14 @@
|
||||||
builder.Services.AddCascadingValue("TitlePrefix",
|
builder.Services.AddCascadingValue("TitlePrefix",
|
||||||
sf => (sf.GetService<IOptions<Customization>>()?.Value.AppName ?? "Wave") + " - ");
|
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
|
#endregion
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
55
Wave/Services/SmtpEmailSender.cs
Normal file
55
Wave/Services/SmtpEmailSender.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -14,6 +14,7 @@
|
||||||
<PackageReference Include="Humanizer.Core.de" Version="2.14.1" />
|
<PackageReference Include="Humanizer.Core.de" Version="2.14.1" />
|
||||||
<PackageReference Include="Humanizer.Core.uk" 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="Magick.NET-Q8-AnyCPU" Version="13.5.0" />
|
||||||
|
<PackageReference Include="MailKit" Version="4.3.0" />
|
||||||
<PackageReference Include="Markdig" Version="0.34.0" />
|
<PackageReference Include="Markdig" Version="0.34.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.0" />
|
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.0" />
|
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.0" />
|
||||||
|
|
Loading…
Reference in a new issue