Implement mail service with configuration and settings management
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -6,4 +6,5 @@ riderModule.iml
|
||||
|
||||
Publish/
|
||||
app.db
|
||||
FoodsharingSiegen.Server/wwwroot/buildinfo.txt
|
||||
FoodsharingSiegen.Server/wwwroot/buildinfo.txt
|
||||
FoodsharingSiegen.Server/config/appsettings.json
|
||||
7
Docker/.env
Normal file
7
Docker/.env
Normal file
@@ -0,0 +1,7 @@
|
||||
MAIL_HOST=smtp.example.com
|
||||
MAIL_PORT=587
|
||||
MAIL_USERNAME=your_username
|
||||
MAIL_PASSWORD=your_password
|
||||
MAIL_FROM_ADDRESS=noreply@example.com
|
||||
MAIL_FROM_NAME="FS Einarbeitungen Musterhausen"
|
||||
MAIL_USE_SSL=true
|
||||
@@ -5,6 +5,17 @@ services:
|
||||
image: ghcr.io/troogs/fs-onboarding/server:latest
|
||||
ports:
|
||||
- "8100:56000"
|
||||
env_file:
|
||||
- .env
|
||||
environment:
|
||||
# Mail Configuration Examples. These override options in appsettings.json.
|
||||
# All values below can also be set via the active .env file or appsettings.json.
|
||||
- Settings__Mail__Host=${MAIL_HOST:-smtp.example.com}
|
||||
- Settings__Mail__Port=${MAIL_PORT:-587}
|
||||
- Settings__Mail__Username=${MAIL_USERNAME:-your_username}
|
||||
- Settings__Mail__Password=${MAIL_PASSWORD:-your_password}
|
||||
- Settings__Mail__FromAddress=${MAIL_FROM_ADDRESS:-no-reply@example.com}
|
||||
- Settings__Mail__UseSsl=${MAIL_USE_SSL:-true}
|
||||
volumes:
|
||||
- /docker/data/fs-onboarding/config:/app/config/
|
||||
- /docker/data/fs-onboarding/data:/app/data/
|
||||
@@ -6,6 +6,11 @@
|
||||
|
||||
public bool DisableStepIn { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the mail server settings.
|
||||
/// </summary>
|
||||
public MailSettings Mail { get; set; } = new();
|
||||
|
||||
public TermSettings Terms { get; set; } = new();
|
||||
|
||||
public bool TestMode { get; set; }
|
||||
|
||||
38
FoodsharingSiegen.Contracts/Model/MailSettings.cs
Normal file
38
FoodsharingSiegen.Contracts/Model/MailSettings.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
namespace FoodsharingSiegen.Contracts.Model
|
||||
{
|
||||
/// <summary>
|
||||
/// Configuration settings for the mail service.
|
||||
/// </summary>
|
||||
public class MailSettings
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the SMTP server host.
|
||||
/// </summary>
|
||||
public string Host { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the SMTP port.
|
||||
/// </summary>
|
||||
public int Port { get; set; } = 587;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the username for SMTP authentication.
|
||||
/// </summary>
|
||||
public string Username { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the password for SMTP authentication.
|
||||
/// </summary>
|
||||
public string Password { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the sender address.
|
||||
/// </summary>
|
||||
public string FromAddress { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether to use SSL/TLS connection.
|
||||
/// </summary>
|
||||
public bool UseSsl { get; set; } = true;
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="LibSassBuilder" Version="2.0.1" />
|
||||
<PackageReference Include="MailKit" Version="4.4.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.3" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.3">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
|
||||
30
FoodsharingSiegen.Server/Pages/Settings.razor
Normal file
30
FoodsharingSiegen.Server/Pages/Settings.razor
Normal file
@@ -0,0 +1,30 @@
|
||||
@page "/settings"
|
||||
|
||||
@inherits FsBase
|
||||
|
||||
<PageTitle>Einstellungen - @AppSettings.Terms.Title</PageTitle>
|
||||
|
||||
<div style="width: 100%; max-width: 500px;">
|
||||
<h2>Einstellungen</h2>
|
||||
|
||||
<div class="card mt-3">
|
||||
<div class="card-header" style="padding: .5rem 0 0 1rem;"><h5>Mail Service</h5></div>
|
||||
<div class="card-body">
|
||||
<p>
|
||||
<strong>SMTP Server:</strong> @AppSettings.Mail.Host<br />
|
||||
<strong>Port:</strong> @AppSettings.Mail.Port<br />
|
||||
<strong>SSL:</strong> @(AppSettings.Mail.UseSsl ? "Ja" : "Nein")<br />
|
||||
</p>
|
||||
<hr />
|
||||
<Fields Class="my-3">
|
||||
<Field ColumnSize="ColumnSize.Is12">
|
||||
<FieldLabel>Testmail-Empfänger</FieldLabel>
|
||||
<FieldBody>
|
||||
<TextEdit @bind-Text="TestMailReceiver" Placeholder="E-Mail Adresse"></TextEdit>
|
||||
</FieldBody>
|
||||
</Field>
|
||||
</Fields>
|
||||
<Button Color="Color.Primary" Clicked="SendTestMail">Testmail senden</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
45
FoodsharingSiegen.Server/Pages/Settings.razor.cs
Normal file
45
FoodsharingSiegen.Server/Pages/Settings.razor.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using FoodsharingSiegen.Contracts.Helper;
|
||||
using FoodsharingSiegen.Server.Service;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FoodsharingSiegen.Server.Pages
|
||||
{
|
||||
public partial class Settings
|
||||
{
|
||||
[Inject]
|
||||
public IMailService MailService { get; set; } = null!;
|
||||
|
||||
private string TestMailReceiver { get; set; } = string.Empty;
|
||||
|
||||
protected override async Task InitializeDataAsync()
|
||||
{
|
||||
if (!CurrentUser.IsAdmin())
|
||||
{
|
||||
NavigationManager.NavigateTo("/");
|
||||
return;
|
||||
}
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
|
||||
private async Task SendTestMail()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(TestMailReceiver))
|
||||
{
|
||||
await Notification.Error("Bitte eine Empfänger-Adresse angeben.");
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await MailService.SendEmailAsync(TestMailReceiver, "Testmail", "Dies ist eine Testmail.");
|
||||
await Notification.Success("Testmail versendet.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await Notification.Error($"Fehler beim Senden: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,7 @@ builder.Services.AddScoped<AuditService>();
|
||||
builder.Services.AddScoped<AuthService>();
|
||||
builder.Services.AddScoped<UserService>();
|
||||
builder.Services.AddScoped<ProspectService>();
|
||||
builder.Services.AddScoped<IMailService, MailService>();
|
||||
|
||||
builder.Services
|
||||
.AddBlazorise()
|
||||
|
||||
19
FoodsharingSiegen.Server/Service/IMailService.cs
Normal file
19
FoodsharingSiegen.Server/Service/IMailService.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FoodsharingSiegen.Server.Service
|
||||
{
|
||||
/// <summary>
|
||||
/// Service interface for sending emails.
|
||||
/// </summary>
|
||||
public interface IMailService
|
||||
{
|
||||
/// <summary>
|
||||
/// Sends an email asynchronously.
|
||||
/// </summary>
|
||||
/// <param name="toEmail">The recipient's email address.</param>
|
||||
/// <param name="subject">The subject of the email.</param>
|
||||
/// <param name="htmlBody">The HTML content of the email body.</param>
|
||||
/// <returns>A task that represents the asynchronous email sending operation.</returns>
|
||||
Task SendEmailAsync(string toEmail, string subject, string htmlBody);
|
||||
}
|
||||
}
|
||||
57
FoodsharingSiegen.Server/Service/MailService.cs
Normal file
57
FoodsharingSiegen.Server/Service/MailService.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using System.Threading.Tasks;
|
||||
using FoodsharingSiegen.Contracts.Model;
|
||||
using MailKit.Net.Smtp;
|
||||
using MailKit.Security;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MimeKit;
|
||||
using MimeKit.Text;
|
||||
|
||||
namespace FoodsharingSiegen.Server.Service
|
||||
{
|
||||
/// <summary>
|
||||
/// Default implementation of <see cref="IMailService"/> which sends emails via SMTP using MailKit.
|
||||
/// </summary>
|
||||
public class MailService : IMailService
|
||||
{
|
||||
private readonly MailSettings _mailSettings;
|
||||
private readonly TermSettings _termSettings;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MailService"/> class.
|
||||
/// </summary>
|
||||
/// <param name="appSettings">The configured application settings injected by DI, containing the <see cref="MailSettings"/>.</param>
|
||||
public MailService(IOptions<AppSettings> appSettings)
|
||||
{
|
||||
_mailSettings = appSettings.Value.Mail;
|
||||
_termSettings = appSettings.Value.Terms;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task SendEmailAsync(string toEmail, string subject, string htmlBody)
|
||||
{
|
||||
var email = new MimeMessage();
|
||||
email.From.Add(new MailboxAddress(_termSettings.Title, _mailSettings.FromAddress));
|
||||
email.To.Add(MailboxAddress.Parse(toEmail));
|
||||
email.Subject = subject;
|
||||
|
||||
var textPart = new TextPart(TextFormat.Html)
|
||||
{
|
||||
Text = htmlBody
|
||||
};
|
||||
email.Body = textPart;
|
||||
|
||||
using var smtp = new SmtpClient();
|
||||
var secureOptions = _mailSettings.UseSsl ? SecureSocketOptions.StartTls : SecureSocketOptions.Auto;
|
||||
|
||||
await smtp.ConnectAsync(_mailSettings.Host, _mailSettings.Port, secureOptions);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(_mailSettings.Username))
|
||||
{
|
||||
await smtp.AuthenticateAsync(_mailSettings.Username, _mailSettings.Password);
|
||||
}
|
||||
|
||||
await smtp.SendAsync(email);
|
||||
await smtp.DisconnectAsync(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -49,13 +49,21 @@
|
||||
|
||||
@if (CurrentUser.IsAdmin())
|
||||
{
|
||||
<div class="nav-item px-3 pb-0">
|
||||
<div class="nav-item px-3">
|
||||
<div @onclick="NavLinkClickedAsync">
|
||||
<NavLink class="nav-link" href="users" Match="NavLinkMatch.All">
|
||||
<span class="fas fa-users mr-2" aria-hidden="true" style="font-size: 1.4em;"></span> Benutzer
|
||||
</NavLink>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="nav-item px-3 pb-0">
|
||||
<div @onclick="NavLinkClickedAsync">
|
||||
<NavLink class="nav-link" href="settings" Match="NavLinkMatch.All">
|
||||
<span class="fas fa-cog mr-2" aria-hidden="true" style="font-size: 1.4em;"></span> Einstellungen
|
||||
</NavLink>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
<div class="nav-item px-3">
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{
|
||||
{
|
||||
"Kestrel": {
|
||||
"Endpoints": {
|
||||
"Http": {
|
||||
@@ -9,10 +9,18 @@
|
||||
"DetailedErrors": true,
|
||||
"Settings": {
|
||||
"TestMode": true,
|
||||
"Mail": {
|
||||
"Host": "mail.example.com",
|
||||
"Port": 587,
|
||||
"Username": "your_username",
|
||||
"Password": "your_password",
|
||||
"FromAddress": "no-reply@example.com",
|
||||
"UseSsl": true
|
||||
},
|
||||
"Terms": {
|
||||
"Title": "Foodsharing Musterhausen",
|
||||
"TitleShort": "Musterhausen",
|
||||
"StepInName": "Krabbelgruppe2"
|
||||
"StepInName": "Neulingstreffen"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user