{"record":{"id":"e90a7952f252bea6","repo":"nopSolutions/nopCommerce","slug":"email-account-could-not-be-loaded","errorCode":null,"errorMessage":"Email account could not be loaded","messagePattern":"Email account could not be loaded","errorType":"exception","errorClass":"NopException","httpStatus":null,"severity":"critical","filePath":"src/Libraries/Nop.Services/Messages/SmtpBuilder.cs","lineNumber":123,"sourceCode":"        return new SaslMechanismOAuth2(emailAccount.Email, authToken.AccessToken);\n    }\n\n    #endregion\n\n    #region Methods\n\n    /// <summary>\n    /// Create a new SMTP client for a specific email account\n    /// </summary>\n    /// <param name=\"emailAccount\">Email account to use. If null, then would be used EmailAccount by default</param>\n    /// <returns>\n    /// A task that represents the asynchronous operation\n    /// The task result contains the an SMTP client that can be used to send email messages\n    /// </returns>\n    public virtual async Task<SmtpClient> BuildAsync(EmailAccount emailAccount = null)\n    {\n        emailAccount ??= await _emailAccountService.GetEmailAccountByIdAsync(_emailAccountSettings.DefaultEmailAccountId)\n                         ?? throw new NopException(\"Email account could not be loaded\");\n\n        var client = new SmtpClient\n        {\n            ServerCertificateValidationCallback = ValidateServerCertificate\n        };\n\n        try\n        {\n            await client.ConnectAsync(\n                emailAccount.Host,\n                emailAccount.Port,\n                emailAccount.EnableSsl ? SecureSocketOptions.SslOnConnect : SecureSocketOptions.StartTlsWhenAvailable);\n\n            switch (emailAccount.EmailAuthenticationMethod)\n            {\n                case EmailAuthenticationMethod.Login:\n                    await client.AuthenticateAsync(new SaslMechanismLogin(emailAccount.Username, emailAccount.Password));\n                    break;","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/nopSolutions/nopCommerce/blob/64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2/src/Libraries/Nop.Services/Messages/SmtpBuilder.cs#L105-L141","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["In admin (Configuration > Email accounts), set a valid account as the default.","Verify the EmailAccount referenced by DefaultEmailAccountId exists.","Re-seed email account settings during installation so DefaultEmailAccountId points at a real account.","Pass an explicit emailAccount to BuildAsync when the default may be unavailable."],"exampleFix":"// before\nemailAccount ??= await _emailAccountService.GetEmailAccountByIdAsync(_emailAccountSettings.DefaultEmailAccountId)\n    ?? throw new NopException(\"Email account could not be loaded\");\n\n// after - fall back to first account with a clear error\nemailAccount ??= await _emailAccountService.GetEmailAccountByIdAsync(_emailAccountSettings.DefaultEmailAccountId)\n    ?? (await _emailAccountService.GetAllEmailAccountsAsync()).FirstOrDefault()\n    ?? throw new NopException(\"No email account configured. Set a default email account in admin.\");","handlingStrategy":"validation","validationCode":"// Ensure the configured default email account is resolvable before sending\nvar defaultAccount = await _emailAccountService.GetEmailAccountByIdAsync(_emailAccountSettings.DefaultEmailAccountId);\nif (defaultAccount is null)\n    throw new InvalidOperationException(\"Default email account is missing. Set a valid default in admin.\");","typeGuard":"static async Task<bool> DefaultEmailAccountExistsAsync(IEmailAccountService svc, IEmailAccountSettings s)\n    => await svc.GetEmailAccountByIdAsync(s.DefaultEmailAccountId) is not null;","tryCatchPattern":"try { var client = await smtpBuilder.BuildAsync(); }\ncatch (NopException ex) when (ex.Message == \"Email account could not be loaded\")\n{ /* set a valid default account in admin, then retry; or pass an explicit account */ }","preventionTips":["Always configure a valid default email account after installation.","When deleting an account, reassign DefaultEmailAccountId first.","Pass an explicit emailAccount to BuildAsync when the default may be unavailable."],"tags":["email","smtp","email-account","configuration","settings","nopcommerce"],"backgroundTag":null,"analyzedSha":"64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2","analyzedAt":"2026-08-13T21:19:38.062Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}