nopSolutions/nopCommerce · critical · NopException

Email account could not be loaded

Error message

Email account could not be loaded

What it means

Thrown by SmtpBuilder.BuildAsync when constructing an SmtpClient. If no emailAccount is passed, it loads the default account by _emailAccountSettings.DefaultEmailAccountId; if that yields null, no SMTP account is available, so it throws NopException. This indicates the default email account setting points at a deleted/nonexistent account or was never configured.

Source

Thrown at src/Libraries/Nop.Services/Messages/SmtpBuilder.cs:123

        return new SaslMechanismOAuth2(emailAccount.Email, authToken.AccessToken);
    }

    #endregion

    #region Methods

    /// <summary>
    /// Create a new SMTP client for a specific email account
    /// </summary>
    /// <param name="emailAccount">Email account to use. If null, then would be used EmailAccount by default</param>
    /// <returns>
    /// A task that represents the asynchronous operation
    /// The task result contains the an SMTP client that can be used to send email messages
    /// </returns>
    public virtual async Task<SmtpClient> BuildAsync(EmailAccount emailAccount = null)
    {
        emailAccount ??= await _emailAccountService.GetEmailAccountByIdAsync(_emailAccountSettings.DefaultEmailAccountId)
                         ?? throw new NopException("Email account could not be loaded");

        var client = new SmtpClient
        {
            ServerCertificateValidationCallback = ValidateServerCertificate
        };

        try
        {
            await client.ConnectAsync(
                emailAccount.Host,
                emailAccount.Port,
                emailAccount.EnableSsl ? SecureSocketOptions.SslOnConnect : SecureSocketOptions.StartTlsWhenAvailable);

            switch (emailAccount.EmailAuthenticationMethod)
            {
                case EmailAuthenticationMethod.Login:
                    await client.AuthenticateAsync(new SaslMechanismLogin(emailAccount.Username, emailAccount.Password));
                    break;

View on GitHub (pinned to 64bdf2ff08)

Solutions

  1. In admin (Configuration > Email accounts), set a valid account as the default.
  2. Verify the EmailAccount referenced by DefaultEmailAccountId exists.
  3. Re-seed email account settings during installation so DefaultEmailAccountId points at a real account.
  4. Pass an explicit emailAccount to BuildAsync when the default may be unavailable.

Example fix

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

// after - fall back to first account with a clear error
emailAccount ??= await _emailAccountService.GetEmailAccountByIdAsync(_emailAccountSettings.DefaultEmailAccountId)
    ?? (await _emailAccountService.GetAllEmailAccountsAsync()).FirstOrDefault()
    ?? throw new NopException("No email account configured. Set a default email account in admin.");
Defensive patterns

Strategy: validation

Validate before calling

// Ensure the configured default email account is resolvable before sending
var defaultAccount = await _emailAccountService.GetEmailAccountByIdAsync(_emailAccountSettings.DefaultEmailAccountId);
if (defaultAccount is null)
    throw new InvalidOperationException("Default email account is missing. Set a valid default in admin.");

Type guard

static async Task<bool> DefaultEmailAccountExistsAsync(IEmailAccountService svc, IEmailAccountSettings s)
    => await svc.GetEmailAccountByIdAsync(s.DefaultEmailAccountId) is not null;

Try / catch

try { var client = await smtpBuilder.BuildAsync(); }
catch (NopException ex) when (ex.Message == "Email account could not be loaded")
{ /* set a valid default account in admin, then retry; or pass an explicit account */ }

Prevention

When it happens

Trigger: Sending any email (order notifications, message templates) when DefaultEmailAccountId is unset or references a deleted EmailAccount; calling BuildAsync() with no argument and no valid default.

Common situations: The configured default email account was deleted but the setting still references its old Id; fresh install where DefaultEmailAccountId was never set; migration that changed Ids without updating the setting.

Related errors


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