{"record":{"id":"c776a530ef9493e9","repo":"nopSolutions/nopCommerce","slug":"email-is-not-valid","errorCode":null,"errorMessage":"Email is not valid.","messagePattern":"Email is not valid\\.","errorType":"validation","errorClass":"NopException","httpStatus":null,"severity":"error","filePath":"src/Libraries/Nop.Core/CommonHelper.cs","lineNumber":42,"sourceCode":"    /// Get email validation regex\r\n    /// </summary>\r\n    /// <returns>Regular expression</returns>\r\n    [GeneratedRegex(EMAIL_EXPRESSION, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture, \"en-US\")]\r\n    public static partial Regex GetEmailRegex();\r\n\r\n    /// <summary>\r\n    /// Ensures the subscriber email or throw.\r\n    /// </summary>\r\n    /// <param name=\"email\">The email.</param>\r\n    /// <returns></returns>\r\n    public static string EnsureSubscriberEmailOrThrow(string email)\r\n    {\r\n        var output = EnsureNotNull(email);\r\n        output = output.Trim();\r\n        output = EnsureMaximumLength(output, 255);\r\n\r\n        if (!IsValidEmail(output))\r\n            throw new NopException(\"Email is not valid.\");\r\n\r\n        return output;\r\n    }\r\n\r\n    /// <summary>\r\n    /// Verifies that a string is in valid email format\r\n    /// </summary>\r\n    /// <param name=\"email\">Email to verify</param>\r\n    /// <returns>true if the string is a valid email address and false if it's not</returns>\r\n    public static bool IsValidEmail(string email)\r\n    {\r\n        if (string.IsNullOrEmpty(email))\r\n            return false;\r\n\r\n        email = email.Trim();\r\n\r\n        return GetEmailRegex().IsMatch(email);\r\n    }\r","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/nopSolutions/nopCommerce/blob/64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2/src/Libraries/Nop.Core/CommonHelper.cs#L24-L60","documentation":"Thrown by CommonHelper.EnsureSubscriberEmailOrThrow when the supplied email fails the RFC 5322 regex check (CommonHelper.IsValidEmail). It is the canonical guard used when a newsletter subscription is created or updated; the email is first trimmed and truncated to 255 chars, then must match the strict EMAIL_EXPRESSION regex. A NopException (not ArgumentException) is raised so the application's global exception filter can localize and display it.","triggerScenarios":"Calling NewsLetterSubscriptionService / anywhere EnsureSubscriberEmailOrThrow(email) is invoked with a malformed address (missing '@', invalid TLD, embedded spaces, quoted-local-part the regex rejects, or a value that after trimming exceeds structural limits). Also fires if the caller passes an already-truncated string that breaks the local-part or domain.","commonSituations":"User pastes a typo'd address on the newsletter signup box; an integration feeds a display-name format like 'John Doe <j@x.com>' instead of the bare address; migration scripts importing legacy subscriber lists with unvalidated values; trailing/leading whitespace plus a stray comma from CSV import.","solutions":["Validate the input with CommonHelper.IsValidEmail(email) BEFORE calling EnsureSubscriberEmailOrThrow, and surface a friendly validation message on false.","Strip display-name syntax and surrounding angle brackets/punctuation (e.g. extract the part between < >) before validating.","Reject or normalize whitespace-only and null submissions at the controller/UI layer so they never reach the helper.","If importing bulk lists, run them through the same IsValidEmail filter and quarantine rejects rather than inserting directly."],"exampleFix":"// before\nvar sub = CommonHelper.EnsureSubscriberEmailOrThrow(rawEmail);\n\n// after\nif (!CommonHelper.IsValidEmail(rawEmail?.Trim() ?? string.Empty))\n    return BadRequest(\"Please enter a valid email address.\");\nvar sub = CommonHelper.EnsureSubscriberEmailOrThrow(rawEmail);","handlingStrategy":"validation","validationCode":"var clean = (email ?? string.Empty).Trim();\nif (!CommonHelper.IsValidEmail(clean))\n    // reject before calling EnsureSubscriberEmailOrThrow\n    return;\nvar validated = CommonHelper.EnsureSubscriberEmailOrThrow(clean);","typeGuard":"static bool IsSubscribableEmail(string email)\n    => !string.IsNullOrWhiteSpace(email)\n       && email.Trim().Length <= 255\n       && CommonHelper.IsValidEmail(email.Trim());","tryCatchPattern":"try { var sub = CommonHelper.EnsureSubscriberEmailOrThrow(email); }\ncatch (NopException ex) when (ex.Message == \"Email is not valid.\")\n{ /* show localized validation error, do not crash */ }","preventionTips":["Run IsValidEmail on raw input at the UI/API boundary before persisting a subscription.","Normalize input (trim, strip display-name/angle brackets) before validation.","For CSV imports, filter rows through IsValidEmail and quarantine failures."],"tags":["validation","email","nopcommerce","user-input"],"backgroundTag":null,"analyzedSha":"64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2","analyzedAt":"2026-08-13T21:19:38.062Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}