{"record":{"id":"a431692638ac12bf","repo":"nopSolutions/nopCommerce","slug":"account-emailusernameerrors-emailtoolong","errorCode":null,"errorMessage":"Account.EmailUsernameErrors.EmailTooLong","messagePattern":"Account\\.EmailUsernameErrors\\.EmailTooLong","errorType":"validation","errorClass":"NopException","httpStatus":null,"severity":"warning","filePath":"src/Libraries/Nop.Services/Customers/CustomerRegistrationService.cs","lineNumber":511,"sourceCode":"    /// <param name=\"customer\">Customer</param>\r\n    /// <param name=\"newEmail\">New email</param>\r\n    /// <param name=\"requireValidation\">Require validation of new email address</param>\r\n    /// <returns>A task that represents the asynchronous operation</returns>\r\n    public virtual async Task SetEmailAsync(Customer customer, string newEmail, bool requireValidation)\r\n    {\r\n        ArgumentNullException.ThrowIfNull(customer);\r\n\r\n        if (newEmail == null)\r\n            throw new NopException(\"Email cannot be null\");\r\n\r\n        newEmail = newEmail.Trim();\r\n        var oldEmail = customer.Email;\r\n\r\n        if (!CommonHelper.IsValidEmail(newEmail))\r\n            throw new NopException(await _localizationService.GetResourceAsync(\"Account.EmailUsernameErrors.NewEmailIsNotValid\"));\r\n\r\n        if (newEmail.Length > 100)\r\n            throw new NopException(await _localizationService.GetResourceAsync(\"Account.EmailUsernameErrors.EmailTooLong\"));\r\n\r\n        var customer2 = await _customerService.GetCustomerByEmailAsync(newEmail);\r\n        if (customer2 != null && customer.Id != customer2.Id)\r\n            throw new NopException(await _localizationService.GetResourceAsync(\"Account.EmailUsernameErrors.EmailAlreadyExists\"));\r\n\r\n        if (requireValidation)\r\n        {\r\n            //re-validate email\r\n            customer.EmailToRevalidate = newEmail;\r\n            await _customerService.UpdateCustomerAsync(customer);\r\n\r\n            //email re-validation message\r\n            await _genericAttributeService.SaveAttributeAsync(customer, NopCustomerDefaults.EmailRevalidationTokenAttribute, Guid.NewGuid().ToString());\r\n            await _workflowMessageService.SendCustomerEmailRevalidationMessageAsync(customer, (await _workContext.GetWorkingLanguageAsync()).Id);\r\n        }\r\n        else\r\n        {\r\n            customer.Email = newEmail;\r","sourceCodeStart":493,"sourceCodeEnd":529,"githubUrl":"https://github.com/nopSolutions/nopCommerce/blob/64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2/src/Libraries/Nop.Services/Customers/CustomerRegistrationService.cs#L493-L529","documentation":"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.","triggerScenarios":"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.","commonSituations":"User pastes a malformed long string; an integration syncs a CRM record whose email field is oversized; test data with no length cap.","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."],"exampleFix":"// before\nawait _customerRegistrationService.SetEmailAsync(customer, email, requireValidation);\n\n// after\nemail = email?.Trim();\nif (email is { Length: > 100 })\n    ModelState.AddModelError(nameof(email), \"Email must be 100 characters or fewer.\");\nelse\n    await _customerRegistrationService.SetEmailAsync(customer, email, requireValidation);","handlingStrategy":"validation","validationCode":"var trimmed = newEmail?.Trim();\nif (trimmed is { Length: > 100 })\n    ModelState.AddModelError(nameof(newEmail), \"Email must be <= 100 chars.\");","typeGuard":"static bool IsEmailWithinLimit(string email) => email is null || email.Trim().Length <= 100;","tryCatchPattern":"try { await _customerRegistrationService.SetEmailAsync(customer, email, true); }\ncatch (NopException ex) { ModelState.AddModelError(nameof(email), ex.Message); }","preventionTips":["Set maxlength=100 on email inputs.","Trim before length checks.","Validate at the model layer with [StringLength(100)]."],"tags":["customer","email","validation","localization"],"backgroundTag":null,"analyzedSha":"64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2","analyzedAt":"2026-08-13T21:19:38.062Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}