26

i am trying to send email through sendgrid. But every time it return status Forbidden.

 public Task SendEmailAsync(string email, string subject, string message)
    {
        var apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
        return Execute(apiKey, subject, message, email);
    }

    public async Task Execute(string apiKey, string subject, string message, string email)
    {

        var client = new SendGridClient(apiKey);
        var from = new EmailAddress(Configuration["Email"], Configuration["Name"]);
        var to = new EmailAddress(email);

        var plainTextContent = message;
        var htmlContent =message;
        var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
        var response = await client.SendEmailAsync(msg);

    }
Muhammad Sami
  • 520
  • 2
  • 8
  • 21

2 Answers2

70

I suspect that you have not registered a "from" email address. You will get a forbidden response when you try to send the email in their example, because the form address is not registered to their account. Assuming you've created a free account, log in and go to https://app.sendgrid.com/settings/sender_auth . In the middle of the page you'll see "Verify Single Sender". If you are just exploring things you can use your own email for this.

GlennSills
  • 3,977
  • 26
  • 28
  • 11
    Thanks! Very disappointing that such information is not mentioned as part of their integration process - unless if I missed it. – Obakeng Molebatsi Apr 22 '20 at 17:24
  • This occurs too if you are sending with an unknown domain. E.g. If you send from gmail.com while the register domain is gmail.com.ae. I was having this issue and this was how I resolved it. – Ismail Umar Jul 27 '20 at 09:54
  • @ismail-umar Could you tell me more about Gmail? Having problems with sending the mail using their approach. – Andrew Polukhin Jan 06 '21 at 12:57
  • @AndrewBumetsov Which of the approaches are you talking about? Using sendgrid with gmail.com or sending mail from gmail.com using SMTP? – Ismail Umar Jan 06 '21 at 14:32
  • thx for the tip. I had exactly this situation! – Catweazle Sep 08 '21 at 07:57
0

In my case, the API key was wrong and the response had status Forbidden.

lukin155
  • 63
  • 8