Files
Wino-Mail/Wino.Core/Services/ImapTestService.cs
T

41 lines
1.3 KiB
C#
Raw Normal View History

2024-04-18 01:44:37 +02:00
using System.Threading.Tasks;
using MailKit.Net.Smtp;
2024-11-10 23:28:25 +01:00
using Wino.Core.Domain.Entities.Shared;
2024-04-18 01:44:37 +02:00
using Wino.Core.Domain.Interfaces;
2024-09-29 21:21:51 +02:00
using Wino.Core.Domain.Models.Connectivity;
using Wino.Core.Integration;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
namespace Wino.Core.Services;
public class ImapTestService : IImapTestService
2024-04-18 01:44:37 +02:00
{
2026-04-05 13:18:50 +02:00
public ImapTestService()
2025-02-16 11:54:23 +01:00
{
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public async Task TestImapConnectionAsync(CustomServerInformation serverInformation, bool allowSSLHandShake)
{
2026-04-05 13:18:50 +02:00
var poolOptions = ImapClientPoolOptions.CreateTestPool(serverInformation);
2024-04-18 01:44:37 +02:00
2026-04-05 13:18:50 +02:00
using (var clientPool = new ImapClientPool(poolOptions)
{
ThrowOnSSLHandshakeCallback = !allowSSLHandShake
})
{
// This call will make sure that everything is authenticated + connected successfully.
var client = await clientPool.GetClientAsync();
2026-04-05 13:18:50 +02:00
clientPool.Release(client);
}
2026-04-05 13:18:50 +02:00
// Test SMTP connectivity.
using var smtpClient = new SmtpClient();
2026-04-05 13:18:50 +02:00
if (!smtpClient.IsConnected)
await smtpClient.ConnectAsync(serverInformation.OutgoingServer, int.Parse(serverInformation.OutgoingServerPort), MailKit.Security.SecureSocketOptions.Auto);
2026-04-05 13:18:50 +02:00
if (!smtpClient.IsAuthenticated)
await smtpClient.AuthenticateAsync(serverInformation.OutgoingServerUsername, serverInformation.OutgoingServerPassword);
2024-04-18 01:44:37 +02:00
}
}