Sleek Sleek - 4 months ago 30
ASP.NET (C#) Question

ASP.NET Boilerplate change EmailSettingNames

According to the documentation on Email Sending:

Email Sender uses setting management system to read email sending
configuration. All setting names are defined in
Abp.Net.Mail.EmailSettingNames class as constant strings.

I need pointers on how to override these default settings for values and use my own.

I save my settings in the Settings table too, but I do not use the name "Abp.Net.Mail.DefaultFromAddress", I use something like "Tenant.DefaultFromAddress".

If possible, I want to override only the email settings without tampering with other Abp settings.

Thank you.


My objective was to find a way to override these settings and use a different naming convention in the Settings table because the requirements of my application dictates that I do.

I just want it to be called differently but behave the same way.

Answer Source

My objective was to find a way to override these settings and use a different naming convention in the Settings table

It takes more than a few lines.

  1. Implement your own EmailSettingNames:

    public static class MyEmailSettingNames
        public const string DefaultFromAddress = "Tenant.DefaultFromAddress";
        public const string DefaultFromDisplayName = "Tenant.DefaultFromDisplayName";
        public static class Smtp
            public const string Host = "Tenant.Smtp.Host";
            public const string Port = "Tenant.Smtp.Port";
            public const string UserName = "Tenant.Smtp.UserName";
            public const string Password = "Tenant.Smtp.Password";
            public const string Domain = "Tenant.Smtp.Domain";
            public const string EnableSsl = "Tenant.Smtp.EnableSsl";
            public const string UseDefaultCredentials = "Tenant.Smtp.UseDefaultCredentials";
  2. Implement your own EmailSettingProvider:

    internal class MyEmailSettingProvider : SettingProvider
        public override IEnumerable<SettingDefinition> GetSettingDefinitions(SettingDefinitionProviderContext context)
            return new[]
                new SettingDefinition(MyEmailSettingNames.Smtp.Host, "", L("SmtpHost"), scopes: SettingScopes.Application | SettingScopes.Tenant),
                new SettingDefinition(MyEmailSettingNames.Smtp.Port, "25", L("SmtpPort"), scopes: SettingScopes.Application | SettingScopes.Tenant),
                new SettingDefinition(MyEmailSettingNames.Smtp.UserName, "", L("Username"), scopes: SettingScopes.Application | SettingScopes.Tenant),
                new SettingDefinition(MyEmailSettingNames.Smtp.Password, "", L("Password"), scopes: SettingScopes.Application | SettingScopes.Tenant),
                new SettingDefinition(MyEmailSettingNames.Smtp.Domain, "", L("DomainName"), scopes: SettingScopes.Application | SettingScopes.Tenant),
                new SettingDefinition(MyEmailSettingNames.Smtp.EnableSsl, "false", L("UseSSL"), scopes: SettingScopes.Application | SettingScopes.Tenant),
                new SettingDefinition(MyEmailSettingNames.Smtp.UseDefaultCredentials, "true", L("UseDefaultCredentials"), scopes: SettingScopes.Application | SettingScopes.Tenant),
                new SettingDefinition(MyEmailSettingNames.DefaultFromAddress, "", L("DefaultFromSenderEmailAddress"), scopes: SettingScopes.Application | SettingScopes.Tenant),
                new SettingDefinition(MyEmailSettingNames.DefaultFromDisplayName, "", L("DefaultFromSenderDisplayName"), scopes: SettingScopes.Application | SettingScopes.Tenant)
        private static LocalizableString L(string name)
            return new LocalizableString(name, MyLocalizationSourceName);
  3. Implement your own SmtpEmailSenderConfiguration:

    public class MySmtpEmailSenderConfiguration : EmailSenderConfiguration, ISmtpEmailSenderConfiguration, ITransientDependency
        public virtual string Host => GetNotEmptySettingValue(MyEmailSettingNames.Smtp.Host);
        public virtual int Port => SettingManager.GetSettingValue<int>(MyEmailSettingNames.Smtp.Port);
        public virtual string UserName => GetNotEmptySettingValue(MyEmailSettingNames.Smtp.UserName);
        public virtual string Password => GetNotEmptySettingValue(MyEmailSettingNames.Smtp.Password);
        public virtual string Domain => SettingManager.GetSettingValue(MyEmailSettingNames.Smtp.Domain);
        public virtual bool EnableSsl => SettingManager.GetSettingValue<bool>(MyEmailSettingNames.Smtp.EnableSsl);
        public virtual bool UseDefaultCredentials => SettingManager.GetSettingValue<bool>(MyEmailSettingNames.Smtp.UseDefaultCredentials);
        public MySmtpEmailSenderConfiguration(ISettingManager settingManager)
            : base(settingManager)
  4. Configure these in the PreInitialize method of YourProjectNameCoreModule:

    Configuration.ReplaceService<ISmtpEmailSenderConfiguration, MySmtpEmailSenderConfiguration>(DependencyLifeStyle.Transient);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download