Skip to content

Commit 6e531bd

Browse files
committed
Refactored settings to IOptions pattern, added user secrets for Google Maps API Key
1 parent 6be609c commit 6e531bd

16 files changed

+215
-207
lines changed

Devlord.Utilities.sln.DotSettings

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@
1515
<s:Boolean x:Key="/Default/Environment/Filtering/ExcludeCoverageFilters/=Devlord_002EUtilities_003B_002A_003BDevlord_002EUtilities_002EResources_002E_002A_003B_002A/@EntryIndexedValue">True</s:Boolean>
1616
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EdotCover_002EIde_002ECore_002EFilterManagement_002EModel_002ESolutionFilterSettingsManagerMigrateSettings/@EntryIndexedValue">True</s:Boolean>
1717
<s:String x:Key="/Default/FilterSettingsManager/CoverageFilterXml/@EntryValue">&lt;data&gt;&lt;IncludeFilters /&gt;&lt;ExcludeFilters&gt;&lt;Filter ModuleMask="Devlord.Utilities" ModuleVersionMask="*" ClassMask="Devlord.Utilities.Properties.*" FunctionMask="*" IsEnabled="True" /&gt;&lt;Filter ModuleMask="Devlord.Utilities" ModuleVersionMask="*" ClassMask="Devlord.Utilities.Resources.*" FunctionMask="*" IsEnabled="True" /&gt;&lt;/ExcludeFilters&gt;&lt;/data&gt;</s:String>
18-
<s:Boolean x:Key="/Default/UserDictionary/Words/=Devlord/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
18+
<s:Boolean x:Key="/Default/UserDictionary/Words/=botmail/@EntryIndexedValue">True</s:Boolean>
19+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Devlord/@EntryIndexedValue">True</s:Boolean>
20+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Mailbot/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

src/Devlord.Utilities/Devlord.Utilities.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<ItemGroup>
2626
<PackageReference Include="BCrypt.Net-Next" Version="2.1.1" />
2727
<PackageReference Include="Elmah.Io.AspNetCore" Version="3.2.39-pre" />
28+
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.0.0" />
2829
<PackageReference Include="Newtonsoft.Json" Version="10.0.1" />
2930
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.3.0" />
3031
<ProjectReference Include="..\Encryptamajig\Encryptamajig.csproj" />

src/Devlord.Utilities/DevlordSettings.cs renamed to src/Devlord.Utilities/DevlordOptions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@
1818

1919
namespace Devlord.Utilities
2020
{
21-
public class DevlordSettings
21+
public class DevlordOptions
2222
{
2323
public string GoogleMapsApiKey { get; set; }
2424
private static readonly IConfiguration _configuration = GetConfig();
2525

26-
public static DevlordSettings Default { get; } = new DevlordSettings();
26+
public static DevlordOptions Default { get; } = new DevlordOptions();
2727

2828
public int SmtpPort => int.Parse(GetValue("SmtpPort"));
2929
public string SmtpLogin => GetValue("SmtpLogin");
3030
public string SmtpPassword => GetValue("SmtpPassword");
31-
31+
public string SmtpServer { get; set; }
32+
3233
private static IConfiguration GetConfig()
3334
{
3435
var builder = new ConfigurationBuilder()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// --------------------------------------------------------------------------------------------------------------------
2+
// <copyright file="DevlordSettingsExtension.cs" company="Lord Design">
3+
// © 2022 Lord Design
4+
// </copyright>
5+
// <license type="GPL-3.0">
6+
// You may use freely and commercially without modification; if you make changes, please share back to the
7+
// community.
8+
// </license>
9+
// <author>Aaron Lord</author>
10+
// --------------------------------------------------------------------------------------------------------------------
11+
12+
using Devlord.Utilities;
13+
using Microsoft.Extensions.Configuration;
14+
15+
namespace Microsoft.Extensions.DependencyInjection
16+
{
17+
public static class DevlordSettingsExtension
18+
{
19+
public static IServiceCollection AddDevlordUtilities(this IServiceCollection services,
20+
IConfiguration namedConfigurationSection)
21+
{
22+
services.Configure<DevlordOptions>(namedConfigurationSection);
23+
services.AddSingleton<IMailbotFactory, MailbotFactory>();
24+
25+
return services;
26+
}
27+
}
28+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// --------------------------------------------------------------------------------------------------------------------
2+
// <copyright file="IMailbotFactory.cs" company="Lord Design">
3+
// © 2022 Lord Design
4+
// </copyright>
5+
// <license type="GPL-3.0">
6+
// You may use freely and commercially without modification; if you make changes, please share back to the
7+
// community.
8+
// </license>
9+
// <author>Aaron Lord</author>
10+
// --------------------------------------------------------------------------------------------------------------------
11+
12+
namespace Devlord.Utilities
13+
{
14+
public interface IMailbotFactory
15+
{
16+
Mailbot GetMailbot(string smtpServer);
17+
}
18+
}

src/Devlord.Utilities/Mailbot.Core.cs

Lines changed: 0 additions & 65 deletions
This file was deleted.

src/Devlord.Utilities/Mailbot.Framework.cs

Lines changed: 0 additions & 74 deletions
This file was deleted.

src/Devlord.Utilities/Mailbot.cs

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,37 @@
1010
// <author>aaron@lorddesign.net</author>
1111
// --------------------------------------------------------------------------------------------------------------------
1212

13-
using System.Collections.Generic;
1413
using System.Threading.Tasks;
1514
using Devlord.Utilities.Cryptography;
15+
using MailKit.Net.Smtp;
16+
using MailKit.Security;
17+
using MimeKit;
1618

1719
namespace Devlord.Utilities
1820
{
1921
/// <summary>
2022
/// Multi-threaded mailer to keep your systems running by sending messages asynchronously. If you're using it in a
2123
/// service that sends a lot of messages, be aware that you'll be limited by your .NET working threads.
2224
/// </summary>
23-
public partial class Mailbot
25+
public class Mailbot
2426
{
25-
private static readonly object DictionaryLock = new object();
27+
#region Fields
2628

27-
private static readonly Dictionary<string, Mailbot> Instances = new Dictionary<string, Mailbot>();
29+
private readonly Crypt _crypt = new Crypt();
2830

31+
/// <summary>
32+
/// The throttles.
33+
/// </summary>
34+
internal Throttles Throttles = new Throttles();
35+
36+
#endregion
37+
2938
#region Constructors and Destructors
3039

3140
/// <summary>
3241
/// Private constructor to enforce use of singleton.
3342
/// </summary>
34-
private Mailbot()
43+
internal Mailbot()
3544
{
3645
_crypt.Key = new byte[]
3746
{
@@ -51,51 +60,49 @@ private Mailbot()
5160
/// <summary>
5261
/// Gets the SMTP server.
5362
/// </summary>
54-
public string SmtpServer { get; private set; }
63+
public string SmtpServer { get; internal set; }
64+
65+
public int SmtpPort { get; internal set; }
66+
public string SmtpLogin { get; internal set; }
67+
public string EncryptedPassword { get; internal set; }
5568

5669
#endregion
5770

58-
// Per minute 180, Per hour 3600, Per day 10,000
71+
#region Methods
5972

73+
// Per minute 180, Per hour 3600, Per day 10,000
6074
public async Task QueueMail(Botmail message)
6175
{
6276
await SendMail(message);
6377
}
6478

65-
/// <summary>
66-
/// Gets the instance.
67-
/// </summary>
68-
public static Mailbot GetInstance(string smtpServer)
79+
80+
// Per minute 180, Per hour 3600, Per day 10,000
81+
private async Task SendMail(Botmail botmail)
6982
{
70-
smtpServer = smtpServer.ToLower();
71-
lock (DictionaryLock)
83+
Throttles.Wait();
84+
85+
var mm = new MimeMessage();
86+
mm.From.Add(new MailboxAddress(botmail.Addresser));
87+
mm.Subject = botmail.Subject;
88+
mm.Body = new TextPart(botmail.Format.ToString().ToLower())
89+
{
90+
Text = botmail.Body
91+
};
92+
93+
botmail.Addressees.ForEach(a => mm.To.Add(new MailboxAddress(a)));
94+
using (var client = new SmtpClient())
7295
{
73-
if (Instances.ContainsKey(smtpServer))
74-
{
75-
return Instances[smtpServer];
76-
}
77-
78-
var instance = new Mailbot { SmtpServer = smtpServer };
79-
if (smtpServer == "smtp.gmail.com")
80-
{
81-
instance._throttles = new GmailThrottles();
82-
}
83-
84-
instance.SmtpServer = smtpServer;
85-
Instances.Add(smtpServer, instance);
86-
return instance;
96+
client.Connect(SmtpServer, SmtpPort, SecureSocketOptions.SslOnConnect);
97+
await client.AuthenticateAsync(SmtpLogin,
98+
_crypt.DecryptSecret(EncryptedPassword));
99+
await client.SendAsync(mm);
100+
Throttles.Increment();
101+
client.Disconnect(true);
87102
}
88103
}
89104

90-
#region Fields
91-
92-
private readonly Crypt _crypt = new Crypt();
93-
94-
/// <summary>
95-
/// The throttles.
96-
/// </summary>
97-
private Throttles _throttles = new Throttles();
98-
99105
#endregion
106+
100107
}
101108
}

0 commit comments

Comments
 (0)