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:
@@ -5,11 +5,10 @@ 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;
|
||||
using System.Net;
|
||||
using System.Net.Mail;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.Admin;
|
||||
|
||||
@@ -153,28 +152,24 @@ public class AdminChannelsController : Controller
|
||||
|
||||
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")
|
||||
using var client = new SmtpClient(smtp.Host, smtp.Port)
|
||||
{
|
||||
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 secureSocket = smtp.Port == 465
|
||||
? SecureSocketOptions.SslOnConnect
|
||||
: smtp.UseSsl
|
||||
? SecureSocketOptions.StartTls
|
||||
: SecureSocketOptions.None;
|
||||
await client.ConnectAsync(smtp.Host, smtp.Port, secureSocket, ct);
|
||||
var message = new MailMessage
|
||||
{
|
||||
From = new MailAddress(smtp.FromEmail, smtp.FromName),
|
||||
Subject = "✅ Test email from Notification Service",
|
||||
Body = $"This is a test email sent from the Notification Service admin panel.\n\nChannel: {result.Result.ServiceName}\nHost: {smtp.Host}:{smtp.Port}",
|
||||
IsBodyHtml = false
|
||||
};
|
||||
message.To.Add(request.ToEmail);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(smtp.Username))
|
||||
await client.AuthenticateAsync(smtp.Username, smtp.Password, ct);
|
||||
|
||||
await client.SendAsync(message, ct);
|
||||
await client.DisconnectAsync(true, ct);
|
||||
await client.SendMailAsync(message, ct);
|
||||
|
||||
return Ok(new { success = true, message = $"Test email sent to {request.ToEmail}." });
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MailKit" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design">
|
||||
|
||||
Reference in New Issue
Block a user