0

I'm sending confirmation mail using SendGrid to confirm email but ApiKey always null

public Task SendAsync(IdentityMessage message)
{

    var ApiKey = Environment.GetEnvironmentVariable("ApiKeyFromSendGrid");
    var client = new SendGridClient(ApiKey);
    var from = new EmailAddress("XXX@gmail.com", "Any Name");
    var subject = "Sending with SendGrid is Fun";
    var to = new EmailAddress(message.Destination, "Any Name");
    var plainTextContent = "and easy to do anywhere, even with C#";
    var htmlContent = "<strong>and easy to do anywhere, even with C#</strong>";
    var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
    var response = client.SendEmailAsync(msg);

    return response;

}

where is the error? enter image description here

Jorge
  • 17,164
  • 18
  • 79
  • 125
Omar Seleim
  • 21
  • 1
  • 6

2 Answers2

0

The error is in the recovery of the environment variable:

Environment.GetEnvironmentVariable("ApiKeyFromSendGrid");

The ApiKeyFromSendGrid exist?

See if these questions and answers help you to retrieve the environment variable correctly:

How do I get and set Environment variables in C#?

Environment.GetEnvironmentVariable won't find variable value

George Wurthmann
  • 343
  • 1
  • 9
  • 17
0

You're using the wrong value for the API Key. You are using the API Key ID. You'll probably need to create a new key if you didn't store the original key value somewhere.

See SendGrid API Key not working. "The provided authorization grant is invalid, expired or revoked"

Justin Steele
  • 2,120
  • 12
  • 16