refactor: replace mailkit with system.net.mail for smtp test

MailKit/MimeKit are unnecessary third-party dependencies with known
vulnerabilities. .NET's built-in System.Net.Mail.SmtpClient handles
the same test send without any additional packages.

Ref: IT-628

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Anatolii Grynchuk
2026-05-02 03:48:36 +03:00
parent f7c35671b7
commit 395f5573a1
3 changed files with 16 additions and 23 deletions
-1
View File
@@ -32,7 +32,6 @@
<PackageVersion Include="HrynCo.Common" Version="1.0.0" /> <PackageVersion Include="HrynCo.Common" Version="1.0.0" />
<PackageVersion Include="HrynCo.RabbitMq" Version="1.0.11" /> <PackageVersion Include="HrynCo.RabbitMq" Version="1.0.11" />
<PackageVersion Include="MailKit" Version="4.11.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.14.1" /> <PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageVersion Include="coverlet.collector" Version="6.0.4" /> <PackageVersion Include="coverlet.collector" Version="6.0.4" />
<PackageVersion Include="xunit" Version="2.9.3" /> <PackageVersion Include="xunit" Version="2.9.3" />
@@ -5,11 +5,10 @@ using HrynCo.NotificationService.Services.EmailChannels.Get;
using HrynCo.NotificationService.Services.EmailChannels.GetAll; using HrynCo.NotificationService.Services.EmailChannels.GetAll;
using HrynCo.NotificationService.Services.EmailChannels.Update; using HrynCo.NotificationService.Services.EmailChannels.Update;
using HrynCo.NotificationService.Web.Controllers.Admin.ViewModels; using HrynCo.NotificationService.Web.Controllers.Admin.ViewModels;
using MailKit.Net.Smtp;
using MailKit.Security;
using MediatR; using MediatR;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using MimeKit; using System.Net;
using System.Net.Mail;
namespace HrynCo.NotificationService.Web.Controllers.Admin; namespace HrynCo.NotificationService.Web.Controllers.Admin;
@@ -153,28 +152,24 @@ public class AdminChannelsController : Controller
try try
{ {
var message = new MimeMessage(); using var client = new SmtpClient(smtp.Host, smtp.Port)
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}" EnableSsl = smtp.UseSsl,
Credentials = string.IsNullOrWhiteSpace(smtp.Username)
? null
: new NetworkCredential(smtp.Username, smtp.Password)
}; };
using var client = new SmtpClient(); var message = new MailMessage
var secureSocket = smtp.Port == 465 {
? SecureSocketOptions.SslOnConnect From = new MailAddress(smtp.FromEmail, smtp.FromName),
: smtp.UseSsl Subject = "✅ Test email from Notification Service",
? SecureSocketOptions.StartTls Body = $"This is a test email sent from the Notification Service admin panel.\n\nChannel: {result.Result.ServiceName}\nHost: {smtp.Host}:{smtp.Port}",
: SecureSocketOptions.None; IsBodyHtml = false
await client.ConnectAsync(smtp.Host, smtp.Port, secureSocket, ct); };
message.To.Add(request.ToEmail);
if (!string.IsNullOrWhiteSpace(smtp.Username)) await client.SendMailAsync(message, ct);
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}." }); return Ok(new { success = true, message = $"Test email sent to {request.ToEmail}." });
} }
@@ -8,7 +8,6 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="MailKit" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" /> <PackageReference Include="Microsoft.AspNetCore.OpenApi" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design"> <PackageReference Include="Microsoft.EntityFrameworkCore.Design">