2

我正在使用 aspNetBoilerplate 发送电子邮件(或尝试发送电子邮件)

我已经使用种子数据中的默认设置创建器将设置添加到数据库中,如下所示

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();
        }
    }
}

我已经确认这些设置在项目运行时添加到数据库中

但是,当我尝试发送电子邮件时,我收到一条错误消息,指出 127.0.0.1:25 的服务器没有响应。这显然意味着数据库中的设置没有被使用。

当我在发送电子邮件时不包含发件人地址时,这一点得到了进一步证实。我收到一条错误消息,提示 DefaultFromAddress 为空或为空。情况并非如此,因为我已确认它存在于数据库设置表中。

这是我用来发送电子邮件的代码。以上所有错误都是在try语句的catch部分产生的

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;
            }
4

1 回答 1

2

事实证明,我没有正确添加设置。我已将它们添加到种子数据中,尽管它在数据库中可见,但在查询时并未返回。通过自定义设置提供程序添加它们解决了这个问题。

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()
                        )
                };
        }
    }
}

这在模块的 PreInitialize() 中

    //custom settings provider
    Configuration.Settings.Providers.Add<AppSettingProvider>();
于 2019-06-24T18:01:46.237 回答