Skip to content

Commit ae546e7

Browse files
committed
Add support for protocolLogger to MailKit PR358
https://github.com/carlin010/FluentEmail
1 parent c1cb19a commit ae546e7

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/Senders/FluentEmail.MailKit/FluentEmailMailKitBuilderExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using FluentEmail.Core.Interfaces;
22
using FluentEmail.MailKitSmtp;
3+
using MailKit;
34
using Microsoft.Extensions.DependencyInjection.Extensions;
45

56
namespace Microsoft.Extensions.DependencyInjection
@@ -11,5 +12,10 @@ public static FluentEmailServicesBuilder AddMailKitSender(this FluentEmailServic
1112
builder.Services.TryAdd(ServiceDescriptor.Scoped<ISender>(_ => new MailKitSender(smtpClientOptions)));
1213
return builder;
1314
}
15+
public static FluentEmailServicesBuilder AddMailKitSender(this FluentEmailServicesBuilder builder, SmtpClientOptions smtpClientOptions, IProtocolLogger protocolLogger)
16+
{
17+
builder.Services.TryAdd(ServiceDescriptor.Scoped<ISender>(_ => new MailKitSender(smtpClientOptions, protocolLogger)));
18+
return builder;
19+
}
1420
}
1521
}

src/Senders/FluentEmail.MailKit/MailKitSender.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Text;
99
using System.Threading;
1010
using System.Threading.Tasks;
11+
using MailKit;
1112

1213
namespace FluentEmail.MailKitSmtp
1314
{
@@ -17,6 +18,7 @@ namespace FluentEmail.MailKitSmtp
1718
public class MailKitSender : ISender
1819
{
1920
private readonly SmtpClientOptions _smtpClientOptions;
21+
private readonly IProtocolLogger _protocolLogger;
2022

2123
/// <summary>
2224
/// Creates a sender that uses the given SmtpClientOptions when sending with MailKit. Since the client is internal this will dispose of the client.
@@ -27,6 +29,12 @@ public MailKitSender(SmtpClientOptions smtpClientOptions)
2729
_smtpClientOptions = smtpClientOptions;
2830
}
2931

32+
public MailKitSender(SmtpClientOptions smtpClientOptions, IProtocolLogger protocolLogger)
33+
{
34+
_smtpClientOptions = smtpClientOptions;
35+
_protocolLogger = protocolLogger;
36+
}
37+
3038
/// <summary>
3139
/// Send the specified email.
3240
/// </summary>
@@ -52,7 +60,7 @@ public SendResponse Send(IFluentEmail email, CancellationToken? token = null)
5260
return response;
5361
}
5462

55-
using (var client = new SmtpClient())
63+
using (var client = _protocolLogger == null ? new SmtpClient() : new SmtpClient(_protocolLogger))
5664
{
5765
if (_smtpClientOptions.SocketOptions.HasValue)
5866
{
@@ -115,7 +123,7 @@ public async Task<SendResponse> SendAsync(IFluentEmail email, CancellationToken?
115123
return response;
116124
}
117125

118-
using (var client = new SmtpClient())
126+
using (var client = _protocolLogger == null ? new SmtpClient() : new SmtpClient(_protocolLogger))
119127
{
120128
if (_smtpClientOptions.SocketOptions.HasValue)
121129
{

0 commit comments

Comments
 (0)