nopSolutions/nopCommerce · warning · NopException
Account.EmailUsernameErrors.EmailTooLong
Error message
Account.EmailUsernameErrors.EmailTooLong
What it means
Thrown by SetEmailAsync when the trimmed email length exceeds 100 characters, using localized resource 'Account.EmailUsernameErrors.EmailTooLong'. The 100-char ceiling is a hard-coded schema/business constraint on the Customer.Email column.
Source
Thrown at src/Libraries/Nop.Services/Customers/CustomerRegistrationService.cs:511
/// <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);
}
else
{
customer.Email = newEmail;
View on GitHub (pinned to 64bdf2ff08)
Solutions
- Enforce maxlength=100 on the email input in forms and API models.
- Validate length before calling: if (newEmail.Trim().Length > 100) report error.
- If the business requires longer emails, this is a hard-coded limit — file a feature change rather than silently truncating.
Example fix
// before
await _customerRegistrationService.SetEmailAsync(customer, email, requireValidation);
// after
email = email?.Trim();
if (email is { Length: > 100 })
ModelState.AddModelError(nameof(email), "Email must be 100 characters or fewer.");
else
await _customerRegistrationService.SetEmailAsync(customer, email, requireValidation); Defensive patterns
Strategy: validation
Validate before calling
var trimmed = newEmail?.Trim();
if (trimmed is { Length: > 100 })
ModelState.AddModelError(nameof(newEmail), "Email must be <= 100 chars."); Type guard
static bool IsEmailWithinLimit(string email) => email is null || email.Trim().Length <= 100;
Try / catch
try { await _customerRegistrationService.SetEmailAsync(customer, email, true); }
catch (NopException ex) { ModelState.AddModelError(nameof(email), ex.Message); } Prevention
- Set maxlength=100 on email inputs.
- Trim before length checks.
- Validate at the model layer with [StringLength(100)].
When it happens
Trigger: Calling SetEmailAsync with newEmail whose trimmed length > 100. Happens with very long local parts or unusually long domain segments, or when extra whitespace/content is concatenated.
Common situations: User pastes a malformed long string; an integration syncs a CRM record whose email field is oversized; test data with no length cap.
Related errors
- Account.EmailUsernameErrors.NewEmailIsNotValid
- 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/a431692638ac12bf.
Report an issue: GitHub.