I am using aspNetBoilerplate to send an email (or attempt to send an email)
I have added the settings to the database using the Default Settings Creator in the seed data like so
namespace ESMPortal.EntityFrameworkCore.Seed.Host
{
public class DefaultSettingsCreator
{
private readonly ESMPortalDbContext _context;
public DefaultSettingsCreator(ESMPortalDbContext context)
{
_context = context;
}
public void Create()
{
// Emailing
AddSettingIfNotExists(EmailSettingNames.DefaultFromAddress, "system@elite-sports-management.co.uk");
AddSettingIfNotExists(EmailSettingNames.DefaultFromDisplayName, "Elite Sports Management");
AddSettingIfNotExists(EmailSettingNames.Smtp.Host, "mail.x.co.uk");
AddSettingIfNotExists(EmailSettingNames.Smtp.Password, "xxx");
AddSettingIfNotExists(EmailSettingNames.Smtp.UserName, "system@x.co.uk");
AddSettingIfNotExists(EmailSettingNames.Smtp.EnableSsl, "true");
// Languages
AddSettingIfNotExists(LocalizationSettingNames.DefaultLanguage, "en");
}
private void AddSettingIfNotExists(string name, string value, int? tenantId = null)
{
if (_context.Settings.IgnoreQueryFilters().Any(s => s.Name == name && s.TenantId == tenantId && s.UserId == null))
{
return;
}
_context.Settings.Add(new Setting(tenantId, null, name, value));
_context.SaveChanges();
}
}
}
I have confirmed that these settings are added to the database when the project runs
However when i attempt to send an email i get an error stating that the server at 127.0.0.1:25 did not respond. This obviously means that the settings in the database arn't being used.
This is further confirmed when i dont include a from address when sending an email. I receive an error saying that the DefaultFromAddress is null or empty. This is not the case as i have confirmed it is present in the database settings table.
Here is the code that im using to send the email. All the above errors are generated in the catch section of the try statement
try
{
_emailSender.Send(
to: userId.EmailAddress,
from: "system@x.co.uk",
subject: "New Upload",
body: $"A new upload is assigned to you: <b>{upload.DisplayName}</b>",
isBodyHtml: true
);
}
catch (Exception e)
{
throw;
}
It turns out that i wasn't adding the settings correctly. I had added them to the seed data and although it was visible in the database it was not returned when queried. Adding them via a custom setting provider solved the problem.
namespace ESMPortal.Settings
{
public class CustomSettingProvider : SettingProvider
{
public override IEnumerable<SettingDefinition> GetSettingDefinitions(SettingDefinitionProviderContext context)
{
return new[]
{
new SettingDefinition(
"Abp.Net.Mail.Smtp.Host",
"mail.x.co.uk"
),
new SettingDefinition(
"Abp.Net.Mail.DefaultFromAddress",
"x@x.co.uk",
scopes: SettingScopes.User,
clientVisibilityProvider: new VisibleSettingClientVisibilityProvider()
)
,
new SettingDefinition(
"Abp.Net.Mail.DefaultFromDisplayName",
"XXX",
scopes: SettingScopes.User,
clientVisibilityProvider: new VisibleSettingClientVisibilityProvider()
)
,
new SettingDefinition(
"Abp.Net.Mail.Smtp.UserName",
"x@x.co.uk",
scopes: SettingScopes.User,
clientVisibilityProvider: new VisibleSettingClientVisibilityProvider()
)
,
new SettingDefinition(
"Abp.Net.Mail.Smtp.Password",
"XXX",
scopes: SettingScopes.User,
clientVisibilityProvider: new VisibleSettingClientVisibilityProvider()
)
// ,
// new SettingDefinition(
//"Abp.Net.Mail.Smtp.Domain",
//"x.co.uk",
//scopes: SettingScopes.User,
//clientVisibilityProvider: new VisibleSettingClientVisibilityProvider()
//)
// ,
// new SettingDefinition(
//"Abp.Net.Mail.Smtp.EnableSsl",
//"true",
//scopes: SettingScopes.User,
//clientVisibilityProvider: new VisibleSettingClientVisibilityProvider()
//)
,
new SettingDefinition(
"Abp.Net.Mail.Smtp.UseDefaultCredentials",
"false",
scopes: SettingScopes.User,
clientVisibilityProvider: new VisibleSettingClientVisibilityProvider()
)
};
}
}
}
And this in the PreInitialize() of the module
//custom settings provider
Configuration.Settings.Providers.Add<AppSettingProvider>();