nopSolutions/nopCommerce · warning · NopException
Account.EmailUsernameErrors.NewEmailIsNotValid
Error message
Account.EmailUsernameErrors.NewEmailIsNotValid
What it means
Thrown by SetEmailAsync when CommonHelper.IsValidEmail(newEmail) returns false, using the localized resource 'Account.EmailUsernameErrors.NewEmailIsNotValid'. The message text is user-facing and depends on the localization resource being present; the exception type is NopException.
Source
Thrown at src/Libraries/Nop.Services/Customers/CustomerRegistrationService.cs:508
/// <summary>
/// Sets a user email
/// </summary>
/// <param name="customer">Customer</param>
/// <param name="newEmail">New email</param>
/// <param name="requireValidation">Require validation of new email address</param>
/// <returns>A task that represents the asynchronous operation</returns>
public virtual async Task SetEmailAsync(Customer customer, string newEmail, bool requireValidation)
{
ArgumentNullException.ThrowIfNull(customer);
if (newEmail == null)
throw new NopException("Email cannot be null");
newEmail = newEmail.Trim();
var oldEmail = customer.Email;
if (!CommonHelper.IsValidEmail(newEmail))
throw new NopException(await _localizationService.GetResourceAsync("Account.EmailUsernameErrors.NewEmailIsNotValid"));
if (newEmail.Length > 100)
throw new NopException(await _localizationService.GetResourceAsync("Account.EmailUsernameErrors.EmailTooLong"));
var customer2 = await _customerService.GetCustomerByEmailAsync(newEmail);
if (customer2 != null && customer.Id != customer2.Id)
throw new NopException(await _localizationService.GetResourceAsync("Account.EmailUsernameErrors.EmailAlreadyExists"));
if (requireValidation)
{
//re-validate email
customer.EmailToRevalidate = newEmail;
await _customerService.UpdateCustomerAsync(customer);
//email re-validation message
await _genericAttributeService.SaveAttributeAsync(customer, NopCustomerDefaults.EmailRevalidationTokenAttribute, Guid.NewGuid().ToString());
await _workflowMessageService.SendCustomerEmailRevalidationMessageAsync(customer, (await _workContext.GetWorkingLanguageAsync()).Id);
}
View on GitHub (pinned to 64bdf2ff08)
Solutions
- Validate email format client-side and server-side before calling SetEmailAsync (e.g., [EmailAddress] attribute, Regex).
- Trim and normalize the email before submission.
- Ensure the localization resource 'Account.EmailUsernameErrors.NewEmailIsNotValid' exists for all active languages so users see a meaningful message.
- Catch NopException at the controller/UI layer and show it as a friendly validation error rather than a 500.
Example fix
// before
await _customerRegistrationService.SetEmailAsync(customer, newEmail, requireValidation);
// after
if (!CommonHelper.IsValidEmail(newEmail))
ModelState.AddModelError(nameof(newEmail), "Please enter a valid email address.");
else
await _customerRegistrationService.SetEmailAsync(customer, newEmail, requireValidation); Defensive patterns
Strategy: validation
Validate before calling
if (!CommonHelper.IsValidEmail(newEmail))
ModelState.AddModelError(nameof(newEmail), "Invalid email format.");
else
await _customerRegistrationService.SetEmailAsync(customer, newEmail, requireValidation); Type guard
static bool IsValidEmailFormat(string email) => !string.IsNullOrEmpty(email) && CommonHelper.IsValidEmail(email);
Try / catch
try { await _customerRegistrationService.SetEmailAsync(customer, email, true); }
catch (NopException ex) { ModelState.AddModelError(nameof(email), ex.Message); } Prevention
- Validate email format on both client and server before calling SetEmailAsync.
- Ensure the localization resource 'Account.EmailUsernameErrors.NewEmailIsNotValid' is present per language.
- Catch NopException at the controller layer and render as a validation message.
When it happens
Trigger: Calling SetEmailAsync with an email that fails the regex/format validation in CommonHelper.IsValidEmail (missing '@', bad domain, stray characters, etc.). The trimmed email is checked after the null guard.
Common situations: End user typing a malformed email in the account-edit form; an import job feeding unvalidated addresses; a client-side validation bypass (disabled JS) letting bad input reach the server.
Related errors
- Account.EmailUsernameErrors.EmailTooLong
- Account.EmailUsernameErrors.EmailAlreadyExists
- Email cannot be null
- Account.EmailUsernameErrors.UsernameTooLong
- Account.EmailUsernameErrors.UsernameAlreadyExists
AI-assisted analysis of nopSolutions/nopCommerce@64bdf2ff08 (2026-08-13).
Data as JSON: /api/errors/f4c4ae5e3646ff56.
Report an issue: GitHub.