I'm trying to implement SendGrid for emails and I'm running into some issues. I found the example here for c# and set up a quick desktop example based on this. I create a WPF application and put this in a button handler:
async void BtnSend_Click(object sender, RoutedEventArgs e)
{
var apikey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
var client = new SendGridClient(apikey);
var from = new EmailAddress("<<my email>>", "me");
var to = new EmailAddress("<<test receiver email>>", "you");
var msg = new SendGridMessage();
msg.SetFrom(from);
msg.AddTo(to);
msg.SetTemplateId("<<my template id>>");
var response = await client.SendEmailAsync(msg);
MessageBox.Show($"Status: {response.StatusCode}\n\nHeaders:\n{response.Headers}");
}
However when I click the button I get this:
I did go to the url on that header as well as found it elsewhere here. However that link is talking about browser based which this is not, plus it says to use one of the libraries which I am (I'm using the c# NuGet package).
What am I doing wrong? I will eventually put this on a web server in my AWS lambda API so I guess if I have to do that for testing I can but was hoping to do some testing and setup on desktop to familiarize before I move it over to the server...
If you set the environment variable after starting Visual Studio, you need to restart VS. VS launches applications with the set of environment variables it had when VS itself launched. If you set the variable after VS was launched, then VS doesn't have it, and consequently, neither will your application.