{"record":{"id":"7ab944253e755887","repo":"dotnet/AspNetCore.Docs","slug":"null-emailauthkey","errorCode":null,"errorMessage":"Null EmailAuthKey","messagePattern":"Null EmailAuthKey","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"critical","filePath":"aspnetcore/blazor/security/account-confirmation-and-password-recovery.md","lineNumber":208,"sourceCode":"        string confirmationLink) => SendEmailAsync(email, \"Confirm your email\",\n        \"<html lang=\\\"en\\\"><head></head><body>Please confirm your account by \" +\n        $\"<a href='{confirmationLink}'>clicking here</a>.</body></html>\");\n\n    public Task SendPasswordResetLinkAsync(ApplicationUser user, string email,\n        string resetLink) => SendEmailAsync(email, \"Reset your password\",\n        \"<html lang=\\\"en\\\"><head></head><body>Please reset your password by \" +\n        $\"<a href='{resetLink}'>clicking here</a>.</body></html>\");\n\n    public Task SendPasswordResetCodeAsync(ApplicationUser user, string email,\n        string resetCode) => SendEmailAsync(email, \"Reset your password\",\n        \"<html lang=\\\"en\\\"><head></head><body>Please reset your password \" +\n        $\"using the following code:<br>{resetCode}</body></html>\");\n\n    public async Task SendEmailAsync(string toEmail, string subject, string message)\n    {\n        if (string.IsNullOrEmpty(Options.EmailAuthKey))\n        {\n            throw new Exception(\"Null EmailAuthKey\");\n        }\n\n        await Execute(Options.EmailAuthKey, subject, message, toEmail);\n    }\n\n    public async Task Execute(string apiKey, string subject, string message, \n        string toEmail)\n    {\n        var api = new MandrillApi(apiKey);\n        var mandrillMessage = new MandrillMessage(\"sarah@contoso.com\", toEmail, \n            subject, message);\n        await api.Messages.SendAsync(mandrillMessage);\n\n        logger.LogInformation(\"Email to {EmailAddress} sent!\", toEmail);\n    }\n}\n```\n","sourceCodeStart":190,"sourceCodeEnd":226,"githubUrl":"https://github.com/dotnet/AspNetCore.Docs/blob/c67a80103a1a74db20784debd919c7fdda96c510/aspnetcore/blazor/security/account-confirmation-and-password-recovery.md#L190-L226","documentation":"Exception(\"Null EmailAuthKey\") thrown by the sample EmailSender before calling the Mandrill API. The IOptions<AuthMessageSenderOptions>.EmailAuthKey is read in SendEmailAsync; if it is null or empty the sample refuses to send because the API key is required to authenticate with Mandrill. Guards against sending with no credentials.","triggerScenarios":"EmailAuthKey configuration value is missing from appsettings/secrets/user-secrets, or the AuthMessageSenderOptions was never bound from configuration. Any password-reset/confirmation flow that triggers SendEmailAsync will throw.","commonSituations":"Forgetting to set the EmailAuthKey user secret in Development; deployment missing the environment variable/secret; configuration section name mismatch so options binding yields null; running locally without `dotnet user-secrets set`.","solutions":["Set the EmailAuthKey in user secrets (Development) or environment variables/secrets store (Production): dotnet user-secrets set \"EmailAuthKey\" \"<key>\".","Confirm AuthMessageSenderOptions is bound via builder.Configuration.GetSection(...).Get<AuthMessageSenderOptions>() or IOptions<AuthMessageSenderOptions>.","Validate options at startup (ValidateOnStart) so misconfiguration fails fast rather than at first email send.","Throw a more specific ConfigurationException with the key name for clearer diagnostics."],"exampleFix":"// before\nif (string.IsNullOrEmpty(Options.EmailAuthKey))\n{\n    throw new Exception(\"Null EmailAuthKey\");\n}\n\n// after — validated options + clearer exception\nbuilder.Services.AddOptions<AuthMessageSenderOptions>()\n    .Bind(builder.Configuration.GetSection(\"AuthMessageSender\"))\n    .ValidateDataAnnotations()\n    .ValidateOnStart();\n\n// in sender\nif (string.IsNullOrWhiteSpace(Options.EmailAuthKey))\n{\n    throw new InvalidOperationException(\n        \"Configuration value 'AuthMessageSender:EmailAuthKey' is missing. \" +\n        \"Set it via user-secrets (Development) or environment variable (Production).\");\n}","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(builder.Configuration[\"AuthMessageSender:EmailAuthKey\"])) {\n    throw new InvalidOperationException(\"EmailAuthKey is not configured.\");\n}","typeGuard":null,"tryCatchPattern":"try { await emailSender.SendEmailAsync(to, subject, body); }\ncatch (Exception ex) when (ex.Message.Contains(\"EmailAuthKey\"))\n{\n    logger.LogCritical(ex, \"Email provider not configured.\");\n}","preventionTips":["Set EmailAuthKey via user-secrets (Development) and secrets/env (Production).","Bind AuthMessageSenderOptions and validate with DataAnnotations + ValidateOnStart.","Use named configuration keys consistently across code and config files.","Document required secrets in onboarding material."],"tags":["blazor","email","configuration","options","secrets","identity"],"backgroundTag":null,"analyzedSha":"c67a80103a1a74db20784debd919c7fdda96c510","analyzedAt":"2026-08-13T17:46:11.763Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}