forked from lukencode/FluentEmail
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathFluentEmailMailKitBuilderExtensions.cs
46 lines (44 loc) · 2.11 KB
/
FluentEmailMailKitBuilderExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using FluentEmail.Core.Interfaces;
using FluentEmail.MailKitSmtp;
using MailKit;
using MailKit.Security;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
namespace Microsoft.Extensions.DependencyInjection
{
public static class FluentEmailMailKitBuilderExtensions
{
public static FluentEmailServicesBuilder AddMailKitSender(this FluentEmailServicesBuilder builder, SmtpClientOptions smtpClientOptions)
{
builder.Services.TryAdd(ServiceDescriptor.Scoped<ISender>(_ => new MailKitSender(smtpClientOptions)));
return builder;
}
public static FluentEmailServicesBuilder AddMailKitSender(this FluentEmailServicesBuilder builder, SmtpClientOptions smtpClientOptions, IProtocolLogger protocolLogger)
{
builder.Services.TryAdd(ServiceDescriptor.Scoped<ISender>(_ => new MailKitSender(smtpClientOptions, protocolLogger)));
return builder;
}
public static FluentEmailServicesBuilder AddMailKitSender(this FluentEmailServicesBuilder builder, string configPath = "SmtpClientOptions")
{
builder.Services.AddOptions<SmtpClientOptions>().BindConfiguration(configPath);
builder.Services.TryAddScoped<SmtpClientOptions>(resolver =>
{
var smtpOptions = resolver.GetRequiredService<IOptions<SmtpClientOptions>>();
if (smtpOptions.Value is null)
{
throw new System.Exception("SmtpClientOptions is null");
}
if (smtpOptions.Value.User is not null && smtpOptions.Value.Password is not null)
{
smtpOptions.Value.RequiresAuthentication = true;
smtpOptions.Value.UseSsl = true;
if (smtpOptions.Value.SocketOptions is null)
smtpOptions.Value.SocketOptions = SecureSocketOptions.StartTls;
}
return smtpOptions.Value;
});
builder.Services.TryAddScoped<ISender, MailKitSender>();
return builder;
}
}
}