{"record":{"id":"f4c4ae5e3646ff56","repo":"nopSolutions/nopCommerce","slug":"account-emailusernameerrors-newemailisnotvalid","errorCode":null,"errorMessage":"Account.EmailUsernameErrors.NewEmailIsNotValid","messagePattern":"Account\\.EmailUsernameErrors\\.NewEmailIsNotValid","errorType":"validation","errorClass":"NopException","httpStatus":null,"severity":"warning","filePath":"src/Libraries/Nop.Services/Customers/CustomerRegistrationService.cs","lineNumber":508,"sourceCode":"    /// <summary>\r\n    /// Sets a user email\r\n    /// </summary>\r\n    /// <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","sourceCodeStart":490,"sourceCodeEnd":526,"githubUrl":"https://github.com/nopSolutions/nopCommerce/blob/64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2/src/Libraries/Nop.Services/Customers/CustomerRegistrationService.cs#L490-L526","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nawait _customerRegistrationService.SetEmailAsync(customer, newEmail, requireValidation);\n\n// after\nif (!CommonHelper.IsValidEmail(newEmail))\n    ModelState.AddModelError(nameof(newEmail), \"Please enter a valid email address.\");\nelse\n    await _customerRegistrationService.SetEmailAsync(customer, newEmail, requireValidation);","handlingStrategy":"validation","validationCode":"if (!CommonHelper.IsValidEmail(newEmail))\n    ModelState.AddModelError(nameof(newEmail), \"Invalid email format.\");\nelse\n    await _customerRegistrationService.SetEmailAsync(customer, newEmail, requireValidation);","typeGuard":"static bool IsValidEmailFormat(string email) => !string.IsNullOrEmpty(email) && CommonHelper.IsValidEmail(email);","tryCatchPattern":"try { await _customerRegistrationService.SetEmailAsync(customer, email, true); }\ncatch (NopException ex) { ModelState.AddModelError(nameof(email), ex.Message); }","preventionTips":["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."],"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"}