Skip to content

Instantly share code, notes, and snippets.

@Philo
Last active October 18, 2022 17:22
Show Gist options
  • Save Philo/6810390023ce3165d06728edaf1fbd82 to your computer and use it in GitHub Desktop.
Save Philo/6810390023ce3165d06728edaf1fbd82 to your computer and use it in GitHub Desktop.
Using .net 4.7.1 configuration builders to configuration SmtpClient from AppSettings - https://github.com/aspnet/MicrosoftConfigurationBuilders
public interface ISmtpMailDeliverySettings
{
string Host { get; }
int Port { get; }
string Username { get; }
string Password { get; }
bool EnableSSL { get; }
}
public class SmtpMailDeliverySettings : ISmtpMailDeliverySettings
{
private const string Prefix = "Smtp:";
private readonly NameValueCollection nvc;
public SmtpMailDeliverySettings() : this(ConfigurationManager.AppSettings)
{
}
public SmtpMailDeliverySettings(NameValueCollection nvc)
{
this.nvc = nvc ?? throw new ArgumentNullException(nameof(nvc), "You must supply a NameValueCollection");
}
private int ParseInt(string key, int defaultValue)
{
if (int.TryParse(nvc[key], out var value))
{
return value;
}
return defaultValue;
}
private bool ParseBoolean(string key, bool defaultValue)
{
if (bool.TryParse(nvc[key], out var value))
{
return value;
}
return defaultValue;
}
private string ParseString(string key, string defaultValue = "")
{
var value = nvc[key];
if (!string.IsNullOrWhiteSpace(value))
{
return value;
}
return defaultValue;
}
public string From => ParseString($"{Prefix}{nameof(From)}");
public string Host => ParseString($"{Prefix}{nameof(Host)}");
public int Port => ParseInt($"{Prefix}{nameof(Port)}", 25);
public string Username => ParseString($"{Prefix}{nameof(Username)}");
public string Password => ParseString($"{Prefix}{nameof(Password)}");
public bool EnableSSL => ParseBoolean($"{Prefix}{nameof(EnableSSL)}", false);
}
public class SmtpMailSettingsConfigBuilder : ConfigurationBuilder
{
public SmtpMailSettingsConfigBuilder() : this(ConfigurationManager.AppSettings)
{
}
public SmtpMailSettingsConfigBuilder(NameValueCollection nvc) : this(new SmtpMailDeliverySettings(nvc))
{
}
public SmtpMailSettingsConfigBuilder(ISmtpMailDeliverySettings smtpMailDeliverySettings)
{
}
public override ConfigurationSection ProcessConfigurationSection(ConfigurationSection configSection)
{
if (configSection is SmtpSection smtpSection)
{
var config = new SmtpMailDeliverySettings();
smtpSection.From = config.From;
smtpSection.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpSection.Network.Host = config.Host;
smtpSection.Network.Port = config.Port;
smtpSection.Network.UserName = config.Username;
smtpSection.Network.Password = config.Password;
smtpSection.Network.EnableSsl = config.EnableSSL;
return smtpSection;
}
else
{
return base.ProcessConfigurationSection(configSection);
}
}
}
public async Task SendEmail()
{
using (var smtpClient = new SmtpClient())
{
await smtpClient.SendMailAsync("[email protected]", "[email protected]", "Test", "testing email config");
}
}
<configuration>
<configSections>
<section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" />
</configSections>
<configBuilders>
<builders>
<add name="MailSettingsConfigurationProvider" type="<Namespace-Path-To-Class>.SmtpMailSettingsConfigBuilder, <assembly-name>" />
</builders>
</configBuilders>
<appSettings>
<add key="Smtp:From" value="[email protected]" />
<add key="Smtp:Host" value="mail.localtest.me" />
<add key="Smtp:Port" value="25" />
<add key="Smtp:Username" value="username" />
<add key="Smtp:Password" value="supersecret" />
</appSettings>
<system.net>
<mailSettings>
<smtp configBuilders="MailSettingsConfigurationProvider" />
</mailSettings>
</system.net>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment