Narrow ISmtpClient to the four operations MailSender actually uses, behind a Yavsc.Interfaces.ISmtpClientFactory. Production wires MailKitSmtpClient (SmtpClientFactory); tests wire a recording fake (RecordingSmtpClientFactory). The fake is pre-registered in WebServerFixture so SMTP calls are short-circuited; the EMailling test now asserts the Connect -> Authenticate -> Send -> Disconnect sequence. Yavsc.Abstract stays free of MailKit/MimeKit.
53 lines
1.4 KiB
C#
53 lines
1.4 KiB
C#
using MailKit.Security;
|
|
using MimeKit;
|
|
using Yavsc.Interfaces;
|
|
|
|
namespace Yavsc.Services
|
|
{
|
|
/// <summary>
|
|
/// Production adapter from <see cref="Yavsc.Interfaces.ISmtpClient"/>
|
|
/// to MailKit's <c>SmtpClient</c>. Stateless; the underlying
|
|
/// MailKit client is created per <c>Connect</c> cycle by the
|
|
/// factory and disposed by <c>MailSender</c>'s <c>using</c>.
|
|
/// </summary>
|
|
public sealed class MailKitSmtpClient : ISmtpClient
|
|
{
|
|
private readonly MailKit.Net.Smtp.SmtpClient _client;
|
|
|
|
public MailKitSmtpClient()
|
|
{
|
|
_client = new MailKit.Net.Smtp.SmtpClient();
|
|
}
|
|
|
|
public int Timeout
|
|
{
|
|
get => _client.Timeout;
|
|
set => _client.Timeout = value;
|
|
}
|
|
|
|
public void Connect(string host, int port, SecureSocketOptions options)
|
|
{
|
|
_client.Connect(host, port, options);
|
|
}
|
|
|
|
public void Authenticate(string userName, string password)
|
|
{
|
|
_client.Authenticate(userName, password);
|
|
}
|
|
|
|
public Task SendAsync(MimeMessage message, CancellationToken cancellationToken = default)
|
|
{
|
|
return _client.SendAsync(message, cancellationToken);
|
|
}
|
|
|
|
public void Disconnect(bool quit)
|
|
{
|
|
_client.Disconnect(quit);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_client.Dispose();
|
|
}
|
|
}
|
|
}
|