< Summary

Information
Class: MyNet.Mail.MailKit.MailKitSmtpConnectionManager
Assembly: MyNet.Mail.MailKit
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Mail.MailKit/MailKitSmtpConnectionManager.cs
Tag: 342_31701152804
Line coverage
14%
Covered lines: 5
Uncovered lines: 30
Coverable lines: 35
Total lines: 98
Line coverage: 14.2%
Branch coverage
6%
Covered branches: 1
Total branches: 16
Branch coverage: 6.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
SendAsync()0%2040%
Dispose()50%22100%
GetConnectedClientAsync()0%4260%
ResetAsync()0%2040%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Mail.MailKit/MailKitSmtpConnectionManager.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="MailKitSmtpConnectionManager.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Threading;
 9using System.Threading.Tasks;
 10using MailKit;
 11using MailKit.Net.Smtp;
 12using MimeKit;
 13using MyNet.Mail.Smtp;
 14
 15namespace MyNet.Mail.MailKit;
 16
 17/// <summary>
 18/// Thread-safe, reusable MailKit <see cref="SmtpClient"/> for a fixed <see cref="SmtpClientOptions"/> profile.
 19/// </summary>
 20internal sealed class MailKitSmtpConnectionManager(SmtpClientOptions options) : IDisposable
 21{
 2422    private readonly SemaphoreSlim _gate = new(1, 1);
 23    private SmtpClient? _client;
 24
 25    public async Task<string> SendAsync(MimeMessage message, CancellationToken cancellationToken)
 26    {
 027        await _gate.WaitAsync(cancellationToken).ConfigureAwait(false);
 28        try
 29        {
 030            var client = await GetConnectedClientAsync(cancellationToken).ConfigureAwait(false);
 031            return await client.SendAsync(message, cancellationToken).ConfigureAwait(false);
 32        }
 033        catch
 34        {
 035            await ResetAsync().ConfigureAwait(false);
 036            throw;
 37        }
 38        finally
 39        {
 040            _ = _gate.Release();
 41        }
 042    }
 43
 44    public void Dispose()
 45    {
 2446        _gate.Dispose();
 2447        _client?.Dispose();
 2448        _client = null;
 2449    }
 50
 51    private async Task<SmtpClient> GetConnectedClientAsync(CancellationToken cancellationToken)
 52    {
 053        _client ??= new SmtpClient();
 54
 055        if (_client.IsConnected)
 56        {
 057            return _client;
 58        }
 59
 060        await _client.ConnectAsync(
 061            options.Server!,
 062            options.Port,
 063            SmtpSecureSocketOptionsResolver.Resolve(options),
 064            cancellationToken).ConfigureAwait(false);
 65
 066        if (options.RequiresAuthentication)
 67        {
 068            await _client.AuthenticateAsync(options.User!, options.Password!, cancellationToken).ConfigureAwait(false);
 69        }
 70
 071        return _client;
 072    }
 73
 74    private async Task ResetAsync()
 75    {
 076        if (_client is null)
 77        {
 078            return;
 79        }
 80
 81        try
 82        {
 083            if (_client.IsConnected)
 84            {
 085                await _client.DisconnectAsync(true).ConfigureAwait(false);
 86            }
 087        }
 088        catch (ServiceNotConnectedException)
 89        {
 090        }
 91        finally
 92        {
 093            _client.Dispose();
 094            _client = null;
 95        }
 096    }
 97}
 98