@page "/design/smtp" @using ScadaLink.Security @using ScadaLink.Commons.Interfaces.Repositories @using SmtpConfigurationEntity = ScadaLink.Commons.Entities.Notifications.SmtpConfiguration @attribute [Authorize(Policy = AuthorizationPolicies.RequireDesign)] @inject INotificationRepository NotificationRepository @inject NavigationManager NavigationManager

SMTP Configuration

@if (_loading) { } else if (_errorMessage != null) {
@_errorMessage
} else { @if (_smtpConfigs.Count == 0 && !_showForm) {

No SMTP configuration set.

} else { @foreach (var smtp in _smtpConfigs) {
@smtp.Host @if (_editingSmtp?.Id != smtp.Id || !_showForm) { }
Host
@smtp.Host:@smtp.Port
Auth Type
@smtp.AuthType
From Address
@smtp.FromAddress
Credentials
@(string.IsNullOrWhiteSpace(smtp.Credentials) ? "(not set)" : "(stored)")
} @if (_showForm) {
@(_editingSmtp != null ? "Edit SMTP Configuration" : "Add SMTP Configuration")
Treat as sensitive — visible to admins only.
@if (_formError != null) {
@_formError
}
} else if (_smtpConfigs.Count == 0) { } } }
@code { private bool _loading = true; private string? _errorMessage; private List _smtpConfigs = new(); private bool _showForm; private SmtpConfigurationEntity? _editingSmtp; private string _host = string.Empty; private int _port = 587; private string _authType = "OAuth2"; private string? _credentials; private string _fromAddress = string.Empty; private string? _formError; private ToastNotification _toast = default!; protected override async Task OnInitializedAsync() { await LoadAsync(); } private async Task LoadAsync() { _loading = true; _errorMessage = null; try { _smtpConfigs = (await NotificationRepository.GetAllSmtpConfigurationsAsync()).ToList(); } catch (Exception ex) { _errorMessage = ex.Message; } _loading = false; } private void ShowAddForm() { _editingSmtp = null; _host = string.Empty; _port = 587; _authType = "OAuth2"; _credentials = null; _fromAddress = string.Empty; _formError = null; _showForm = true; } private void StartEdit(SmtpConfigurationEntity smtp) { _editingSmtp = smtp; _host = smtp.Host; _port = smtp.Port; _authType = smtp.AuthType; _credentials = smtp.Credentials; _fromAddress = smtp.FromAddress; _formError = null; _showForm = true; } private void CancelForm() { _showForm = false; _formError = null; } private async Task Save() { _formError = null; if (string.IsNullOrWhiteSpace(_host) || string.IsNullOrWhiteSpace(_fromAddress)) { _formError = "Host and From Address are required."; return; } try { if (_editingSmtp != null) { _editingSmtp.Host = _host.Trim(); _editingSmtp.Port = _port; _editingSmtp.AuthType = _authType; _editingSmtp.Credentials = _credentials?.Trim(); _editingSmtp.FromAddress = _fromAddress.Trim(); await NotificationRepository.UpdateSmtpConfigurationAsync(_editingSmtp); } else { var smtp = new SmtpConfigurationEntity(_host.Trim(), _authType, _fromAddress.Trim()) { Port = _port, Credentials = _credentials?.Trim() }; await NotificationRepository.AddSmtpConfigurationAsync(smtp); } await NotificationRepository.SaveChangesAsync(); _showForm = false; _toast.ShowSuccess("SMTP configuration saved."); await LoadAsync(); } catch (Exception ex) { _formError = ex.Message; } } }