-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceCollectionExtensions.cs
More file actions
121 lines (110 loc) · 5.26 KB
/
ServiceCollectionExtensions.cs
File metadata and controls
121 lines (110 loc) · 5.26 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// <copyright file="ServiceCollectionExtensions.cs" company="Moonrise Software, LLC">
// Copyright (c) Moonrise Software, LLC. All rights reserved.
// Licensed under the GNU Public License, Version 3.0 (https://www.gnu.org/licenses/gpl-3.0.html)
// See https://github.com/MoonriseSoftwareCalifornia/CosmosCMS
// for more information concerning the license and the contributors participating to this project.
// </copyright>
namespace Cosmos.EmailServices
{
using System.Configuration;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
/// <summary>
/// extension methods for <see cref="IServiceCollection"/>.
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Adds the Cosmos Email Services to the services collection.
/// </summary>
/// <param name="services">Startup services collection.</param>
/// <param name="configuration">System configuration.</param>
/// <remarks>
/// Tries to add an email service in this order: SMTP, Azure Communication, SendGrid.
/// </remarks>
public static void AddCosmosEmailServices(this IServiceCollection services, IConfiguration configuration)
{
var adminEmail = configuration.GetValue<string>("AdminEmail") ?? throw new ArgumentException("No AdminEmail configuration found.");
// Attempt to add SMTP Email Provider.
try
{
var smtpConfig = configuration.GetSection("SmtpEmailProviderOptions").Get<SmtpEmailProviderOptions>();
if (smtpConfig != null
&& string.IsNullOrEmpty(smtpConfig.Host) == false
&& string.IsNullOrEmpty(smtpConfig.UserName) == false
&& string.IsNullOrEmpty(smtpConfig.Password) == false
&& smtpConfig.Port > 0)
{
smtpConfig.DefaultFromEmailAddress = adminEmail;
services.AddSmtpEmailProvider(smtpConfig);
return;
}
}
catch
{
// Ignore and try the next provider.
}
// Attempt to add Azure Communication Email Provider.
var azureCommunicationConnection = configuration.GetConnectionString("AzureCommunicationConnection");
if (!string.IsNullOrEmpty(azureCommunicationConnection))
{
services.AddAzureCommunicationEmailSenderProvider(new AzureCommunicationEmailProviderOptions()
{
ConnectionString = azureCommunicationConnection,
DefaultFromEmailAddress = adminEmail
});
return;
}
// Attempt to add SendGrid Email Provider.
var sendGridApiKey = configuration.GetValue<string>("CosmosSendGridApiKey");
if (!string.IsNullOrEmpty(sendGridApiKey))
{
var sendGridOptions = new SendGridEmailProviderOptions(sendGridApiKey, adminEmail);
services.AddSendGridEmailProvider(sendGridOptions);
return;
}
// Add a NoOp Email Sender.
services.AddNoOpEmailSender();
}
/// <summary>
/// Adds the SendGrid Email Provider to the services collection.
/// </summary>
/// <param name="services">Startup services collection.</param>
/// <param name="options">SendGrid provider options.</param>
public static void AddSendGridEmailProvider(this IServiceCollection services, SendGridEmailProviderOptions options)
{
services.AddSingleton(Options.Create(options));
services.AddTransient<IEmailSender, SendGridEmailSender>();
}
/// <summary>
/// Adds the default Azure Email Communication Services.
/// </summary>
/// <param name="services">Startup services collection.</param>
/// <param name="options">Azure Communications email provider options.</param>
public static void AddAzureCommunicationEmailSenderProvider(this IServiceCollection services, AzureCommunicationEmailProviderOptions options)
{
services.AddSingleton(Options.Create(options));
services.AddTransient<IEmailSender, AzureCommunicationEmailSender>();
}
/// <summary>
/// Add SMTP EMail Provider.
/// </summary>
/// <param name="services">Startup services collection.</param>
/// <param name="options">SMTP Email provider options.</param>
public static void AddSmtpEmailProvider(this IServiceCollection services, SmtpEmailProviderOptions options)
{
services.AddSingleton(Options.Create(options));
services.AddTransient<IEmailSender, SmtpEmailSender>();
}
/// <summary>
/// Adds a NoOp Email Sender to the services collection.
/// </summary>
/// <param name="services">Startup services collection.</param>
public static void AddNoOpEmailSender(this IServiceCollection services)
{
services.AddTransient<IEmailSender, CosmosNoOpEmailSender>();
}
}
}