Code Vader
Code Vader

Reputation: 755

SendGrid not working in Azure App Service

I'm creating a MVC Core app and deploying it to an Azure App Service. I'm trying to send emails using SendGrid from the application which seems to be working fine in my local environment but does not work in production. I'm using free subscriptions for anything Azure.

I've followed this pretty much to the tee.

This type of question has popped up on stack overflow and github (here and here, etc), but after going through about 50 such posts nothing seems to be working for me. Reading through the documentation in SendGrid doesn't help a lot either because all the examples provided looks like my own code. I don't get any exceptions, and like I mentioned it works just fine locally.

Please help

Code

            string sendGridApiKey = _configuration["SENDGRID_API_KEY"];
            var client = new SendGridClient(sendGridApiKey);   

            var msg = new SendGridMessage();
            msg.SetFrom(new EmailAddress(email: "[email protected]",
                name: "ENR Management"));
            msg.AddTo(new EmailAddress(email: user.Email, name: user.FriendlyName));
            msg.SetSubject("Reset Password");
            msg.AddContent(MimeType.Html, $"Please reset your password by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'> clicking here </a>.");
            msg.AddContent(MimeType.Text, "Please reset your password by clicking the link");
            var response = await client.SendEmailAsync(msg).ConfigureAwait(false);

Being called by

        _emailService.SendResetPasswordEmail(
            user: user, 
            callbackUrl: callbackUrl).Wait();

appsettings.json

{
  "ConnectionStrings": {
    "DefaultConnection": "XXX",
    "ENRModelsDB": "XXX"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "SENDGRID_API_KEY": "SG.XXX",
  "AllowedHosts": "*"
}

I also have the same key/value in my App Service in Azure under Configuration -> Application setting for what it's worth.

Upvotes: 0

Views: 1049

Answers (2)

Code Vader
Code Vader

Reputation: 755

I finally found the issue and I feel so stupid.

I only send 1 email from my app, the password reset email. On my live environment, it would fail at this step in ForgotPassword.cshtml.cs (the scaffolded page)

        if (user == null || !(await _userManager.IsEmailConfirmedAsync(user)))
        {
            // Don't reveal that the user does not exist or is not confirmed
            return RedirectToPage("./ForgotPasswordConfirmation");
        }

because when I seeded the user I did not set email confirmed to be true. Could not have done it without the remote debug suggestion. It never even got to the part where it is supposed to send the email, and no errors reports because there was none.

Found some newer articles (here and here) to help with the remote debugging which came with its own rabbit holes.

Thanx for the suggestion @KodiaMx

Upvotes: 2

KodiakMx
KodiakMx

Reputation: 235

Could it be that your App Service has the configuration setup with different value?

enter image description here

Another suggestion to you is you to debug your app running in the App Service to see what exactly is happening.

Introduction to Remote Debugging on Azure Web Sites

*it is old but it will give you the idea.

Upvotes: 2

Related Questions