Open Closed

Why is the above code unable to correctly retrieve EmailSettingNames.DefaultFromAddress? #10069


User avatar
0
rexhxiao created

internal class Program { private static async Task Main(string[] args) { using var app = await AbpApplicationFactory.CreateAsync<AbpEmailingModule>();

    await app.InitializeAsync();

    var settingProvider = app.ServiceProvider.GetRequiredService&lt;ISettingProvider&gt;();

    var test = await settingProvider.GetOrNullAsync(EmailSettingNames.DefaultFromAddress);
}

}

Unhandled exception. Volo.Abp.AbpException: Undefined setting: Abp.Mailing.DefaultFromAddress at Volo.Abp.Settings.SettingDefinitionManager.GetAsync(String name) at Volo.Abp.Settings.SettingProvider.GetOrNullAsync(String name) at TwAbp.Emailing.Surenotify.ConsoleTest.Program.Main(String[] args) in /Users/rex/Documents/surenotify-emailing/TwAbp.Emailing.Surenotify/TwAbp.Emailing.Surenotify.ConsoleTest/Program.cs:line 19 at TwAbp.Emailing.Surenotify.ConsoleTest.Program.<Main>(String[] args)

Process finished with exit code 134.


4 Answer(s)
  • User Avatar
    0
    AI-Bot created
    Support Team ABP Agent
    1. Solution
    • Root cause: You created a minimal app with only AbpEmailingModule. The setting system throws “Undefined setting: Abp.Mailing.DefaultFromAddress” because the Email settings are defined by the Emailing module but are registered through module initialization and typically read from configuration/setting stores. In a console app created this way, you must:

      • Ensure the Emailing module is added and initialized.
      • Provide values for the email settings via appsettings.json (Settings section) or a setting store before you try to read them.
      • Make sure the virtual file system and configuration are available to the module.
    • Fix (recommended simple approach): Add an appsettings.json next to your console app and load it when creating the application so ABP’s configuration system can populate settings.

    using Microsoft.Extensions.Configuration;
      using Volo.Abp;
      using Volo.Abp.Emailing;
      using Volo.Abp.Settings;
    
      internal class Program
      {
          private static async Task Main(string[] args)
          {
              // Build configuration that includes appsettings.json
              var configuration = new ConfigurationBuilder()
                  .AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
                  .AddEnvironmentVariables()
                  .Build();
    
              // Create Abp application with the configuration
              using var app = await AbpApplicationFactory.CreateAsync<AbpEmailingModule>(options =>
              {
                  options.Services.ReplaceConfiguration(configuration);
              });
    
              await app.InitializeAsync();
    
              var settingProvider = app.ServiceProvider.GetRequiredService<ISettingProvider>();
    
              // Now this will work if setting exists in config or store
              var from = await settingProvider.GetOrNullAsync(EmailSettingNames.DefaultFromAddress);
              Console.WriteLine(from ?? "No value found");
          }
      }
    

    appsettings.json:

    {
        "Settings": {
          "Abp.Mailing.DefaultFromAddress": "noreply@yourapp.com",
          "Abp.Mailing.DefaultFromDisplayName": "Your App",
          "Abp.Mailing.Smtp.Host": "127.0.0.1",
          "Abp.Mailing.Smtp.Port": "25",
          "Abp.Mailing.Smtp.UseDefaultCredentials": "true"
        }
      }
    
    • Alternative: Set the setting programmatically before reading it (e.g., when you don’t want to use configuration). Note: Password must be stored encrypted normally; shown here for the DefaultFromAddress only:
    var settingManager = app.ServiceProvider.GetRequiredService<ISettingManager>();
      await settingManager.SetGlobalAsync(EmailSettingNames.DefaultFromAddress, "noreply@yourapp.com");
    
      var from2 = await settingProvider.GetOrNullAsync(EmailSettingNames.DefaultFromAddress);
    
    • Notes:
      • If you also need to manage settings via database UI, include Setting Management modules in a real app. For a console-only test, configuration or programmatic set is sufficient.
      • Ensure you are referencing Volo.Abp.Emailing and that AbpEmailingModule is included (you already do).
      • The error “Undefined setting” means the setting definition wasn’t available to the SettingDefinitionManager at the time of resolution or no configuration was provided; wiring configuration as shown resolves it.
    1. Sources
    • https://docs.abp.io/en/abp/latest/Emailing
    • https://docs.abp.io/en/abp/latest/Settings
    • https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.Emailing/Volo/Abp/Emailing/EmailSettingNames.cs

    This response was generated by AI. If it didn’t solve your problem, feel free to post a new answer — our support team will jump in to help you soon.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Can you add the AutoFac module dependency?

    https://abp.io/docs/latest/framework/fundamentals/autofac-integration

    Thanks.

  • User Avatar
    0
    rexhxiao created

    That is a built-in module in ABP. I think I found the reason. You must execute:

    options.UseAutofac();

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Yes, AutoFac will support more advanced features.

    Thanks.

Learn More, Pay Less
33% OFF
All Trainings!
Get Your Deal
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.2.0-preview. Updated on January 09, 2026, 07:22
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.