8

I've just created a sendgrid account. Then I went to settings=>API Keys and clicked on "Create API Key" and gave any possible permission.

Then I've created a c# project, added nuget packages and put my write the hello world code from here

 public async Task HelloEmail()
 {
            dynamic sg = new SendGrid.SendGridAPIClient("XXX-XXXXXXXXXXXXXXXXXX", "https://api.sendgrid.com");

        Email from = new Email("MY@Email.com");

        String subject = "Hello World from the SendGrid CSharp Library";
        Email to = new Email("test@example.com");
        Content content = new Content("text/plain", "Textual content");
        Mail mail = new Mail(from, subject, to, content);
        Email email = new Email("test2@example.com");
        mail.Personalization[0].AddTo(email);

        dynamic response = await sg.client.mail.send.post(requestBody: mail.Get());

        var x=response.StatusCode;
        var y = response.Body.ReadAsStringAsync().Result;
        var z = response.Headers.ToString();
 }

But I get

Unauthorized =>

"{\"errors\":[{\"message\":\"The provided authorization grant is invalid, expired, or revoked\",\"field\":null,\"help\":null}]}"


In the example, they got the API key from the EnvironmentVariableTarget.User is it related to that?

string apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY", EnvironmentVariableTarget.User);
dynamic sg = new SendGridAPIClient(apiKey);

*The problem is that no one reads messages when creating a key, also Microsoft chooses to show us "API Key ID" which is worst name ever

It is not a duplicate because although the reason was the same, no one would guess it since in c# we use a nuget library, not the api.

Ashkan S
  • 10,464
  • 6
  • 51
  • 80
  • Possible duplicate of [Send Grid seems to be preventing my node js server sending emails](http://stackoverflow.com/questions/34789622/send-grid-seems-to-be-preventing-my-node-js-server-sending-emails) – bwest Nov 09 '16 at 16:00
  • 1
    @bwest it is not a duplicate since we use nuget library and in the other question they used the api. So although the answer is the same, questions are different and no one would guess it unless he knows the problem, or reads the correct question – Ashkan S Nov 09 '16 at 18:30
  • It's the same exact error message, root cause, and solution, regardless of language, therefore it's the same fundamental problem. That's the criteria for a duplicate on SO. But we'll see what the mods do. – bwest Nov 09 '16 at 18:48

2 Answers2

3

Something is wrong with your API key. Check this answer, generate a new key, and double check your permissions.

You also don't need to specify the URL in your SendGrid.SendGridAPIClient. I'd remove that line to reduce hardcoded values.

Community
  • 1
  • 1
bwest
  • 9,182
  • 3
  • 28
  • 58
-1

Put key directly , do not use System.getenv(KEY)

String key = "YOUR KEY";
 SendGrid sg = new SendGrid(key);
sneha
  • 107
  • 1
  • 2
  • 3
    -1 for anti-pattern. placing the key directly in code should only be for development purposes. if you rotate your keys (bc security told you to) you will have to redeploy the app. you also don't want keys, even if they're stale to be stored in source control. to avoid a stick note with keys, consider a tool like Azure DevOps (Library), or GitHub secret manager to store keys for development and use Azure Key Vault, AWS Secrets Manager, or HashiCorp's Vault. – daviesdoesit Nov 03 '21 at 15:26