根据Email Sending上的文档:
电子邮件发件人使用设置管理系统读取电子邮件发送
组态。所有设置名称均在
Abp.Net.Mail.EmailSettingNames类为常量字符串。
我需要有关如何覆盖这些默认设置的值并使用我自己的指针。
我也将设置保存在“设置”表中,但是我不使用名称“ Abp.Net.Mail.DefaultFromAddress”,而是使用类似“ Tenant.DefaultFromAddress”的名称。
如果可能的话,我只想覆盖电子邮件设置,而不篡改其他Abp设置。
谢谢。
澄清度
我的目标是找到一种方法来覆盖这些设置,并在“设置”表中使用不同的命名约定,因为应用程序的要求决定了我这样做。
我只是希望它的名称不同,但行为方式相同。
最佳答案
我的目标是找到一种方法来覆盖这些设置,并在“设置”表中使用其他命名约定
它需要几行。
实现自己的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";
}
}
实现自己的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);
}
}
实现自己的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)
{
}
}
在YourProjectNameCoreModule的
PreInitialize
方法中配置它们:Configuration.Settings.Providers.Add<MyEmailSettingProvider>();
Configuration.ReplaceService<ISmtpEmailSenderConfiguration, MySmtpEmailSenderConfiguration>(DependencyLifeStyle.Transient);