Search code examples
c#asp.net-coresendgrid

Status Forbidden -When Sending Email with Sendgrid


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);

    }

Solution

  • 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.