简体   繁体   English

ASP.NET样板更改EmailSettingNames

[英]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. 所有设置名称在Abp.Net.Mail.EmailSettingNames类中均定义为常量字符串。

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". 我也将设置保存在“设置”表中,但是我不使用名称“ Abp.Net.Mail.DefaultFromAddress”,而是使用“ Tenant.DefaultFromAddress”之类的名称。

If possible, I want to override only the email settings without tampering with other Abp settings. 如果可能的话,我只想覆盖电子邮件设置,而不篡改其他Abp设置。

Thank you. 谢谢。

Clarification 澄清

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. 我只是希望它的名称不同,但行为方式相同。

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 : 实现自己的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 : 实现自己的EmailSettingProvider

     internal class MyEmailSettingProvider : SettingProvider { public override IEnumerable<SettingDefinition> GetSettingDefinitions(SettingDefinitionProviderContext context) { return new[] { new SettingDefinition(MyEmailSettingNames.Smtp.Host, "127.0.0.1", 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 : 实现自己的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 : YourProjectNameCoreModulePreInitialize方法中进行配置:

     Configuration.Settings.Providers.Add<MyEmailSettingProvider>(); Configuration.ReplaceService<ISmtpEmailSenderConfiguration, MySmtpEmailSenderConfiguration>(DependencyLifeStyle.Transient); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM