feat: add test channel feature in admin UI
- POST /admin/channels/{id}/test — direct SMTP send via MailKit
- Test button shown only on existing channels (not create)
- Bootstrap modal with recipient email input and spinner
- Inline success/error result inside the modal
Ref: IT-628
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -5,8 +5,11 @@ using HrynCo.NotificationService.Services.EmailChannels.Get;
|
||||
using HrynCo.NotificationService.Services.EmailChannels.GetAll;
|
||||
using HrynCo.NotificationService.Services.EmailChannels.Update;
|
||||
using HrynCo.NotificationService.Web.Controllers.Admin.ViewModels;
|
||||
using MailKit.Net.Smtp;
|
||||
using MailKit.Security;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MimeKit;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.Admin;
|
||||
|
||||
@@ -142,6 +145,46 @@ public class AdminChannelsController : Controller
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
// POST /admin/channels/{id}/test
|
||||
[HttpPost("{id:guid}/test")]
|
||||
public async Task<IActionResult> Test(Guid id, [FromBody] TestChannelRequest request, CancellationToken ct)
|
||||
{
|
||||
var result = await _mediator.Send(new GetEmailChannelQuery(id), ct);
|
||||
if (!result.IsSuccess || result.Result is null)
|
||||
return NotFound(new { success = false, message = "Channel not found." });
|
||||
|
||||
if (result.Result.Settings is not SmtpChannelSettings smtp)
|
||||
return BadRequest(new { success = false, message = "Only SMTP channels are supported." });
|
||||
|
||||
try
|
||||
{
|
||||
var message = new MimeMessage();
|
||||
message.From.Add(new MailboxAddress(smtp.FromName, smtp.FromEmail));
|
||||
message.To.Add(MailboxAddress.Parse(request.ToEmail));
|
||||
message.Subject = "✅ Test email from Notification Service";
|
||||
message.Body = new TextPart("plain")
|
||||
{
|
||||
Text = $"This is a test email sent from the Notification Service admin panel.\n\nChannel: {result.Result.ServiceName}\nHost: {smtp.Host}:{smtp.Port}"
|
||||
};
|
||||
|
||||
using var client = new SmtpClient();
|
||||
var secureSocket = smtp.UseSsl ? SecureSocketOptions.SslOnConnect : SecureSocketOptions.StartTlsWhenAvailable;
|
||||
await client.ConnectAsync(smtp.Host, smtp.Port, secureSocket, ct);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(smtp.Username))
|
||||
await client.AuthenticateAsync(smtp.Username, smtp.Password, ct);
|
||||
|
||||
await client.SendAsync(message, ct);
|
||||
await client.DisconnectAsync(true, ct);
|
||||
|
||||
return Ok(new { success = true, message = $"Test email sent to {request.ToEmail}." });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Ok(new { success = false, message = ex.Message });
|
||||
}
|
||||
}
|
||||
|
||||
// POST /admin/channels/{id}/delete
|
||||
[HttpPost("{id:guid}/delete")]
|
||||
[ValidateAntiForgeryToken]
|
||||
@@ -151,3 +194,5 @@ public class AdminChannelsController : Controller
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
}
|
||||
|
||||
public record TestChannelRequest(string ToEmail);
|
||||
|
||||
Reference in New Issue
Block a user