{"record":{"id":"b42f1fd4ee83ccb8","repo":"nopSolutions/nopCommerce","slug":"account-emailusernameerrors-emailalreadyexists","errorCode":null,"errorMessage":"Account.EmailUsernameErrors.EmailAlreadyExists","messagePattern":"Account\\.EmailUsernameErrors\\.EmailAlreadyExists","errorType":"validation","errorClass":"NopException","httpStatus":null,"severity":"warning","filePath":"src/Libraries/Nop.Services/Customers/CustomerRegistrationService.cs","lineNumber":515,"sourceCode":"    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\n            await _customerService.UpdateCustomerAsync(customer);\r\n\r\n            if (string.IsNullOrEmpty(oldEmail) || oldEmail.Equals(newEmail, StringComparison.InvariantCultureIgnoreCase))\r\n                return;\r","sourceCodeStart":497,"sourceCodeEnd":533,"githubUrl":"https://github.com/nopSolutions/nopCommerce/blob/64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2/src/Libraries/Nop.Services/Customers/CustomerRegistrationService.cs#L497-L533","documentation":"Thrown by SetEmailAsync when GetCustomerByEmailAsync(newEmail) finds another customer (different Id) already using that email, using localized resource 'Account.EmailUsernameErrors.EmailAlreadyExists'. Enforces email uniqueness across customers.","triggerScenarios":"Calling SetEmailAsync with an email already claimed by a different customer account. The lookup is case-insensitive in the standard implementation, so case variations also collide.","commonSituations":"User trying to switch to an email they registered with on a second account; an admin reassigning an email that's taken; duplicate-account cleanup attempts.","solutions":["Pre-check uniqueness: call GetCustomerByEmailAsync before attempting the change and warn the user.","If the duplicate is a stale/inactive account, delete or reassign it first so the email frees up.","Catch the NopException at the UI layer and surface the localized message as a validation error."],"exampleFix":"// before\nawait _customerRegistrationService.SetEmailAsync(customer, newEmail, requireValidation);\n\n// after\nvar owner = await _customerService.GetCustomerByEmailAsync(newEmail);\nif (owner is not null && owner.Id != customer.Id)\n    ModelState.AddModelError(nameof(newEmail), \"This email is already in use.\");\nelse\n    await _customerRegistrationService.SetEmailAsync(customer, newEmail, requireValidation);","handlingStrategy":"validation","validationCode":"var owner = await _customerService.GetCustomerByEmailAsync(newEmail);\nif (owner is not null && owner.Id != customer.Id)\n    ModelState.AddModelError(nameof(newEmail), \"Email already in use.\");","typeGuard":null,"tryCatchPattern":"try { await _customerRegistrationService.SetEmailAsync(customer, email, true); }\ncatch (NopException ex) { ModelState.AddModelError(nameof(email), ex.Message); }","preventionTips":["Pre-check email uniqueness before attempting the change.","Clean up duplicate/stale accounts so emails free up.","Remember email lookups are typically case-insensitive."],"tags":["customer","email","duplicate","localization","validation"],"backgroundTag":null,"analyzedSha":"64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2","analyzedAt":"2026-08-13T21:19:38.062Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}