nopSolutions/nopCommerce · error · NopException

Email account could not be loaded

Error message

Email account could not be loaded

What it means

Thrown by CampaignController.GetEmailAccountAsync as a NopException after a two-stage fallback: first by the provided emailAccountId, then by EmailAccountSettings.DefaultEmailAccountId. If both resolve to null, sending a campaign is impossible and it throws. It is a configuration error — no usable email account exists for campaigns.

Source

Thrown at src/Presentation/Nop.Web/Areas/Admin/Controllers/CampaignController.cs:77

        _emailAccountService = emailAccountService;
        _localizationService = localizationService;
        _notificationService = notificationService;
        _newsLetterSubscriptionService = newsLetterSubscriptionService;
        _permissionService = permissionService;
        _storeContext = storeContext;
        _storeService = storeService;
        _workContext = workContext;
    }

    #endregion

    #region Utilities

    protected virtual async Task<EmailAccount> GetEmailAccountAsync(int emailAccountId)
    {
        return await _emailAccountService.GetEmailAccountByIdAsync(emailAccountId)
               ?? await _emailAccountService.GetEmailAccountByIdAsync(_emailAccountSettings.DefaultEmailAccountId)
               ?? throw new NopException("Email account could not be loaded");
    }

    #endregion

    #region Methods

    public virtual IActionResult Index()
    {
        return RedirectToAction("List");
    }

    [CheckPermission(StandardPermission.Promotions.CAMPAIGNS_VIEW)]
    public virtual async Task<IActionResult> List()
    {
        //prepare model
        var model = await _campaignModelFactory.PrepareCampaignSearchModelAsync(new CampaignSearchModel());

        return View(model);

View on GitHub (pinned to 64bdf2ff08)

Solutions

  1. In Admin > Configuration > Email Accounts, create or designate a default email account and save the DefaultEmailAccountId.
  2. Update the campaign's own EmailAccountId field to a valid account before sending.
  3. Run a DB repair aligning EmailAccountSettings.DefaultEmailAccountId with an existing EmailAccount row.

Example fix

// before
return await _emailAccountService.GetEmailAccountByIdAsync(emailAccountId)
       ?? await _emailAccountService.GetEmailAccountByIdAsync(_emailAccountSettings.DefaultEmailAccountId)
       ?? throw new NopException("Email account could not be loaded");

// after (point at the actionable config)
var account = await _emailAccountService.GetEmailAccountByIdAsync(emailAccountId)
            ?? await _emailAccountService.GetEmailAccountByIdAsync(_emailAccountSettings.DefaultEmailAccountId);
if (account == null)
    throw new NopException("Email account could not be loaded. Set a valid DefaultEmailAccountId in Configuration > Email Accounts.");
return account;
Defensive patterns

Strategy: validation

Validate before calling

// Ensure a default email account is configured before any campaign send.
var defaultId = _emailAccountSettings.DefaultEmailAccountId;
if (defaultId == 0 || await _emailAccountService.GetEmailAccountByIdAsync(defaultId) == null)
    throw new NopException("Configure a default email account in Configuration > Email Accounts before sending campaigns.");

Type guard

static async Task<bool> DefaultEmailAccountIsUsable(IEmailAccountService svc, int id)
    => id != 0 && await svc.GetEmailAccountByIdAsync(id) is not null;

Try / catch

try { var account = await GetEmailAccountAsync(emailAccountId); }
catch (NopException ex) when (ex.Message == "Email account could not be loaded")
{
    _notificationService.ErrorNotification("No email account configured. Set a default in Configuration > Email Accounts.");
    return RedirectToAction("List");
}

Prevention

When it happens

Trigger: Any campaign send/preview path that calls GetEmailAccountAsync when both the supplied emailAccountId and the configured DefaultEmailAccountId point at deleted/non-existent EmailAccount rows.

Common situations: Default email account was deleted but EmailAccountSettings.DefaultEmailAccountId still references it; fresh install where no email account was designated as default; multi-store email account misconfiguration.

Related errors


AI-assisted analysis of nopSolutions/nopCommerce@64bdf2ff08 (2026-08-13). Data as JSON: /api/errors/f374945e4695ac29. Report an issue: GitHub.