forked from jcamp-code/FluentEmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FluentEmailPostmarkBuilderExtensions.cs
57 lines (54 loc) · 2.85 KB
/
FluentEmailPostmarkBuilderExtensions.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
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Collections.Generic;
using System.Text;
using FluentEmail.Core.Interfaces;
using FluentEmail.Postmark;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Provides fluent extension methods for adding Postmark as a sending backend to FluentEmail.
/// </summary>
public static class FluentEmailPostmarkBuilderExtensions
{
/// <summary>
/// Adds a PostmarkSender to be used by FluentEmail.
/// </summary>
/// <param name="builder">The builder for this FluentEmail service.</param>
/// <param name="serverToken">The serverToken to use to authenticate at the Postmark API.</param>
public static FluentEmailServicesBuilder AddPostmarkSender(this FluentEmailServicesBuilder builder, string serverToken)
{
_ = builder ?? throw new ArgumentNullException(nameof(builder));
builder.Services.TryAdd(ServiceDescriptor.Scoped<ISender>(x => new PostmarkSender(serverToken)));
return builder;
}
/// <summary>
/// Adds a PostmarkSender to be used by FluentEmail.
/// </summary>
/// <param name="builder">The builder for this FluentEmail service.</param>
/// <param name="serverToken">The serverToken to use to authenticate at the Postmark API.</param>
/// <param name="configureOptions">A method that changes the desired options of a PostmarkSenderOptions instance.</param>
public static FluentEmailServicesBuilder AddPostmarkSender(this FluentEmailServicesBuilder builder, string serverToken, Action<PostmarkSenderOptions> configureOptions)
{
_ = builder ?? throw new ArgumentNullException(nameof(builder));
_ = configureOptions ?? throw new ArgumentNullException(nameof(configureOptions));
var opts = new PostmarkSenderOptions(serverToken);
configureOptions(opts);
builder.Services.TryAdd(ServiceDescriptor.Scoped<ISender>(x => new PostmarkSender(opts)));
return builder;
}
/// <summary>
/// Adds a PostmarkSender to be used by FluentEmail.
/// </summary>
/// <param name="builder">The builder for this FluentEmail service.</param>
/// <param name="options">A preconfigured PostmarkSenderOptions instance.</param>
public static FluentEmailServicesBuilder AddPostmarkSender(this FluentEmailServicesBuilder builder, PostmarkSenderOptions options)
{
_ = builder ?? throw new ArgumentNullException(nameof(builder));
_ = options ?? throw new ArgumentNullException(nameof(options));
builder.Services.TryAdd(ServiceDescriptor.Scoped<ISender>(x => new PostmarkSender(options)));
return builder;
}
}
}