| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="MailKitSmtpConnectionManager.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Threading; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | using MailKit; |
| | | 11 | | using MailKit.Net.Smtp; |
| | | 12 | | using MimeKit; |
| | | 13 | | using MyNet.Mail.Smtp; |
| | | 14 | | |
| | | 15 | | namespace MyNet.Mail.MailKit; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Thread-safe, reusable MailKit <see cref="SmtpClient"/> for a fixed <see cref="SmtpClientOptions"/> profile. |
| | | 19 | | /// </summary> |
| | | 20 | | internal sealed class MailKitSmtpConnectionManager(SmtpClientOptions options) : IDisposable |
| | | 21 | | { |
| | 24 | 22 | | private readonly SemaphoreSlim _gate = new(1, 1); |
| | | 23 | | private SmtpClient? _client; |
| | | 24 | | |
| | | 25 | | public async Task<string> SendAsync(MimeMessage message, CancellationToken cancellationToken) |
| | | 26 | | { |
| | 0 | 27 | | await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); |
| | | 28 | | try |
| | | 29 | | { |
| | 0 | 30 | | var client = await GetConnectedClientAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 31 | | return await client.SendAsync(message, cancellationToken).ConfigureAwait(false); |
| | | 32 | | } |
| | 0 | 33 | | catch |
| | | 34 | | { |
| | 0 | 35 | | await ResetAsync().ConfigureAwait(false); |
| | 0 | 36 | | throw; |
| | | 37 | | } |
| | | 38 | | finally |
| | | 39 | | { |
| | 0 | 40 | | _ = _gate.Release(); |
| | | 41 | | } |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | public void Dispose() |
| | | 45 | | { |
| | 24 | 46 | | _gate.Dispose(); |
| | 24 | 47 | | _client?.Dispose(); |
| | 24 | 48 | | _client = null; |
| | 24 | 49 | | } |
| | | 50 | | |
| | | 51 | | private async Task<SmtpClient> GetConnectedClientAsync(CancellationToken cancellationToken) |
| | | 52 | | { |
| | 0 | 53 | | _client ??= new SmtpClient(); |
| | | 54 | | |
| | 0 | 55 | | if (_client.IsConnected) |
| | | 56 | | { |
| | 0 | 57 | | return _client; |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | await _client.ConnectAsync( |
| | 0 | 61 | | options.Server!, |
| | 0 | 62 | | options.Port, |
| | 0 | 63 | | SmtpSecureSocketOptionsResolver.Resolve(options), |
| | 0 | 64 | | cancellationToken).ConfigureAwait(false); |
| | | 65 | | |
| | 0 | 66 | | if (options.RequiresAuthentication) |
| | | 67 | | { |
| | 0 | 68 | | await _client.AuthenticateAsync(options.User!, options.Password!, cancellationToken).ConfigureAwait(false); |
| | | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | return _client; |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | private async Task ResetAsync() |
| | | 75 | | { |
| | 0 | 76 | | if (_client is null) |
| | | 77 | | { |
| | 0 | 78 | | return; |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | try |
| | | 82 | | { |
| | 0 | 83 | | if (_client.IsConnected) |
| | | 84 | | { |
| | 0 | 85 | | await _client.DisconnectAsync(true).ConfigureAwait(false); |
| | | 86 | | } |
| | 0 | 87 | | } |
| | 0 | 88 | | catch (ServiceNotConnectedException) |
| | | 89 | | { |
| | 0 | 90 | | } |
| | | 91 | | finally |
| | | 92 | | { |
| | 0 | 93 | | _client.Dispose(); |
| | 0 | 94 | | _client = null; |
| | | 95 | | } |
| | 0 | 96 | | } |
| | | 97 | | } |
| | | 98 | | |